Skip to content

Commit

Permalink
fix(select): disabled select shouldn't get update by keyboard (#2649)
Browse files Browse the repository at this point in the history
* fix(select): disable all keys for isDisabled case

* fix(select): add "disabled select shouldn't update by keyboard" test case

* refactor(select): remove unnecessary map
  • Loading branch information
wingkwong committed Apr 14, 2024
1 parent c2ef4c0 commit e458432
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/nervous-geckos-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/select": patch
---

Fixed Disabled Select Allows Changes Using Blur + Keyboard (#2345)
35 changes: 35 additions & 0 deletions packages/components/select/__tests__/select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -361,4 +361,39 @@ describe("Select", () => {
// assert that the select is closed
expect(select).toHaveAttribute("aria-expanded", "false");
});

it("disabled select shouldn't update by keyboard", async () => {
let onSelectionChange = jest.fn();

const wrapper = render(
<Select
isDisabled
aria-label="Favorite Animal"
data-testid="test-select"
label="Favorite Animal"
selectionMode="single"
value="penguin"
onSelectionChange={onSelectionChange}
>
<SelectItem key="penguin" value="penguin">
Penguin
</SelectItem>
<SelectItem key="zebra" value="zebra">
Zebra
</SelectItem>
<SelectItem key="shark" value="shark">
Shark
</SelectItem>
</Select>,
);
const select = wrapper.getByTestId("test-select");

await act(async () => {
await userEvent.click(document.body);
await userEvent.tab();
await userEvent.type(select, "z", {skipClick: true});

expect(onSelectionChange).toBeCalledTimes(0);
});
});
});
9 changes: 8 additions & 1 deletion packages/components/select/src/use-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export function useSelect<T extends object>(originalProps: UseSelectProps<T>) {
const listBoxRef = useRef<HTMLUListElement>(null);
const popoverRef = useRef<HTMLDivElement>(null);

const state = useMultiSelectState<T>({
let state = useMultiSelectState<T>({
...props,
isOpen,
selectionMode,
Expand Down Expand Up @@ -235,6 +235,13 @@ export function useSelect<T extends object>(originalProps: UseSelectProps<T>) {
},
});

state = {
...state,
...(originalProps?.isDisabled && {
disabledKeys: new Set([...state.collection.getKeys()]),
}),
};

const {labelProps, triggerProps, valueProps, menuProps, descriptionProps, errorMessageProps} =
useMultiSelect(
{...props, disallowEmptySelection, isDisabled: originalProps?.isDisabled},
Expand Down

0 comments on commit e458432

Please sign in to comment.