Skip to content
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/curly-zoos-thank.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/listbox": patch
---

change ListboxItem key to optional (#3880)
2 changes: 1 addition & 1 deletion packages/components/listbox/src/base/listbox-item-base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ interface Props<T extends object = {}> extends Omit<ItemProps<"li", T>, "childre

export type ListboxItemBaseProps<T extends object = {}> = Props<T> &
ListboxItemVariantProps &
AriaOptionProps &
Omit<AriaOptionProps, "key"> &
FocusableProps &
PressEvents;

Expand Down
66 changes: 58 additions & 8 deletions packages/components/select/__tests__/select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,61 @@ describe("Select", () => {
});
});

it("should pre-select items based on defaultSelectedKeys (numeric keys)", () => {
const items = [
{key: 1, value: "Penguin"},
{key: 2, value: "Zebra"},
{key: 3, value: "Shark"},
];

const wrapper = render(
<Select
isOpen
defaultSelectedKeys={[1, 2]} // Numeric keys for selection
items={items}
label="Test Default Selected Keys"
selectionMode="multiple"
>
{(item) => <SelectItem>{item.value}</SelectItem>}
Comment thread
ryo-manba marked this conversation as resolved.
</Select>,
);

const selectedOptions = wrapper.getAllByRole("option", {selected: true});

expect(selectedOptions.length).toBe(2);
expect(selectedOptions.map((opt) => opt.textContent)).toEqual(["Penguin", "Zebra"]);
});

it("should pre-select items based on defaultSelectedKeys (numeric ids)", () => {
const items = [
{id: 1, value: "Penguin"},
{id: 2, value: "Zebra"},
{id: 3, value: "Shark"},
];

const wrapper = render(
<Select
isOpen
defaultSelectedKeys={[1, 2]} // Numeric ids for selection
items={items}
label="Test Default Selected IDs"
selectionMode="multiple"
>
{(item) => <SelectItem>{item.value}</SelectItem>}
</Select>,
);

const selectedOptions = wrapper.getAllByRole("option", {selected: true});

expect(selectedOptions.length).toBe(2);
expect(selectedOptions.map((opt) => opt.textContent)).toEqual(["Penguin", "Zebra"]);
});

it("onSelectionChange should be called with a Set of item ids upon selection", async () => {
const itemsWithId = [
{id: "1", value: "penguin"},
{id: "2", value: "zebra"},
{id: "3", value: "shark"},
{id: 1, value: "penguin"},
{id: 2, value: "zebra"},
{id: 3, value: "shark"},
];

const onSelectionChangeId = jest.fn();
Expand All @@ -365,7 +415,7 @@ describe("Select", () => {
label="Test with ID"
onSelectionChange={onSelectionChangeId}
>
{(item) => <SelectItem key={item.id}>{item.value}</SelectItem>}
{(item) => <SelectItem>{item.value}</SelectItem>}
</Select>,
);

Expand All @@ -392,9 +442,9 @@ describe("Select", () => {

it("onSelectionChange should be called with a Set of item keys upon selection", async () => {
const itemsWithKey = [
{key: "1", value: "penguin"},
{key: "2", value: "zebra"},
{key: "3", value: "shark"},
{key: 1, value: "penguin"},
{key: 2, value: "zebra"},
{key: 3, value: "shark"},
];

const onSelectionChangeKey = jest.fn();
Expand All @@ -405,7 +455,7 @@ describe("Select", () => {
label="Test with Key"
onSelectionChange={onSelectionChangeKey}
>
{(item) => <SelectItem key={item.key}>{item.value}</SelectItem>}
{(item) => <SelectItem>{item.value}</SelectItem>}
</Select>,
);

Expand Down