Skip to content

Commit

Permalink
COM-160: Fix clearable for FinalFormSelect with multiple (#1365)
Browse files Browse the repository at this point in the history
When the `multiple` prop is set, the value is an array.
Therefore, checking if the select has a value must be done with
`array.length` and clearing the value must set it to an empty array
instead of `undefined`.
  • Loading branch information
jamesricky authored Nov 14, 2023
1 parent a9ebbde commit eac9990
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .changeset/shy-suns-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@comet/admin": patch
---

Fix the clear-button in `FinalFormSelect` when using it with the `multiple` prop.

- The clear button is now only shown when at least one value is selected.
- Clearing the value now sets it to an empty array instead of `undefined`, which would cause an error when trying to render the select.
13 changes: 13 additions & 0 deletions packages/admin/admin-stories/src/admin/form/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function Story() {
<Form
initialValues={{
multipleFlavours: [],
multipleFlavoursClearable: [],
}}
onSubmit={() => {
//
Expand Down Expand Up @@ -133,6 +134,18 @@ function Story() {
</FinalFormSelect>
)}
</Field>

<Field name="multipleFlavoursClearable" label="Multiple Flavours Clearable" fullWidth>
{(props) => (
<FinalFormSelect {...props} fullWidth multiple clearable>
{options.map((option) => (
<MenuItem value={option.value} key={option.value}>
{option.label}
</MenuItem>
))}
</FinalFormSelect>
)}
</Field>
</CardContent>
</Card>
</form>
Expand Down
21 changes: 18 additions & 3 deletions packages/admin/admin/src/form/FinalFormSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,32 @@ export const FinalFormSelect = <T,>({
children,
endAdornment,
clearable,
multiple,
...rest
}: FinalFormSelectProps<T> & Partial<AsyncOptionsProps<T>> & Omit<SelectProps, "input">) => {
const selectEndAdornment = clearable ? (
<ClearInputAdornment position="end" hasClearableContent={Boolean(value)} onClick={() => onChange(undefined)} />
<ClearInputAdornment
position="end"
hasClearableContent={Boolean(multiple ? (Array.isArray(value) ? value.length : value) : value)}
onClick={() => onChange(multiple ? [] : undefined)}
/>
) : (
endAdornment
);

const selectProps = {
...rest,
multiple,
endAdornment: selectEndAdornment,
name,
onChange,
onFocus,
onBlur,
};

if (children) {
return (
<Select {...rest} endAdornment={selectEndAdornment} name={name} onChange={onChange} value={value} onFocus={onFocus} onBlur={onBlur}>
<Select {...selectProps} value={value}>
{children}
</Select>
);
Expand All @@ -53,7 +68,7 @@ export const FinalFormSelect = <T,>({
}

return (
<Select {...rest} endAdornment={selectEndAdornment} name={name} onChange={onChange} value={value} onFocus={onFocus} onBlur={onBlur}>
<Select {...selectProps} value={value}>
{options.length === 0 && (loading || value) && (
<MenuItem value={value as any} key={JSON.stringify(value)}>
{loading ? <CircularProgress size={20} /> : getOptionLabel(value)}
Expand Down

0 comments on commit eac9990

Please sign in to comment.