From e5f53a8a12fe6351a350d714e7a6ce21bba73cd8 Mon Sep 17 00:00:00 2001 From: thatmattlove Date: Wed, 14 Aug 2024 13:01:18 -0400 Subject: [PATCH] fix single select clear --- src/components/form/select-field.tsx | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/components/form/select-field.tsx b/src/components/form/select-field.tsx index dc0181a..4ddceb9 100644 --- a/src/components/form/select-field.tsx +++ b/src/components/form/select-field.tsx @@ -27,7 +27,6 @@ function selectMultiple( matching.push({ value: value[0], label: value[0] }); } } - return matching; }, []); } @@ -38,7 +37,7 @@ function selectSingle( creatable: boolean, ): SelectOptionSingle | null { if (Array.isArray(options)) { - if (creatable) { + if (creatable && value !== null) { return { value, label: value }; } for (const option of options) { @@ -58,7 +57,7 @@ export const SelectField = (props: SelectFieldProps) => { isMulti = false, required = false, creatable = false, - defaultValue = [], + defaultValue = isMulti ? [] : null, ...rest } = props; const { setValue, control } = useFormContext(); @@ -72,6 +71,11 @@ export const SelectField = (props: SelectFieldProps) => { const handleSelect = useCallback( (values: readonly SelectOptionSingle[]) => { + if (values.length === 0) { + setValue(name, defaultValue); + onChange(defaultValue); + return; + } const labels = values.filter(v => !!v.label).map(v => v.label); setValue(name, labels); onChange(labels);