Skip to content

Commit

Permalink
fix(input): prevent clearable when input is readonly (#3643)
Browse files Browse the repository at this point in the history
* fix(input): prevent clearable when input is readonly

* test(input): add tests for isReadOnly and isClearable interaction

* chore(changeset): add changeset for fixing clear button visibility with isReadOnly

* fix(input): disable clear button from input is readonly

* test(input): disable clear button when input is readonly

* chore(changeset): update changeset

* chore(changeset): revise message

---------

Co-authored-by: WK Wong <wingkwong.code@gmail.com>
  • Loading branch information
ryxxn and wingkwong authored Sep 3, 2024
1 parent f36df43 commit 3d68655
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/soft-wombats-wash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/input": patch
---

disable clear button when input is read-only
27 changes: 27 additions & 0 deletions packages/components/input/__tests__/input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,33 @@ describe("Input", () => {

expect(inputs[1]).toBeVisible();
});

it("should disable clear button when isReadOnly is true", async () => {
const onClear = jest.fn();

const ref = React.createRef<HTMLInputElement>();

const {getByRole} = render(
<Input
ref={ref}
isClearable
isReadOnly
defaultValue="readOnly test for clear button"
label="test input"
onClear={onClear}
/>,
);

const clearButton = getByRole("button");

expect(clearButton).not.toBeNull();

const user = userEvent.setup();

await user.click(clearButton);

expect(onClear).toHaveBeenCalledTimes(0);
});
});

describe("Input with React Hook Form", () => {
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 @@ -248,7 +248,7 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
});

const {pressProps: clearPressProps} = usePress({
isDisabled: !!originalProps?.isDisabled,
isDisabled: !!originalProps?.isDisabled || !!originalProps?.isReadOnly,
onPress: handleClear,
});

Expand Down

0 comments on commit 3d68655

Please sign in to comment.