fix(select): placeholder text display for controlled component (#3081)

* fix: return placeholder when selectedItems is empty

* chore: add test and changeset
This commit is contained in:
Ryo Matsukawa 2024-05-27 05:07:36 +09:00 committed by GitHub
parent fa26ce02fd
commit 31bfaebe2c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
"@nextui-org/select": patch
---
Fix: display placeholder text when unselected for controlled (#3062)

View File

@ -506,6 +506,47 @@ describe("Select", () => {
// assert that the second select listbox is open // assert that the second select listbox is open
expect(select2).toHaveAttribute("aria-expanded", "true"); expect(select2).toHaveAttribute("aria-expanded", "true");
}); });
it("should display placeholder text when unselected", async () => {
const wrapper = render(
<Select
aria-label="Favorite Animal"
data-testid="test-select"
label="Favorite Animal"
placeholder="Select an animal"
>
<SelectItem key="penguin">Penguin</SelectItem>
<SelectItem key="zebra">Zebra</SelectItem>
<SelectItem key="shark">Shark</SelectItem>
</Select>,
);
const select = wrapper.getByTestId("test-select");
expect(select).toHaveTextContent("Select an animal");
});
it("should display placeholder text when unselected (controlled)", async () => {
const onSelectionChange = jest.fn();
const wrapper = render(
<Select
isOpen
aria-label="Favorite Animal"
data-testid="test-select"
placeholder="Select an animal"
selectedKeys={[]}
onSelectionChange={onSelectionChange}
>
<SelectItem key="penguin">Penguin</SelectItem>
<SelectItem key="zebra">Zebra</SelectItem>
<SelectItem key="shark">Shark</SelectItem>
</Select>,
);
const select = wrapper.getByTestId("test-select");
expect(select).toHaveTextContent("Select an animal");
});
}); });
describe("Select with React Hook Form", () => { describe("Select with React Hook Form", () => {

View File

@ -75,7 +75,7 @@ function Select<T extends object>(props: Props<T>, ref: ForwardedRef<HTMLSelectE
]); ]);
const renderSelectedItem = useMemo(() => { const renderSelectedItem = useMemo(() => {
if (!state.selectedItems) return placeholder; if (!state.selectedItems?.length) return placeholder;
if (renderValue && typeof renderValue === "function") { if (renderValue && typeof renderValue === "function") {
const mappedItems = [...state.selectedItems].map((item) => ({ const mappedItems = [...state.selectedItems].map((item) => ({