diff --git a/packages/bento-design-system/src/SelectField/BaseSelect.tsx b/packages/bento-design-system/src/SelectField/BaseSelect.tsx index 468eef098..2c6b4f364 100644 --- a/packages/bento-design-system/src/SelectField/BaseSelect.tsx +++ b/packages/bento-design-system/src/SelectField/BaseSelect.tsx @@ -55,6 +55,7 @@ export function BaseSelect(props: Props) { autoFocus, menuSize = dropdownConfig.defaultMenuSize, searchable, + clearable = true, } = props; return ( @@ -79,7 +80,11 @@ export function BaseSelect(props: Props) { onChange(multiValue.map((a) => a.value)); } else { const singleValue = o as SingleValueT>; - onChange(singleValue == null ? undefined : singleValue.value); + if (clearable) { + onChange(singleValue == null ? undefined : singleValue.value); + } else { + singleValue != null && onChange(singleValue.value); + } } }} onBlur={onBlur} diff --git a/packages/bento-design-system/src/SelectField/types.ts b/packages/bento-design-system/src/SelectField/types.ts index 88e7a2a6a..d6b416d1a 100644 --- a/packages/bento-design-system/src/SelectField/types.ts +++ b/packages/bento-design-system/src/SelectField/types.ts @@ -11,6 +11,7 @@ export type SelectOption = Omit< export type BaseSingleProps = { isMulti?: false; + clearable?: boolean; }; export type BaseMultiProps = { @@ -18,6 +19,7 @@ export type BaseMultiProps = { showMultiSelectBulkActions?: boolean; selectAllButtonLabel?: LocalizedString; clearAllButtonLabel?: LocalizedString; + clearable?: never; } & ( | { multiSelectMode?: "summary"; diff --git a/packages/bento-design-system/stories/Components/SelectField.stories.tsx b/packages/bento-design-system/stories/Components/SelectField.stories.tsx index 5ddc843b9..fc0c01a0c 100644 --- a/packages/bento-design-system/stories/Components/SelectField.stories.tsx +++ b/packages/bento-design-system/stories/Components/SelectField.stories.tsx @@ -7,7 +7,9 @@ import { SelectField, BentoConfigProvider, SelectFieldProps, + SelectOption, } from ".."; +import { useState } from "react"; const meta = { component: SelectField, @@ -191,3 +193,23 @@ export const CustomListConfig = { ), ], } satisfies Story; + +// NOTE(gabro): using a render function instead of just args, because the `value/onChange` trick we +// use to make the story controlled doesn't play well with the implementation of `clearable` +export const NotClearable = { + args: { + clearable: false, + }, + render: (args) => { + const [value, onChange] = useState(undefined); + return ( + []} + value={value} + onChange={onChange} + /> + ); + }, +} satisfies Story;