mirror of
https://github.com/nextui-org/nextui.git
synced 2025-12-08 19:26:11 +00:00
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:
parent
fa26ce02fd
commit
31bfaebe2c
5
.changeset/proud-baboons-cough.md
Normal file
5
.changeset/proud-baboons-cough.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
"@nextui-org/select": patch
|
||||
---
|
||||
|
||||
Fix: display placeholder text when unselected for controlled (#3062)
|
||||
@ -506,6 +506,47 @@ describe("Select", () => {
|
||||
// assert that the second select listbox is open
|
||||
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", () => {
|
||||
|
||||
@ -75,7 +75,7 @@ function Select<T extends object>(props: Props<T>, ref: ForwardedRef<HTMLSelectE
|
||||
]);
|
||||
|
||||
const renderSelectedItem = useMemo(() => {
|
||||
if (!state.selectedItems) return placeholder;
|
||||
if (!state.selectedItems?.length) return placeholder;
|
||||
|
||||
if (renderValue && typeof renderValue === "function") {
|
||||
const mappedItems = [...state.selectedItems].map((item) => ({
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user