-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(List): support virtual scroll (#3360)
* feat(List): support virtual scroll * chore: update snapshot
- Loading branch information
Showing
12 changed files
with
628 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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,14 @@ | ||
<template> | ||
<t-list style="height: 300px" :scroll="{ type: 'virtual' }" | ||
><t-list-item v-for="(item, index) in listRef" :key="index"> {{ item.content }}</t-list-item></t-list | ||
> | ||
</template> | ||
<script setup> | ||
import { ref } from 'vue'; | ||
const list = []; | ||
for (let i = 0; i < 10000; i++) { | ||
list.push({ content: `选项${i + 1}` }); | ||
} | ||
const listRef = ref(list); | ||
</script> |
This file contains 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,28 @@ | ||
import { computed } from 'vue'; | ||
import isArray from 'lodash/isArray'; | ||
|
||
import { useChildComponentSlots } from '../../hooks/slot'; | ||
|
||
export const useListItems = () => { | ||
const getChildComponentSlots = useChildComponentSlots(); | ||
|
||
const listItems = computed(() => { | ||
const computedListItems = []; | ||
// 处理 slots | ||
const listItemSlots = getChildComponentSlots('ListItem'); | ||
|
||
if (isArray(listItemSlots)) { | ||
for (const child of listItemSlots) { | ||
computedListItems.push({ | ||
...child.props, | ||
slots: child.children, | ||
} as any); | ||
} | ||
} | ||
return computedListItems; | ||
}); | ||
|
||
return { | ||
listItems, | ||
}; | ||
}; |
This file contains 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,61 @@ | ||
import { Ref, computed } from 'vue'; | ||
import useVirtualScroll from '../../hooks/useVirtualScrollNew'; | ||
import { TdListProps } from '../type'; | ||
import { Styles } from '../../common'; | ||
|
||
export const useListVirtualScroll = ( | ||
scroll: TdListProps['scroll'], | ||
listRef: Ref<HTMLElement>, | ||
listItems: Ref<any[]>, | ||
) => { | ||
const virtualScrollParams = computed(() => ({ | ||
data: listItems.value, | ||
scroll: scroll, | ||
})); | ||
const virtualConfig = useVirtualScroll(listRef, virtualScrollParams); | ||
const isVirtualScroll = computed(() => virtualConfig.isVirtualScroll.value); | ||
let lastScrollY = -1; | ||
|
||
const onInnerVirtualScroll = (e: WheelEvent) => { | ||
const target = (e.target || e.srcElement) as HTMLElement; | ||
const top = target.scrollTop; | ||
if (lastScrollY !== top) { | ||
virtualConfig.isVirtualScroll.value && virtualConfig.handleScroll(); | ||
} else { | ||
lastScrollY = -1; | ||
} | ||
lastScrollY = top; | ||
}; | ||
|
||
const cursorStyle = computed( | ||
() => | ||
({ | ||
position: 'absolute', | ||
width: '1px', | ||
height: '1px', | ||
transition: 'transform 0.2s', | ||
transform: `translate(0, ${virtualConfig.scrollHeight.value}px)`, | ||
'-ms-transform': `translate(0, ${virtualConfig.scrollHeight.value}px)`, | ||
'-moz-transform': `translate(0, ${virtualConfig.scrollHeight.value}px)`, | ||
'-webkit-transform': `translate(0, ${virtualConfig.scrollHeight.value}px)`, | ||
} as Styles), | ||
); | ||
|
||
const listStyle = computed( | ||
() => | ||
({ | ||
transform: `translate(0, ${virtualConfig.translateY.value}px)`, | ||
'-ms-transform': `translate(0, ${virtualConfig.translateY.value}px)`, | ||
'-moz-transform': `translate(0, ${virtualConfig.translateY.value}px)`, | ||
'-webkit-transform': `translate(0, ${virtualConfig.translateY.value}px)`, | ||
} as Styles), | ||
); | ||
|
||
return { | ||
virtualConfig, | ||
cursorStyle, | ||
listStyle, | ||
isVirtualScroll, | ||
onInnerVirtualScroll, | ||
}; | ||
}; |
This file contains 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 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 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 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 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 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 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
Oops, something went wrong.