-
Notifications
You must be signed in to change notification settings - Fork 359
feat(Cascader): support virtual scroll api #3901
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
base: develop
Are you sure you want to change the base?
Changes from all commits
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 | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,50 @@ | ||||||||||||||||||||||
| import React, { useState } from 'react'; | ||||||||||||||||||||||
| import { Cascader } from 'tdesign-react'; | ||||||||||||||||||||||
| import type { CascaderProps, CascaderValue } from 'tdesign-react'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const list = []; | ||||||||||||||||||||||
| for (let i = 1; i < 100; i++) { | ||||||||||||||||||||||
| const children = []; | ||||||||||||||||||||||
| for (let j = 1; j < 100; j++) { | ||||||||||||||||||||||
| const child = []; | ||||||||||||||||||||||
| for (let k = 1; k < 100; k++) { | ||||||||||||||||||||||
|
Comment on lines
+6
to
+10
|
||||||||||||||||||||||
| for (let i = 1; i < 100; i++) { | |
| const children = []; | |
| for (let j = 1; j < 100; j++) { | |
| const child = []; | |
| for (let k = 1; k < 100; k++) { | |
| for (let i = 1; i < 30; i++) { | |
| const children = []; | |
| for (let j = 1; j < 30; j++) { | |
| const child = []; | |
| for (let k = 1; k < 30; k++) { |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,124 @@ | ||||||
| import React, { useEffect, useRef } from 'react'; | ||||||
| import classNames from 'classnames'; | ||||||
| import { TreeNode } from '../interface'; | ||||||
| import useConfig from '../../hooks/useConfig'; | ||||||
| import Item from './Item'; | ||||||
| import parseTNode from '../../_util/parseTNode'; | ||||||
| import { expendClickEffect, valueChangeEffect } from '../core/effect'; | ||||||
| import { useListVirtualScroll } from '../../list/hooks/useListVirtualScroll'; | ||||||
| import useEventCallback from '../../hooks/useEventCallback'; | ||||||
| import { usePanelContext } from '../context'; | ||||||
|
|
||||||
| interface PanelList { | ||||||
| treeNodes: TreeNode[]; | ||||||
| isFilter: boolean; | ||||||
| segment?: boolean; | ||||||
| listKey?: string; | ||||||
| level?: number; | ||||||
| } | ||||||
|
|
||||||
| const List = (props: PanelList) => { | ||||||
| const { treeNodes, isFilter = false, segment = true, listKey: key, level = 0 } = props; | ||||||
| const ctx = usePanelContext(); | ||||||
| const panelWrapperRef = useRef<HTMLDivElement>(null); | ||||||
| const { classPrefix } = useConfig(); | ||||||
| const COMPONENT_NAME = `${classPrefix}-cascader`; | ||||||
|
|
||||||
| const { virtualConfig, cursorStyle, listStyle, isVirtualScroll, onInnerVirtualScroll, scrollToElement } = | ||||||
| useListVirtualScroll(ctx.scroll, panelWrapperRef, treeNodes); | ||||||
|
Comment on lines
+21
to
+28
|
||||||
|
|
||||||
| const onScrollIntoView = useEventCallback(() => { | ||||||
| const checkedNodes = ctx.cascaderContext.treeStore.getCheckedNodes(); | ||||||
| let lastCheckedNodes = checkedNodes[checkedNodes.length - 1]; | ||||||
| let index = -1; | ||||||
| if (lastCheckedNodes?.level === level) { | ||||||
| index = treeNodes.findLastIndex((item) => item.value === lastCheckedNodes.value); | ||||||
| } else { | ||||||
| while (lastCheckedNodes) { | ||||||
| if (lastCheckedNodes?.level === level) { | ||||||
| // eslint-disable-next-line no-loop-func | ||||||
| index = treeNodes.findIndex((item) => item.value === lastCheckedNodes.value); | ||||||
| break; | ||||||
|
Comment on lines
+30
to
+41
|
||||||
| } | ||||||
| lastCheckedNodes = lastCheckedNodes?.parent; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (index !== -1) { | ||||||
| scrollToElement({ | ||||||
| index, | ||||||
| }); | ||||||
| } | ||||||
| }); | ||||||
|
|
||||||
| const handleExpand = (node: TreeNode, trigger: 'hover' | 'click') => { | ||||||
| expendClickEffect(ctx.trigger, trigger, node, ctx.cascaderContext); | ||||||
| }; | ||||||
|
|
||||||
| const renderItem = (node: TreeNode, index: number) => ( | ||||||
| <Item | ||||||
| ref={(el) => { | ||||||
| if (el) { | ||||||
| virtualConfig.handleRowMounted({ | ||||||
| ref: el, | ||||||
| data: node, | ||||||
| }); | ||||||
| } | ||||||
| }} | ||||||
| key={index} | ||||||
| node={node} | ||||||
| optionChild={node.data.content || parseTNode(ctx.option, { item: node.data, index, context: { node } })} | ||||||
| cascaderContext={ctx.cascaderContext} | ||||||
| onClick={() => { | ||||||
| handleExpand(node, 'click'); | ||||||
| }} | ||||||
| onMouseEnter={() => { | ||||||
| handleExpand(node, 'hover'); | ||||||
| }} | ||||||
| onChange={() => { | ||||||
| valueChangeEffect(node, ctx.cascaderContext); | ||||||
| }} | ||||||
| /> | ||||||
| ); | ||||||
|
|
||||||
| const handleScroll = (event: React.WheelEvent<HTMLDivElement>): void => { | ||||||
| if (isVirtualScroll) onInnerVirtualScroll(event as unknown as globalThis.WheelEvent); | ||||||
| }; | ||||||
|
Comment on lines
+84
to
+86
|
||||||
|
|
||||||
| useEffect(() => { | ||||||
| if (ctx.scroll && ctx.cascaderContext.visible) { | ||||||
| const timer = setTimeout(onScrollIntoView, 16); | ||||||
|
|
||||||
| return () => { | ||||||
| clearTimeout(timer); | ||||||
| }; | ||||||
| } | ||||||
| }, [onScrollIntoView, ctx.scroll, ctx.cascaderContext.visible]); | ||||||
|
|
||||||
| return ( | ||||||
| <div | ||||||
| ref={panelWrapperRef} | ||||||
| onScroll={handleScroll} | ||||||
|
||||||
| onScroll={handleScroll} | |
| onWheel={handleScroll} |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,12 @@ | ||||||||||||||||||||||||||||||
| import { createContext, useContext } from 'react'; | ||||||||||||||||||||||||||||||
| import { CascaderContextType } from './interface'; | ||||||||||||||||||||||||||||||
| import { CascaderPanelProps } from './components/Panel'; | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export const PanelContext = createContext<{ | ||||||||||||||||||||||||||||||
| cascaderContext: CascaderContextType; | ||||||||||||||||||||||||||||||
| trigger: CascaderPanelProps['trigger']; | ||||||||||||||||||||||||||||||
| option: CascaderPanelProps['option']; | ||||||||||||||||||||||||||||||
| scroll: CascaderPanelProps['scroll']; | ||||||||||||||||||||||||||||||
|
Comment on lines
+3
to
+9
|
||||||||||||||||||||||||||||||
| import { CascaderPanelProps } from './components/Panel'; | |
| export const PanelContext = createContext<{ | |
| cascaderContext: CascaderContextType; | |
| trigger: CascaderPanelProps['trigger']; | |
| option: CascaderPanelProps['option']; | |
| scroll: CascaderPanelProps['scroll']; | |
| import type { TdCascaderProps } from './type'; | |
| export const PanelContext = createContext<{ | |
| cascaderContext: CascaderContextType; | |
| trigger: TdCascaderProps['trigger']; | |
| option: TdCascaderProps['option']; | |
| scroll: TdCascaderProps['scroll']; |
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.
This disables updateScrollTop for any scroll config, but the intent appears to be only when using virtual scrolling; if scroll also configures lazy loading, this change will unintentionally suppress the alignment behavior. Check the scroll type instead, e.g., if (props.scroll?.type === 'virtual') return;.