-
Notifications
You must be signed in to change notification settings - Fork 77
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -840,7 +840,7 @@ export class Combobox | |
} | ||
event.preventDefault(); | ||
this.updateActiveItemIndex(0); | ||
this.scrollToActiveItem(); | ||
this.scrollToActiveOrSelectedItem(); | ||
if (!this.comboboxInViewport()) { | ||
this.el.scrollIntoView(); | ||
} | ||
|
@@ -851,7 +851,7 @@ export class Combobox | |
} | ||
event.preventDefault(); | ||
this.updateActiveItemIndex(this.filteredItems.length - 1); | ||
this.scrollToActiveItem(); | ||
this.scrollToActiveOrSelectedItem(); | ||
if (!this.comboboxInViewport()) { | ||
this.el.scrollIntoView(); | ||
} | ||
|
@@ -902,11 +902,12 @@ export class Combobox | |
} | ||
|
||
onBeforeOpen(): void { | ||
this.scrollToActiveItem(); | ||
this.scrollToActiveOrSelectedItem(); | ||
this.calciteComboboxBeforeOpen.emit(); | ||
} | ||
|
||
onOpen(): void { | ||
this.scrollToActiveOrSelectedItem(true); | ||
this.calciteComboboxOpen.emit(); | ||
} | ||
|
||
|
@@ -1367,27 +1368,30 @@ export class Combobox | |
chip?.setFocus(); | ||
} | ||
|
||
private scrollToActiveItem(): void { | ||
const activeItem = this.filteredItems[this.activeItemIndex]; | ||
private scrollToActiveOrSelectedItem(selected = false): void { | ||
const item = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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/. |
||
selected && this.selectedItems && this.selectedItems.length | ||
? this.selectedItems[0] | ||
: this.filteredItems[this.activeItemIndex]; | ||
|
||
if (!activeItem) { | ||
if (!item) { | ||
return; | ||
} | ||
|
||
const height = this.calculateScrollerHeight(activeItem); | ||
const height = this.calculateScrollerHeight(item); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @josercarcamo instead of calculating the height, can you try the following: item.scrollIntoView({ block: "nearest" }); I'm not sure calculating the offset is necessary. |
||
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; | ||
if (offsetHeight + scrollTop < item.offsetTop + height) { | ||
this.listContainerEl.scrollTop = item.offsetTop - offsetHeight + height; | ||
} else if (item.offsetTop < scrollTop) { | ||
this.listContainerEl.scrollTop = item.offsetTop; | ||
} | ||
} | ||
|
||
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 { | ||
|
There was a problem hiding this comment.
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:
html
formatting helper (example)id
doesn't seem to be used and optionallyplaceholder
since the test name and assertion convey thisitem1
to something that matches the test?