Skip to content

Commit 77df7b2

Browse files
committed
fix(scroll): fix incorrect key unicity
1 parent 41d5e08 commit 77df7b2

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

src/components/common/buttons/use-back-to-top.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const useBackToTop = () => {
77
const scrolled = ref(false);
88

99
const onClick = (scrollTo?: ScrollTo) => {
10-
listRef.value?.list?.scrollTo({ top: 0, left: 0, behavior: 'smooth', ...scrollTo });
10+
listRef.value?.list?.scrollTo({ top: 0, left: 0, behavior: 'smooth', debounce: true, ...scrollTo });
1111
scrolled.value = false;
1212
};
1313

src/components/common/list/use-list-scroll.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ export const computeNewArray = <T extends ListScrollSourceItemWithDate<D>, D ext
216216
if (!_item.getProgressQuery) _item.getProgressQuery = getProgressQuery(item);
217217
if (!_item.tags) _item.tags = getTags(item, _item.type);
218218

219+
_item.key = `${_item.type}-${_item.id}`;
219220
_item.date = getDate(item, array, index, dateFn);
220221
_item.meta = {
221222
source: item,
@@ -291,7 +292,7 @@ export const useListScrollEvents = (
291292
) => {
292293
const onScroll: OnScroll = async listRef => {
293294
const _listRef = isRef(listRef) ? listRef.value : listRef;
294-
const key = data.value[data.value.length - 1].id;
295+
const { key } = data.value[data.value.length - 1];
295296
await callback({
296297
page: pagination.value?.page ? pagination.value.page + 1 : 0,
297298
});
@@ -335,7 +336,8 @@ export const addAllLoaded = (items: Ref<ListScrollItem[]>, pagination: Ref<Store
335336
if (array[array.length - 1].type === ListScrollItemType.AllLoaded) return array;
336337
const type = ListScrollItemType.AllLoaded;
337338
const index = items.value.length;
338-
const allLoaded: ListScrollItem = { id: type, type, index, key: `${index}-${type}` };
339+
// All loaded should be unique so id and key are based on the type
340+
const allLoaded: ListScrollItem = { id: type, type, index, key: type };
339341
return [...array, allLoaded];
340342
});
341343
};
@@ -353,7 +355,8 @@ export const addLoadMore = (items: Ref<ListScrollItem[]>, pagination: Ref<StoreP
353355
if (array[array.length - 1].type === ListScrollItemType.LoadMore) return array;
354356
const type = ListScrollItemType.LoadMore;
355357
const index = items.value.length;
356-
const loadMore: ListScrollItem = { id: type, type, index, key: `${index}-${type}` };
358+
// Load more should be unique so id and key are based on the type
359+
const loadMore: ListScrollItem = { id: type, type, index, key: type };
357360
return [...array, loadMore];
358361
});
359362
};

src/utils/calendar.utils.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { randomHex } from '@dvcol/common-utils';
12
import { DateUtils } from '@dvcol/common-utils/common/date';
23
import { computed, nextTick, type Ref, ref, watch } from 'vue';
34

@@ -26,9 +27,9 @@ export const CalendarPlaceholder: Partial<CalendarItem> = {
2627
type: ListScrollItemType.Placeholder,
2728
} as const;
2829

29-
export const getPlaceholder = (date: Date) => ({ ...CalendarPlaceholder, id: `empty-${date.getTime()}`, date }) as CalendarItem;
30+
export const getPlaceholder = (date: Date) => ({ ...CalendarPlaceholder, id: `empty-${date.getTime()}-${randomHex(4)}`, date }) as CalendarItem;
3031
export const getLoadingPlaceholder = (date: Date) =>
31-
({ ...getPlaceholder(date), id: `loading-${date.getTime()}`, type: ListScrollItemType.Loading }) as CalendarItem;
32+
({ ...getPlaceholder(date), id: `loading-${date.getTime()}-${randomHex(4)}`, type: ListScrollItemType.Loading }) as CalendarItem;
3233

3334
export const getEmptyWeeks = ({ startDate, loading = false, days = 14 }: { startDate: Date; loading?: boolean; days?: number }): CalendarItem[] => {
3435
return Array(days)
@@ -149,7 +150,8 @@ export const useCalendar = ({
149150
if (!listRef.value?.list) return;
150151

151152
listRef.value?.list.scrollTo({
152-
top: index * 145,
153+
top: index * itemSize,
154+
debounce: true,
153155
...options,
154156
});
155157
};
@@ -172,7 +174,7 @@ export const useCalendar = ({
172174
// offset the scroll by 1 item if a placeholder is added
173175
let placeholder = 0;
174176
const timeout = setTimeout(() => {
175-
listRef.value?.list.scrollTo({ top: 145 });
177+
listRef.value?.list.scrollTo({ top: itemSize });
176178
placeholder = placeholders;
177179
}, defaultDebounceLoadingDelay); // default debounceLoading delay
178180

0 commit comments

Comments
 (0)