Skip to content

Commit 9b1b545

Browse files
authored
Merge pull request #822 from buildo/fix-807
2 parents 8c7c987 + 22fbf93 commit 9b1b545

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

packages/bento-design-system/src/SelectField/BaseSelect.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export function BaseSelect<A>(props: Props<A>) {
5555
autoFocus,
5656
menuSize = dropdownConfig.defaultMenuSize,
5757
searchable,
58+
clearable = true,
5859
} = props;
5960

6061
return (
@@ -79,7 +80,11 @@ export function BaseSelect<A>(props: Props<A>) {
7980
onChange(multiValue.map((a) => a.value));
8081
} else {
8182
const singleValue = o as SingleValueT<SelectOption<A>>;
82-
onChange(singleValue == null ? undefined : singleValue.value);
83+
if (clearable) {
84+
onChange(singleValue == null ? undefined : singleValue.value);
85+
} else {
86+
singleValue != null && onChange(singleValue.value);
87+
}
8388
}
8489
}}
8590
onBlur={onBlur}

packages/bento-design-system/src/SelectField/types.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ export type SelectOption<A> = Omit<
1111

1212
export type BaseSingleProps = {
1313
isMulti?: false;
14+
clearable?: boolean;
1415
};
1516

1617
export type BaseMultiProps = {
1718
isMulti: true;
1819
showMultiSelectBulkActions?: boolean;
1920
selectAllButtonLabel?: LocalizedString;
2021
clearAllButtonLabel?: LocalizedString;
22+
clearable?: never;
2123
} & (
2224
| {
2325
multiSelectMode?: "summary";

packages/bento-design-system/stories/Components/SelectField.stories.tsx

+22
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
SelectField,
88
BentoConfigProvider,
99
SelectFieldProps,
10+
SelectOption,
1011
} from "..";
12+
import { useState } from "react";
1113

1214
const meta = {
1315
component: SelectField,
@@ -191,3 +193,23 @@ export const CustomListConfig = {
191193
),
192194
],
193195
} satisfies Story;
196+
197+
// NOTE(gabro): using a render function instead of just args, because the `value/onChange` trick we
198+
// use to make the story controlled doesn't play well with the implementation of `clearable`
199+
export const NotClearable = {
200+
args: {
201+
clearable: false,
202+
},
203+
render: (args) => {
204+
const [value, onChange] = useState<number | undefined>(undefined);
205+
return (
206+
<SelectField
207+
label={args.label}
208+
clearable={args.clearable}
209+
options={args.options as SelectOption<number>[]}
210+
value={value}
211+
onChange={onChange}
212+
/>
213+
);
214+
},
215+
} satisfies Story;

0 commit comments

Comments
 (0)