Skip to content

Commit

Permalink
fix(select): controlled isInvalid prop (#4082)
Browse files Browse the repository at this point in the history
* fix(select): controlled isInvalid prop

* chore: add changeset

* Merge branch 'beta/release-next' into pr/4082

---------

Co-authored-by: WK Wong <wingkwong.code@gmail.com>
  • Loading branch information
chirokas and wingkwong authored Nov 28, 2024
1 parent 50a4d56 commit 1e05a72
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-spies-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/select": patch
---

Fix Controlled IsInvalid Prop In Select
36 changes: 36 additions & 0 deletions packages/components/select/__tests__/select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,42 @@ describe("Select", () => {

expect(trigger).toHaveTextContent(labelContent);
});

it("should support controlled isInvalid prop", async () => {
function Test() {
const [isInvalid, setInvalid] = React.useState(false);

return (
<>
<Select
data-testid="select"
errorMessage="Invalid value"
isInvalid={isInvalid}
label="Test"
name="select"
>
<SelectItem key="one">One</SelectItem>
<SelectItem key="two">Two</SelectItem>
<SelectItem key="three">Three</SelectItem>
</Select>
<button data-testid="button" onClick={() => setInvalid((isInvalid) => !isInvalid)}>
Click Me
</button>
</>
);
}

const {getByTestId} = render(<Test />);
const select = getByTestId("select");

expect(select).not.toHaveAttribute("aria-describedby");

await user.click(getByTestId("button"));
expect(select).toHaveAttribute("aria-describedby");
expect(document.getElementById(select.getAttribute("aria-describedby"))).toHaveTextContent(
"Invalid value",
);
});
});

describe("Select with React Hook Form", () => {
Expand Down
1 change: 1 addition & 0 deletions packages/components/select/src/use-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ export function useSelect<T extends object>(originalProps: UseSelectProps<T>) {
children: children as CollectionChildren<T>,
isRequired: originalProps.isRequired,
isDisabled: originalProps.isDisabled,
isInvalid: originalProps.isInvalid,
defaultOpen,
onOpenChange: (open) => {
onOpenChange?.(open);
Expand Down

0 comments on commit 1e05a72

Please sign in to comment.