Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Select): do not trigger onOpenChange in case of controlled open #1667

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ export const Select = React.forwardRef<HTMLButtonElement, SelectProps>(function
const isErrorIconVisible =
isErrorStateVisible && Boolean(errorMessage) && errorPlacement === 'inside';

const toggleOpenIfNeeded = React.useCallback(
(nextOpen?: boolean) => {
if (typeof propsOpen === 'undefined') {
toggleOpen(nextOpen);
}
},
[propsOpen, toggleOpen],
);

const handleOptionClick = React.useCallback(
(option?: FlattenOption) => {
if (!option || option?.disabled || 'label' in option) {
Expand Down Expand Up @@ -215,12 +224,12 @@ export const Select = React.forwardRef<HTMLButtonElement, SelectProps>(function
}
if ([KeyCode.ARROW_DOWN, KeyCode.ARROW_UP].includes(e.key) && !open) {
e.preventDefault();
toggleOpen();
toggleOpenIfNeeded();
}

listRef?.current?.onKeyDown(e);
},
[handleOptionClick, open, toggleOpen],
[open, handleOptionClick, toggleOpenIfNeeded],
);

const handleFilterKeyDown = React.useCallback((e: React.KeyboardEvent<HTMLElement>) => {
Expand Down Expand Up @@ -262,7 +271,7 @@ export const Select = React.forwardRef<HTMLButtonElement, SelectProps>(function
inlineStyles.width = width;
}

const handleClose = React.useCallback(() => toggleOpen(false), [toggleOpen]);
const handleClose = React.useCallback(() => toggleOpenIfNeeded(false), [toggleOpenIfNeeded]);
const {onFocus, onBlur} = props;
const {focusWithinProps} = useFocusWithin({
onFocusWithin: onFocus,
Expand Down Expand Up @@ -332,7 +341,7 @@ export const Select = React.forwardRef<HTMLButtonElement, SelectProps>(function
style={inlineStyles}
>
<SelectControl
toggleOpen={toggleOpen}
toggleOpen={toggleOpenIfNeeded}
hasClear={hasClear}
clearValue={handleClearValue}
ref={handleControlRef}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,13 @@ describe('Select base actions', () => {
expect(onOpenChange).toHaveBeenCalledWith(false);
expect(onOpenChange).toHaveBeenCalledTimes(2);
});
test('should call onOpenChange whith controlled open', async () => {
test('should not call onOpenChange with controlled open', async () => {
const onOpenChange = jest.fn();
setup({open: true, onOpenChange});

await toggleSelectPopup();

expect(onOpenChange).toHaveBeenCalledWith(false);
expect(onOpenChange).toHaveBeenCalledTimes(1);
expect(onOpenChange).toHaveBeenCalledTimes(0);
});
});

Expand Down
7 changes: 4 additions & 3 deletions src/hooks/useSelect/useSelect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ export const useSelect = <T extends unknown>({
const nextValue = [option.value];
setValue(nextValue);
}

toggleOpen(false);
if (typeof open === 'undefined') {
toggleOpen(false);
}
},
[value, setValue, toggleOpen],
[value, open, setValue, toggleOpen],
);

const handleMultipleSelection = React.useCallback(
Expand Down
Loading