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(select): placeholder text display for controlled component #3081

Merged
merged 2 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/proud-baboons-cough.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/select": patch
---

Fix: display placeholder text when unselected for controlled (#3062)
41 changes: 41 additions & 0 deletions packages/components/select/__tests__/select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/components/select/src/select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => ({
Expand Down
Loading