From 871ccf6d906c139318059c39bcda7a8594c9d955 Mon Sep 17 00:00:00 2001 From: Allison King Date: Tue, 21 Feb 2023 10:11:12 -0500 Subject: [PATCH] Stack creatable multiselect (#2620) --- .../src/features/common/form/inputs.tsx | 299 ++++++++++-------- .../features/system/DescribeSystemStep.tsx | 6 +- .../admin-ui/src/features/taxonomy/hooks.tsx | 6 +- 3 files changed, 168 insertions(+), 143 deletions(-) diff --git a/clients/admin-ui/src/features/common/form/inputs.tsx b/clients/admin-ui/src/features/common/form/inputs.tsx index c580812d97..c9c065872c 100644 --- a/clients/admin-ui/src/features/common/form/inputs.tsx +++ b/clients/admin-ui/src/features/common/form/inputs.tsx @@ -120,6 +120,24 @@ const ErrorMessage = ({ ); }; +export interface Option { + value: string; + label: string; +} +interface SelectProps { + label: string; + labelProps?: FormLabelProps; + tooltip?: string; + options: Option[]; + isDisabled?: boolean; + isSearchable?: boolean; + isClearable?: boolean; + isRequired?: boolean; + size?: Size; + isMulti?: boolean; + variant?: Variant; + menuPosition?: MenuPosition; +} const SelectInput = ({ options, fieldName, @@ -131,7 +149,7 @@ const SelectInput = ({ menuPosition = "absolute", }: { fieldName: string; isMulti?: boolean } & Omit) => { const [initialField] = useField(fieldName); - const field = { ...initialField, value: initialField.value ?? [] }; + const field = { ...initialField, value: initialField.value ?? "" }; const selected = isMulti ? options.filter((o) => field.value.indexOf(o.value) >= 0) : options.find((o) => o.value === field.value) || null; @@ -203,6 +221,89 @@ const SelectInput = ({ ); }; +interface CreatableSelectProps extends SelectProps { + /** Do not render the dropdown menu */ + disableMenu?: boolean; +} +const CreatableSelectInput = ({ + options, + fieldName, + size, + isSearchable, + isClearable, + isMulti, + disableMenu, +}: { fieldName: string } & Omit) => { + const [initialField] = useField(fieldName); + const value: string[] | string = initialField.value ?? []; + const field = { ...initialField, value }; + const selected = Array.isArray(field.value) + ? field.value.map((v) => ({ label: v, value: v })) + : { label: field.value, value: field.value }; + + const { setFieldValue, touched, setTouched } = useFormikContext(); + + const handleChangeMulti = (newValue: MultiValue