diff --git a/packages/inula-dev-tools/src/components/VList/ItemMap.ts b/packages/inula-dev-tools/src/components/VList/ItemMap.ts index 81109df4..e4c43bc3 100644 --- a/packages/inula-dev-tools/src/components/VList/ItemMap.ts +++ b/packages/inula-dev-tools/src/components/VList/ItemMap.ts @@ -20,7 +20,7 @@ export default class ItemMap { // 不要用 indexOf 进行位置计算,它会遍历数组 - private lastRenderItemToIndexMap: Map; + private lastRenderItemToIndexMap: Map; constructor() { this.lastRenderItemToIndexMap = new Map(); @@ -34,10 +34,10 @@ export default class ItemMap { return nextItems; } - const nextRenderItems: T[] = []; + const nextRenderItems: (T | undefined)[] = []; const length = nextItems.length; - const nextRenderItemToIndexMap = new Map(); - const addItems = []; + const nextRenderItemToIndexMap = new Map(); + const addItems: T[] = []; // 遍历 nextItems 找到复用 item 和新增 item nextItems.forEach(item => { diff --git a/packages/inula-dev-tools/src/components/VList/VList.tsx b/packages/inula-dev-tools/src/components/VList/VList.tsx index 86c148c3..9efbbf89 100644 --- a/packages/inula-dev-tools/src/components/VList/VList.tsx +++ b/packages/inula-dev-tools/src/components/VList/VList.tsx @@ -48,7 +48,7 @@ function parseTranslate(data: T[], itemHeight: number) { export function VList(props: IProps) { const { data, maxDeep, height, width, children, itemHeight, scrollToItem, onRendered } = props; - const [scrollTop, setScrollTop] = useState(Math.max(data.indexOf(scrollToItem), 0) * itemHeight); + const [scrollTop, setScrollTop] = useState(Math.max(data.indexOf(scrollToItem!), 0) * itemHeight); const renderInfoRef: { current: RenderInfoType } = useRef({ visibleItems: [], }); @@ -75,7 +75,7 @@ export function VList(props: IProps) { const index = data.indexOf(scrollToItem); // 显示在页面中间 const top = Math.max(index * itemHeight - height / 2, 0); - containerRef.current.scrollTo({ top: top }); + containerRef.current?.scrollTo({ top: top }); } } }, [scrollToItem]); @@ -87,9 +87,9 @@ export function VList(props: IProps) { setScrollTop(scrollTop); }; const container = containerRef.current; - container.addEventListener('scroll', handleScroll); + container?.addEventListener('scroll', handleScroll); return () => { - container.removeEventListener('scroll', handleScroll); + container?.removeEventListener('scroll', handleScroll); }; }, []); @@ -117,7 +117,7 @@ export function VList(props: IProps) { } return (
diff --git a/packages/inula-dev-tools/src/components/VList/index.ts b/packages/inula-dev-tools/src/components/VList/index.ts new file mode 100644 index 00000000..944e8eb8 --- /dev/null +++ b/packages/inula-dev-tools/src/components/VList/index.ts @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2023 Huawei Technologies Co.,Ltd. + * + * openInula is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * + * http://license.coscl.org.cn/MulanPSL2 + * + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +export { VList } from './VList'; +export type { RenderInfoType } from './VList';