diff --git a/src/indexes/indexes.tsx b/src/indexes/indexes.tsx index 497a22921..01de84713 100644 --- a/src/indexes/indexes.tsx +++ b/src/indexes/indexes.tsx @@ -7,8 +7,8 @@ import { onBeforeUnmount, provide, computed, - ComponentInternalInstance, watch, + ComponentPublicInstance, } from 'vue'; import throttle from 'lodash/throttle'; import { preventDefault } from '../shared/dom'; @@ -16,21 +16,18 @@ import config from '../config'; import IndexesProps from './props'; import { usePrefixClass } from '../hooks/useClass'; import { useTNodeJSX } from '../hooks/tnode'; - -interface Child extends ComponentInternalInstance { - [key: string]: any; -} +import { TdIndexesAnchorProps } from './type'; interface State { showSidebarTip: boolean; activeSidebar: string | number; - children: Child[]; + children: ComponentPublicInstance[]; } interface GroupTop { height: number; top: number; - anchor: string; + anchor: string | number; totalHeight: number; } @@ -146,14 +143,12 @@ export default defineComponent({ const getAnchorsRect = () => { return Promise.all( state.children.map((child) => { - const { $el } = child; - const { dataset } = $el; - const { index } = dataset; + const { $el, index } = child; const rect = $el.getBoundingClientRect(); groupTop.push({ height: rect.height, top: rect.top - parentRect.value.top, - anchor: /^\d+$/.test(index) ? Number(index) : index, + anchor: index, totalHeight: 0, }); return child; @@ -168,7 +163,7 @@ export default defineComponent({ const target = document.elementFromPoint(clientX, clientY); if (target && target.className === `${indexesClass.value}__sidebar-item` && target instanceof HTMLElement) { const { index } = target.dataset; - const curIndex = /^\d+$/.test(index ?? '') ? Number(index) : index; + const curIndex = indexList.value.find((idx) => String(idx) === index); if (curIndex !== undefined && state.activeSidebar !== curIndex) { setActiveSidebarAndTip(curIndex); scrollToByIndex(curIndex); @@ -176,7 +171,7 @@ export default defineComponent({ } }; - const relation = (child: ComponentInternalInstance) => { + const relation = (child: ComponentPublicInstance) => { child && state.children.push(child); };