diff --git a/src/select/select.tsx b/src/select/select.tsx index 7ee68a180..f533b1c74 100644 --- a/src/select/select.tsx +++ b/src/select/select.tsx @@ -428,6 +428,7 @@ export default defineComponent({ count += 1; if (count >= displayOptionsLength) break; } + setHoverIntoView(); }; const arrowUpOption = () => { let count = 0; @@ -443,6 +444,25 @@ export default defineComponent({ count += 1; if (count >= displayOptionsLength) break; } + setHoverIntoView(); + }; + /** 让hover元素滚动到下拉面板视口 */ + const setHoverIntoView = () => { + nextTick(() => { + const hoverDom = selectPanelRef.value?.$el.querySelector( + `li.${classPrefix.value}-select-option.${classPrefix.value}-select-option__hover`, + ) as HTMLElement | null; + if (hoverDom) { + const container = selectPanelRef.value.$el.parentNode as HTMLElement; + const containerRect = container.getBoundingClientRect(); + const hoverDomRect = hoverDom.getBoundingClientRect(); + const offsetTop = hoverDomRect.top - containerRect.top + container.scrollTop; + container.scrollTo({ + top: offsetTop - (container.offsetHeight - hoverDom.offsetHeight * 2), + behavior: 'smooth', + }); + } + }); }; if (displayOptionsLength === 0) return; const preventKeys = ['ArrowDown', 'ArrowUp', 'Enter', 'Escape', 'Tab']; diff --git a/src/select/util.ts b/src/select/util.ts index fa16ea63a..229587337 100644 --- a/src/select/util.ts +++ b/src/select/util.ts @@ -42,7 +42,9 @@ export const getNewMultipleValue = (innerValue: SelectValue[], optionValue: Sele export const getAllSelectableOption = (options: TdOptionProps[]) => options.filter((option) => !option.disabled && !option.checkAll); /** 将 options 扁平化,拍扁所有 group */ -export const flattenOptions = (options: (TdOptionProps & { isCreated?: boolean })[]): SelectOption[] => options.reduce((acc, current) => { - acc.push((current as SelectOptionGroup).group ? flattenOptions((current as SelectOptionGroup).children) : current); - return acc; -}, [] as SelectOption[]); +export const flattenOptions = (options: (TdOptionProps & { isCreated?: boolean })[]): SelectOption[] => options.reduce( + (acc, current) => acc.concat( + (current as SelectOptionGroup).group ? flattenOptions((current as SelectOptionGroup).children) : [current], + ), + [] as SelectOption[], +);