Skip to content

Commit

Permalink
fix single select clear
Browse files Browse the repository at this point in the history
  • Loading branch information
thatmattlove committed Aug 14, 2024
1 parent eaef820 commit e5f53a8
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/components/form/select-field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ function selectMultiple(
matching.push({ value: value[0], label: value[0] });
}
}

return matching;
}, []);
}
Expand All @@ -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) {
Expand All @@ -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();
Expand All @@ -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);
Expand Down

0 comments on commit e5f53a8

Please sign in to comment.