Skip to content
Merged
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
16 changes: 1 addition & 15 deletions packages/@react-stately/combobox/src/useComboBoxState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,26 +306,12 @@ export function useComboBoxState<T extends object>(props: ComboBoxStateOptions<T
setFocusedState(isFocused);
};

let selectionManagerProxy = useMemo(() => {
let proxy = new Proxy(selectionManager, {
get(target, prop, receiver) {
// If the menu is closed, don't update the selected key.
if (prop === 'replaceSelection' && !triggerState.isOpen) {
return () => {};
}
return Reflect.get(target, prop, receiver);
}
});

return proxy;
}, [selectionManager, triggerState.isOpen]);

return {
...triggerState,
toggle,
open,
close,
selectionManager: selectionManagerProxy,
selectionManager,
selectedKey,
setSelectedKey,
disabledKeys,
Expand Down
15 changes: 1 addition & 14 deletions packages/@react-stately/combobox/test/useComboBoxState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ describe('useComboBoxState tests', function () {
expect(result.current.selectionManager.selectedKeys).toContain('0');
expect(result.current.selectionManager.selectedKeys).not.toContain('1');

act(() => {result.current.open();});
act(() => result.current.selectionManager.replaceSelection('1'));
expect(result.current.selectionManager.selectedKeys).toContain('0');
expect(result.current.selectionManager.selectedKeys).not.toContain('1');
Expand All @@ -191,34 +190,22 @@ describe('useComboBoxState tests', function () {
expect(result.current.selectionManager.selectedKeys).toContain('0');
expect(result.current.selectionManager.selectedKeys).not.toContain('1');

act(() => {result.current.open();});
act(() => result.current.selectionManager.replaceSelection('1'));
expect(result.current.selectionManager.selectedKeys).toContain('1');
expect(result.current.selectionManager.selectedKeys).not.toContain('0');
expect(onSelectionChange).toHaveBeenCalledWith('1');
});

it('supports default no selection', function () {
it('supports sdefault no selection', function () {
let initialProps = {...defaultProps};
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});
expect(result.current.selectionManager.selectionMode).toBe('single');
expect(result.current.selectionManager.selectedKeys.size).toBe(0);

act(() => {result.current.open();});
act(() => result.current.selectionManager.replaceSelection('1'));
expect(result.current.selectionManager.selectedKeys).toContain('1');
expect(result.current.selectionManager.selectedKeys).not.toContain('0');
expect(onSelectionChange).toHaveBeenCalledWith('1');
});

it('won\'t perform replace a selection if the combobox is closed', function () {
// This case covers if a option in the menu is clicked while the menu is closing
let initialProps = {...defaultProps};
let {result} = renderHook((props) => useComboBoxState(props), {initialProps});

act(() => result.current.selectionManager.replaceSelection('1'));
expect(result.current.selectionManager.selectedKeys.size).toEqual(0);
expect(onSelectionChange).toHaveBeenCalledTimes(0);
});
});
});