Skip to content

Commit

Permalink
feat(Select): add closeOnSelect property
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonid Fedorov committed Oct 25, 2024
1 parent b6516f4 commit 8b3bc5a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const Select = React.forwardRef<HTMLButtonElement, SelectProps>(function
onClose,
id,
hasCounter,
closeOnSelect,
renderCounter,
title,
} = props;
Expand All @@ -116,6 +117,7 @@ export const Select = React.forwardRef<HTMLButtonElement, SelectProps>(function
defaultValue,
defaultOpen,
multiple,
closeOnSelect,
open: propsOpen,
onClose,
onOpenChange,
Expand Down
4 changes: 3 additions & 1 deletion src/components/Select/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ export type SelectProps<T = any> = QAProps &
| React.ReactElement<SelectOptionGroup<T>, typeof OptionGroup>
| React.ReactElement<SelectOptionGroup<T>, typeof OptionGroup>[];
id?: string;
/**Shows selected options count if multiple selection is avalable */
/** Shows selected options count if multiple selection is avalable */
hasCounter?: boolean;
/** Close popup when value changed */
closeOnSelect?: boolean;
title?: string;
name?: string;
form?: string;
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useSelect/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type UseSelectProps = {
value?: string[];
defaultValue?: string[];
multiple?: boolean;
closeOnSelect?: boolean;
onUpdate?: (value: string[]) => void;
disabled?: boolean;
} & UseOpenProps;
Expand Down
22 changes: 19 additions & 3 deletions src/hooks/useSelect/useSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const useSelect = <T extends unknown>({
value: valueProps,
defaultValue = [],
multiple,
closeOnSelect: propsCloseOnSelect,
onUpdate,
disabled,
}: UseSelectProps): UseSelectResult<T> => {
Expand All @@ -25,6 +26,13 @@ export const useSelect = <T extends unknown>({
open,
});

/**
* Old behaviour backward compatibility.
* For multiple select - false.
* For single select - true.
*/
const closeOnSelect = typeof propsCloseOnSelect === 'boolean' ? propsCloseOnSelect : !multiple;

const setValue = React.useCallback(
(v: string[]) => {
if (!disabled) {
Expand All @@ -34,16 +42,22 @@ export const useSelect = <T extends unknown>({
[setValueInner, disabled],
);

const handleCloseOnSelect = React.useCallback(() => {
if (closeOnSelect) {
toggleOpen(false);
}
}, [closeOnSelect, toggleOpen]);

const handleSingleSelection = React.useCallback(
(option: UseSelectOption<T>) => {
if (!value.includes(option.value)) {
const nextValue = [option.value];
setValue(nextValue);
}

toggleOpen(false);
handleCloseOnSelect();
},
[value, setValue, toggleOpen],
[value, setValue, handleCloseOnSelect],
);

const handleMultipleSelection = React.useCallback(
Expand All @@ -54,8 +68,10 @@ export const useSelect = <T extends unknown>({
: [...value, option.value];

setValue(nextValue);

handleCloseOnSelect();
},
[value, setValue],
[value, setValue, handleCloseOnSelect],
);

const handleSelection = React.useCallback(
Expand Down

0 comments on commit 8b3bc5a

Please sign in to comment.