Skip to content

Commit

Permalink
feat(tree): add Tree onScroll api (#3295)
Browse files Browse the repository at this point in the history
* feat(tree): add Tree onScroll api

* chore(tree): adjustment onScroll

* perf(tree): code clean

* perf(tree): code perf
  • Loading branch information
HaixingOoO authored Dec 23, 2024
1 parent a2e730d commit fffe54a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/tree/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const Tree = forwardRef<TreeInstanceFunctions<TreeOptionData>, TreeProps>((origi
className,
style,
allowDrop,
onScroll,
} = props;

const { value, onChange, expanded, onExpand, onActive, actived, setTreeIndeterminate, indeterminate } =
Expand Down Expand Up @@ -135,6 +136,7 @@ const Tree = forwardRef<TreeInstanceFunctions<TreeOptionData>, TreeProps>((origi
treeRef,
scroll,
data: visibleNodes,
onScroll,
});

const setActived = usePersistFn(
Expand Down
21 changes: 11 additions & 10 deletions src/tree/hooks/useTreeVirtualScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import { useMemo, useEffect, CSSProperties } from 'react';
import useVirtualScroll from '../../hooks/useVirtualScroll';
import TreeNode from '../../_common/js/tree-v1/tree-node';
import { TScroll } from '../../common';
import type { TdTreeProps } from '../type';
import useEventCallback from '../../hooks/useEventCallback';

export default function useTreeVirtualScroll({
treeRef,
scroll,
data = [],
onScroll,
}: {
data: TreeNode[];
scroll: TScroll;
treeRef: React.MutableRefObject<HTMLElement>;
onScroll: TdTreeProps['onScroll'];
}) {
const scrollThreshold = scroll?.threshold || 100;
const scrollType = scroll?.type;
Expand Down Expand Up @@ -45,7 +49,8 @@ export default function useTreeVirtualScroll({
});

let lastScrollY = -1;
const onInnerVirtualScroll = (e: WheelEvent) => {
const onInnerVirtualScroll = useEventCallback((e: WheelEvent) => {
onScroll?.({ e });
if (!isVirtual) {
return;
}
Expand All @@ -58,21 +63,17 @@ export default function useTreeVirtualScroll({
lastScrollY = -1;
}
lastScrollY = top;
};
});

useEffect(() => {
const treeList = treeRef?.current;
if (isVirtual) {
treeList?.addEventListener?.('scroll', onInnerVirtualScroll);
}
treeList?.addEventListener?.('scroll', onInnerVirtualScroll);

return () => {
// 卸载时取消监听
if (isVirtual) {
treeList?.removeEventListener?.('scroll', onInnerVirtualScroll);
}
treeList?.removeEventListener?.('scroll', onInnerVirtualScroll);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isVirtual, onInnerVirtualScroll]);
}, [treeRef, onInnerVirtualScroll]);

const cursorStyle = {
position: 'absolute',
Expand Down
4 changes: 2 additions & 2 deletions src/tree/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { CheckboxProps } from '../checkbox';
import { TNode, TreeOptionData, TScroll, ComponentScrollToElementParams } from '../common';
import { MouseEvent, WheelEvent, DragEvent } from 'react';
import { MouseEvent, DragEvent } from 'react';

export interface TdTreeProps<T extends TreeOptionData = TreeOptionData> {
/**
Expand Down Expand Up @@ -238,7 +238,7 @@ export interface TdTreeProps<T extends TreeOptionData = TreeOptionData> {
/**
* 滚动事件
*/
onScroll?: (params: { e: WheelEvent<HTMLDivElement> }) => void;
onScroll?: (params: { e: WheelEvent }) => void;
}

/** 组件实例方法 */
Expand Down

0 comments on commit fffe54a

Please sign in to comment.