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(input): ensure clear button is not focusable when disabled #3774

Merged
merged 8 commits into from
Sep 26, 2024
5 changes: 5 additions & 0 deletions .changeset/two-waves-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/input": patch
---

clear button should not receive focus when input is disabled.
8 changes: 8 additions & 0 deletions packages/components/input/__tests__/input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ describe("Input", () => {
expect(container.querySelector("input")).toHaveAttribute("disabled");
});

it("should set tabIndex to -1 when isDisabled is true", () => {
wingkwong marked this conversation as resolved.
Show resolved Hide resolved
const {getByRole} = render(<Input isClearable isDisabled label="test input" />);

const clearButton = getByRole("button");

expect(clearButton).toHaveAttribute("tabIndex", "-1");
});

it("should have required attribute when isRequired with native validationBehavior", () => {
const {container} = render(<Input isRequired label="test input" validationBehavior="native" />);

Expand Down
2 changes: 1 addition & 1 deletion packages/components/input/src/use-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
return {
...props,
role: "button",
tabIndex: 0,
tabIndex: originalProps?.isDisabled ? -1 : 0,
wingkwong marked this conversation as resolved.
Show resolved Hide resolved
"aria-label": "clear input",
"data-slot": "clear-button",
"data-focus-visible": dataAttr(isClearButtonFocusVisible),
Expand Down