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(combobx): display selected item when initally opened #11427

Merged
merged 3 commits into from
Feb 1, 2025
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 @@ -2571,6 +2571,51 @@ describe("calcite-combobox", () => {
</calcite-combobox>`,
"item3",
));

it("shows the selected item when initially opened", async () => {
const page = await newE2EPage();

await page.setContent(`
Copy link
Member

Choose a reason for hiding this comment

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

Can you follow this up by:

  • using the html formatting helper (example)
  • Removing any attributes not needed for the test – id doesn't seem to be used and optionally placeholder since the test name and assertion convey this
  • adding a test for the multiple selection case (one should suffice) for extra coverage
  • renaming item1 to something that matches the test

?

<calcite-combobox
open
id="labelOne"
label="test"
placeholder="selected element (Black Eyed Susan) should be in view"
max-items="6"
selection-mode="single"
>
<calcite-combobox-item value="Trees" text-label="Trees">
<calcite-combobox-item value="Pine" text-label="Pine">
<calcite-combobox-item value="Pine Nested" text-label="Pine Nested"></calcite-combobox-item>
</calcite-combobox-item>
<calcite-combobox-item value="Sequoia" disabled text-label="Sequoia"></calcite-combobox-item>
<calcite-combobox-item value="Douglas Fir" text-label="Douglas Fir"></calcite-combobox-item>
</calcite-combobox-item>
<calcite-combobox-item value="Flowers" text-label="Flowers">
<calcite-combobox-item value="Daffodil" text-label="Daffodil"></calcite-combobox-item>
<calcite-combobox-item
value="Black Eyed Susan"
text-label="Black Eyed Susan"
selected
></calcite-combobox-item>
<calcite-combobox-item value="Nasturtium" text-label="Nasturtium"></calcite-combobox-item>
</calcite-combobox-item>
<calcite-combobox-item value="Animals" text-label="Animals">
<calcite-combobox-item value="Birds" text-label="Birds"></calcite-combobox-item>
<calcite-combobox-item value="Reptiles" text-label="Reptiles"></calcite-combobox-item>
<calcite-combobox-item value="Amphibians" text-label="Amphibians"></calcite-combobox-item>
</calcite-combobox-item>
<calcite-combobox-item value="Rocks" text-label="Rocks"></calcite-combobox-item>
<calcite-combobox-item value="Insects" text-label="Insects"></calcite-combobox-item>
<calcite-combobox-item value="Rivers" text-label="Rivers"></calcite-combobox-item>
</calcite-combobox>
`);
await page.waitForChanges();
const combobox = await page.find("calcite-combobox");
const item1 = await combobox.find("calcite-combobox-item[value='Black Eyed Susan']");

expect(await item1.isIntersectingViewport()).toBeTruthy();
});
});

describe("multiple-selection", () => {
Expand Down
26 changes: 12 additions & 14 deletions packages/calcite-components/src/components/combobox/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ export class Combobox
}
event.preventDefault();
this.updateActiveItemIndex(0);
this.scrollToActiveItem();
this.scrollToActiveOrSelectedItem();
if (!this.comboboxInViewport()) {
this.el.scrollIntoView();
}
Expand All @@ -850,7 +850,7 @@ export class Combobox
}
event.preventDefault();
this.updateActiveItemIndex(this.filteredItems.length - 1);
this.scrollToActiveItem();
this.scrollToActiveOrSelectedItem();
if (!this.comboboxInViewport()) {
this.el.scrollIntoView();
}
Expand Down Expand Up @@ -901,11 +901,12 @@ export class Combobox
}

onBeforeOpen(): void {
this.scrollToActiveItem();
this.scrollToActiveOrSelectedItem();
this.calciteComboboxBeforeOpen.emit();
}

onOpen(): void {
this.scrollToActiveOrSelectedItem(true);
this.calciteComboboxOpen.emit();
}

Expand Down Expand Up @@ -1366,27 +1367,24 @@ export class Combobox
chip?.setFocus();
}

private scrollToActiveItem(): void {
const activeItem = this.filteredItems[this.activeItemIndex];
private scrollToActiveOrSelectedItem(scrollToSelected = false): void {
const item =
Copy link
Member

@jcfranco jcfranco Feb 1, 2025

Choose a reason for hiding this comment

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

FYI, you can use the optional chaining operator to simplify these types of checks:

scrollToSelected && this.selectedItems?.length

We might be able to automate this with https://typescript-eslint.io/rules/prefer-optional-chain/.

scrollToSelected && this.selectedItems && this.selectedItems.length
? this.selectedItems[0]
: this.filteredItems[this.activeItemIndex];

if (!activeItem) {
if (!item) {
return;
}

const height = this.calculateScrollerHeight(activeItem);
const { offsetHeight, scrollTop } = this.listContainerEl;
if (offsetHeight + scrollTop < activeItem.offsetTop + height) {
this.listContainerEl.scrollTop = activeItem.offsetTop - offsetHeight + height;
} else if (activeItem.offsetTop < scrollTop) {
this.listContainerEl.scrollTop = activeItem.offsetTop;
}
item.scrollIntoView({ block: "nearest" });
}

private shiftActiveItemIndex(delta: number): void {
const { length } = this.filteredItems;
const newIndex = (this.activeItemIndex + length + delta) % length;
this.updateActiveItemIndex(newIndex);
this.scrollToActiveItem();
this.scrollToActiveOrSelectedItem();
}

private updateActiveItemIndex(index: number): void {
Expand Down
Loading