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): disabled select shouldn't get update by keyboard #2649

Merged
merged 3 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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()].map((k) => k)),
}),
};

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