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(listbox): change listBoxItem key to optional #3883

Merged
merged 3 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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>}
ryo-manba marked this conversation as resolved.
Show resolved Hide 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