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(combobox): fix filtering to avoid showing unrelated items #7704

Merged
merged 1 commit into from
Sep 8, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@
::slotted(calcite-combobox-item-group:not([after-empty-group])) {
padding-block-start: var(--calcite-combobox-item-spacing-unit-l);
}

@include base-component();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch.

Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,53 @@ describe("calcite-combobox", () => {
disabled("calcite-combobox");
});

it("filter properly when items have duplicate values with parents", async () => {
const page = await newE2EPage();
await page.setContent(
html`
<calcite-combobox>
<calcite-combobox-item-group label="arcgis-app-identity">
<calcite-combobox-item
value="./html/arcgis-app-identity/index.html"
text-label="index.html"
></calcite-combobox-item>
</calcite-combobox-item-group>
<calcite-combobox-item-group label="arcgis-configuration-editor">
<calcite-combobox-item
value="./html/arcgis-configuration-editor/composite-field-editor.html"
text-label="composite-field-editor.html"
></calcite-combobox-item>
<calcite-combobox-item
value="./html/arcgis-configuration-editor/index.html"
text-label="index.html"
></calcite-combobox-item>
<calcite-combobox-item
value="./html/arcgis-configuration-editor/rule-editor.html"
text-label="rule-editor.html"
></calcite-combobox-item>
</calcite-combobox-item-group>
</calcite-combobox>
`
);

const combobox = await page.find("calcite-combobox");
await combobox.click();
await page.waitForChanges();
await combobox.type("conf");
await page.waitForChanges();

const items = await page.findAll("calcite-combobox-item");
const groups = await page.findAll("calcite-combobox-item-group");

expect(await groups[0].isVisible()).toBe(false);
expect(await items[0].isVisible()).toBe(false);

expect(await groups[1].isVisible()).toBe(true);
expect(await items[1].isVisible()).toBe(true);
expect(await items[2].isVisible()).toBe(true);
expect(await items[3].isVisible()).toBe(true);
});

it("filtering does not match property with value of undefined", async () => {
const page = await newE2EPage({
html: html`
Expand Down
26 changes: 10 additions & 16 deletions packages/calcite-components/src/components/combobox/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ export class Combobox
};

private getMaxScrollerHeight(): number {
const items = this.getCombinedItems().filter((item) => !item.hidden);
const items = this.getItemsAndGroups().filter((item) => !item.hidden);

const { maxItems } = this;

Expand Down Expand Up @@ -843,36 +843,30 @@ export class Combobox
}
};

getCombinedItems(): ComboboxChildElement[] {
getItemsAndGroups(): ComboboxChildElement[] {
return [...this.groupItems, ...this.items];
}

private filterItems = (() => {
const find = (item: ComboboxChildElement, filteredData: ItemData[]) =>
item &&
filteredData.some(({ label, value }) => {
if (isGroup(item)) {
return value === item.label;
}

return (
value === item.textLabel ||
value === item.value ||
label === item.textLabel ||
label === item.value
);
});
filteredData.some(({ label, value }) =>
isGroup(item) ? label === item.label : value === item.value && label === item.textLabel
);

return debounce((text: string): void => {
const filteredData = filter(this.data, text);
const items = this.getCombinedItems();
items.forEach((item) => {
const itemsAndGroups = this.getItemsAndGroups();

itemsAndGroups.forEach((item) => {
const hidden = !find(item, filteredData);
item.hidden = hidden;
const [parent, grandparent] = item.ancestors;

if (find(parent, filteredData) || find(grandparent, filteredData)) {
item.hidden = false;
}

if (!hidden) {
item.ancestors.forEach((ancestor) => (ancestor.hidden = false));
}
Expand Down
Loading