Skip to content

Commit

Permalink
fix(combobox): Fix error when typing a custom value (#6071)
Browse files Browse the repository at this point in the history
**Related Issue:** #5109

## Summary

fix(combobox): Fix error when typing a custom value and then trying to
open the menu. #5109
  • Loading branch information
driskull authored Dec 16, 2022
1 parent c058ea8 commit 246de97
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/components/combobox/combobox.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,20 @@ describe("calcite-combobox", () => {
expect(floatingUI).toBeNull();
});

it("should not throw when typing custom value and pressing ArrowDown", async () => {
const combobox = await page.find("calcite-combobox");
combobox.setProperty("allowCustomValues", true);
await page.waitForChanges();
const inputEl = await page.find(`#myCombobox >>> input`);
await inputEl.focus();
await page.waitForChanges();
expect(await page.evaluate(() => document.activeElement.id)).toBe("myCombobox");
await page.keyboard.type("asdf");
await page.waitForChanges();
await page.keyboard.press("ArrowDown");
await page.waitForChanges();
});

it(`ArrowDown opens the item group for combobox in focus and jumps to the first item`, async () => {
const inputEl = await page.find(`#myCombobox >>> input`);
await inputEl.focus();
Expand Down
9 changes: 9 additions & 0 deletions src/components/combobox/combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,10 @@ export class Combobox
}

private calculateSingleItemHeight(item: ComboboxChildElement): number {
if (!item) {
return;
}

let height = item.offsetHeight;
// if item has children items, don't count their height twice
const children = Array.from(item.querySelectorAll<ComboboxChildElement>(ComboboxChildSelector));
Expand Down Expand Up @@ -1003,6 +1007,11 @@ export class Combobox

private scrollToActiveItem = (): void => {
const activeItem = this.filteredItems[this.activeItemIndex];

if (!activeItem) {
return;
}

const height = this.calculateSingleItemHeight(activeItem);
const { offsetHeight, scrollTop } = this.listContainerEl;
if (offsetHeight + scrollTop < activeItem.offsetTop + height) {
Expand Down

0 comments on commit 246de97

Please sign in to comment.