-
Notifications
You must be signed in to change notification settings - Fork 577
feat(cascader): support virtual scroll api #6117
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
Open
HaixingOoO
wants to merge
5
commits into
Tencent:develop
Choose a base branch
from
HaixingOoO:feat/cascader/virtual
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
029ac9c
feat(cascader): support virtual scroll api
HaixingOoO 5a069bd
chore(cascader): sync example
HaixingOoO ecdf392
fix(cascader): fix error
HaixingOoO dc71efb
fix(cascader): fix error
HaixingOoO db9ee56
chore(cascader): adjust virtual scroll default value
HaixingOoO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
packages/components/cascader/_example-ts/virtual-scroll.vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| <template> | ||
| <t-cascader | ||
| v-model="value" | ||
| :options="options" | ||
| :scroll="{ type: 'virtual', bufferSize: 5, threshold: 10 }" | ||
| clearable | ||
| multiple | ||
| @change="onChange" | ||
| /> | ||
| </template> | ||
|
|
||
| <script setup> | ||
| import { onMounted, ref } from 'vue'; | ||
|
|
||
| const options = ref([]); | ||
| const value = ref(['20.1.20']); | ||
|
|
||
| const onChange = (val, context) => { | ||
| console.log(val, context); | ||
| }; | ||
|
|
||
| function initOptions() { | ||
| 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++) { | ||
| child.push({ | ||
| label: `子选项${i}.${j}.${k}`, | ||
| value: `${i}.${j}.${k}`, | ||
| }); | ||
| } | ||
| children.push({ | ||
| label: `子选项${i}.${j}`, | ||
| value: `${i}.${j}`, | ||
| children: child, | ||
| }); | ||
| } | ||
|
|
||
| list.push({ | ||
| label: `选项${i}`, | ||
| value: `${i}`, | ||
| children, | ||
| }); | ||
| } | ||
| return list; | ||
| } | ||
|
|
||
| onMounted(() => { | ||
| const data = initOptions(); | ||
| options.value = data; | ||
| }); | ||
| </script> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| <template> | ||
| <t-cascader | ||
| v-model="value" | ||
| :options="options" | ||
| :scroll="{ type: 'virtual', bufferSize: 5, threshold: 10 }" | ||
| clearable | ||
| multiple | ||
| @change="onChange" | ||
| /> | ||
| </template> | ||
|
|
||
| <script setup> | ||
| import { onMounted, ref } from 'vue'; | ||
|
|
||
| const options = ref([]); | ||
| const value = ref(['20.1.20']); | ||
|
|
||
| const onChange = (val, context) => { | ||
| console.log(val, context); | ||
| }; | ||
|
|
||
| function initOptions() { | ||
| 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++) { | ||
| child.push({ | ||
| label: `子选项${i}.${j}.${k}`, | ||
| value: `${i}.${j}.${k}`, | ||
| }); | ||
| } | ||
| children.push({ | ||
| label: `子选项${i}.${j}`, | ||
| value: `${i}.${j}`, | ||
| children: child, | ||
| }); | ||
| } | ||
|
|
||
| list.push({ | ||
| label: `选项${i}`, | ||
| value: `${i}`, | ||
| children, | ||
| }); | ||
| } | ||
| return list; | ||
| } | ||
|
|
||
| onMounted(() => { | ||
| const data = initOptions(); | ||
| options.value = data; | ||
| }); | ||
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import { computed, defineComponent, h, nextTick, PropType, ref, watch } from 'vue'; | ||
| import { expandClickEffect, valueChangeEffect } from '../utils'; | ||
| import { CascaderContextType, TreeNode } from '../types'; | ||
| import { usePrefixClass, useTNodeDefault } from '@tdesign/shared-hooks'; | ||
| import Item from './Item'; | ||
| import { getDefaultNode } from '@tdesign/shared-utils'; | ||
| import CascaderProps from '../props'; | ||
| import { useListVirtualScroll } from '../../list/hooks'; | ||
|
|
||
| const props = { | ||
| treeNodes: { | ||
| type: Array as PropType<TreeNode[]>, | ||
| default: [] as PropType<TreeNode[]>, | ||
| }, | ||
| isFilter: { | ||
| type: Boolean, | ||
| default: false, | ||
| }, | ||
| segment: { | ||
| type: Boolean, | ||
| default: true, | ||
| }, | ||
| listKey: { | ||
| type: String, | ||
| }, | ||
| level: { | ||
| type: Number, | ||
| default: 0, | ||
| }, | ||
| option: CascaderProps.option, | ||
| trigger: CascaderProps.trigger, | ||
| scroll: CascaderProps.scroll, | ||
| cascaderContext: { | ||
| type: Object as PropType<CascaderContextType>, | ||
| }, | ||
| }; | ||
|
|
||
| export default defineComponent({ | ||
| name: 'TCascaderList', | ||
| props, | ||
| setup(props) { | ||
| const renderTNodeJSXDefault = useTNodeDefault(); | ||
| const COMPONENT_NAME = usePrefixClass('cascader'); | ||
| const panelWrapperRef = ref<HTMLDivElement>(null); | ||
|
|
||
| const treeNodes = computed(() => props.treeNodes); | ||
| const isVisible = computed(() => props.cascaderContext.visible); | ||
| const scroll = computed(() => ({ | ||
| rowHeight: 28, | ||
| ...props.scroll, | ||
| })); | ||
|
|
||
| const { virtualConfig, cursorStyle, listStyle, isVirtualScroll, onInnerVirtualScroll, scrollToElement } = | ||
| useListVirtualScroll(scroll.value, panelWrapperRef, treeNodes as any); | ||
|
|
||
| const handleExpand = (node: TreeNode, trigger: 'hover' | 'click') => { | ||
| const { trigger: propsTrigger, cascaderContext } = props; | ||
| expandClickEffect(propsTrigger, trigger, node, cascaderContext); | ||
| }; | ||
|
|
||
| const renderItem = (node: TreeNode, index: number) => { | ||
| const optionChild = node.data.content | ||
| ? getDefaultNode(node.data.content(h)) | ||
| : renderTNodeJSXDefault('option', { | ||
| params: { | ||
| item: node.data, | ||
| index, | ||
| onExpand: () => handleExpand(node, 'click'), | ||
| onChange: () => valueChangeEffect(node, props.cascaderContext), | ||
| }, | ||
| }); | ||
| return ( | ||
| <Item | ||
| key={node.value} | ||
| node={node} | ||
| optionChild={optionChild} | ||
| cascaderContext={props.cascaderContext} | ||
| onClick={() => { | ||
| handleExpand(node, 'click'); | ||
| }} | ||
| onMouseenter={() => { | ||
| handleExpand(node, 'hover'); | ||
| }} | ||
| onChange={() => { | ||
| valueChangeEffect(node, props.cascaderContext); | ||
| }} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| const onScrollIntoView = () => { | ||
| const { level, treeNodes, cascaderContext } = props; | ||
| const checkedNodes = 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; | ||
| } | ||
| lastCheckedNodes = lastCheckedNodes?.parent; | ||
| } | ||
| } | ||
| if (index !== -1) { | ||
| scrollToElement({ | ||
| index, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| const handleScroll = (event: WheelEvent): void => { | ||
| if (isVirtualScroll.value) onInnerVirtualScroll(event as unknown as WheelEvent); | ||
| }; | ||
|
|
||
| watch( | ||
| isVisible, | ||
| () => { | ||
| if (props.scroll && props.cascaderContext.visible) { | ||
| setTimeout(() => { | ||
| nextTick(() => { | ||
| onScrollIntoView(); | ||
| }); | ||
| }, 16); | ||
| } | ||
| }, | ||
| { | ||
| immediate: true, | ||
| }, | ||
| ); | ||
|
|
||
| return () => { | ||
| const { treeNodes, isFilter, segment, listKey: key } = props; | ||
|
|
||
| return ( | ||
| <div | ||
| ref={panelWrapperRef} | ||
| onScroll={handleScroll} | ||
| class={[ | ||
| `${COMPONENT_NAME.value}__menu`, | ||
| 'narrow-scrollbar', | ||
| { | ||
| [`${COMPONENT_NAME.value}__menu--segment`]: segment, | ||
| [`${COMPONENT_NAME.value}__menu--filter`]: isFilter, | ||
| }, | ||
| ]} | ||
| style={{ | ||
| position: isVirtualScroll.value ? 'relative' : undefined, | ||
| }} | ||
| > | ||
| {isVirtualScroll.value ? ( | ||
| <> | ||
| <div style={cursorStyle.value}></div> | ||
| <ul key={key} style={listStyle.value}> | ||
| {virtualConfig.visibleData.value.map((node, index) => renderItem(node, index))} | ||
| </ul> | ||
| </> | ||
| ) : ( | ||
| <ul key={key}>{treeNodes.map((node: TreeNode, index: number) => renderItem(node, index))}</ul> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
| }, | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.