Skip to content

Commit

Permalink
fix: 修复虚拟列表首次滚动底部BUG (Evansy#24)
Browse files Browse the repository at this point in the history
* fix: 修复首次滚动底部、移除包裹组件

* style: format code
  • Loading branch information
oljc authored Jun 4, 2023
1 parent 671f87c commit 5e6aa4f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
14 changes: 13 additions & 1 deletion src/components/VirtualList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ export default defineComponent({
type: Number,
default: 0,
},
// Item项的Props
itemProps: {
type: Object,
},
// 指定用什么名字传递数据
dataPropName: {
type: String,
default: 'source',
},
},
setup(props, { emit, expose }) {
const range = ref<Range | null>(null)
Expand Down Expand Up @@ -195,7 +204,7 @@ export default defineComponent({
const getRenderSlots = () => {
const slots = []
const { start, end } = range.value! // 解构获取范围的起始、结束索引
const { data, dataKey, item } = props
const { data, dataKey, item, itemProps, dataPropName } = props
for (let index = start; index <= end; index++) {
const dataSource = data[index] as DataSource // 获取当前索引的数据项
if (dataSource) {
Expand All @@ -209,6 +218,8 @@ export default defineComponent({
uniqueKey={uniqueKey}
source={dataSource}
component={item}
itemProps={itemProps}
dataPropName={dataPropName}
onItemResize={onItemResized}
/>,
)
Expand Down Expand Up @@ -255,6 +266,7 @@ export default defineComponent({
} else if (props.offset) {
scrollToOffset(props.offset)
}
emit('ok')
})

onUnmounted(() => {
Expand Down
18 changes: 15 additions & 3 deletions src/components/VirtualList/item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export default defineComponent({
uniqueKey: {
type: [String, Number],
},
itemProps: {
type: Object,
default: () => {},
},
dataPropName: {
type: String,
default: 'source',
},
},
emits: ['itemResize'],
setup(props, { emit }) {
Expand Down Expand Up @@ -66,12 +74,16 @@ export default defineComponent({
})

return () => {
const { component: Comp, index, source, uniqueKey } = props
const { component: Comp, index, source, uniqueKey, itemProps, dataPropName } = props
const merged = {
...itemProps,
[dataPropName]: source, // 数据源给到指定属性上
}
// 渲染出传入自定义组件项-这里的Comp就是传入的自定义组件 (ts-ignore避免类型警告)
return (
<div key={uniqueKey} ref={rootRef}>
<div key={uniqueKey} ref={rootRef} {...{ index }}>
{/* @ts-ignore */}
<Comp {...{ source, index }} />
<Comp {...merged} />
</div>
)
}
Expand Down
7 changes: 4 additions & 3 deletions src/views/Home/components/ChatList/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import throttle from 'lodash/throttle'
import { useChatStore } from '@/stores/chat'
import type { MessageItemType } from '@/services/types'
import VirtualList from '@/components/VirtualList/index'
import Item from './item.vue'
import MsgItem from './MsgItem/index.vue'
const chatStore = useChatStore()
const virtualListRef = ref()
Expand All @@ -22,7 +22,6 @@ onMounted(() => {
chatStore.chatListToBottomAction = () => {
goToBottom()
}
goToBottom()
})
})
Expand Down Expand Up @@ -62,12 +61,14 @@ const getKey = (item: MessageItemType) => item.message.id
v-if="chatStore.chatMessageList?.length"
ref="virtualListRef"
class="virtual-list"
dataPropName="msg"
:data="chatStore.chatMessageList"
:data-key="getKey"
:item="Item"
:item="MsgItem"
:size="20"
@totop="onTotop"
@scroll="onScroll"
@ok="goToBottom"
/>
<template v-if="!chatStore.isLoading && chatStore.chatMessageList?.length === 0">
<div class="empty">暂无消息,快来发送第一条消息吧~</div>
Expand Down
18 changes: 0 additions & 18 deletions src/views/Home/components/ChatList/item.vue

This file was deleted.

0 comments on commit 5e6aa4f

Please sign in to comment.