Skip to content
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

fix(comp:table): virtual table scroll sync should be done by virtual … #1834

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 45 additions & 8 deletions packages/components/table/src/composables/useScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,42 @@ import { type TableProps } from '../types'

export function useScroll(props: TableProps, { setStickyScrollLeft }: StickyContext): ScrollContext {
const virtualScrollRef = ref<VirtualScrollInstance | undefined>()
const headerVirtualScrollRef = ref<VirtualScrollInstance | undefined>()
const scrollHeadRef = ref<HTMLDivElement>()
const scrollBodyRef = ref<HTMLDivElement>()
const scrollContentRef = ref<HTMLDivElement>()
const scrollFootRef = ref<HTMLDivElement>()

watch(virtualScrollRef, instance => {
if (instance) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
scrollBodyRef.value = (instance as any).getHolderElement()
}
})
watch(
virtualScrollRef,
instance => {
if (instance) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
scrollBodyRef.value = (instance as any).getHolderElement()
}
},
{
immediate: true,
},
)
watch(
headerVirtualScrollRef,
instance => {
if (instance) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
scrollHeadRef.value = (instance as any).getHolderElement()
scrollHeadRef.value!.style.overflow = 'hidden'
}
},
{
immediate: true,
},
)

const { handleScroll, pingedStart, pingedEnd } = useScrollRef(
props,
virtualScrollRef,
headerVirtualScrollRef,
scrollHeadRef,
scrollBodyRef,
scrollFootRef,
Expand Down Expand Up @@ -61,6 +83,7 @@ export function useScroll(props: TableProps, { setStickyScrollLeft }: StickyCont

return {
virtualScrollRef,
headerVirtualScrollRef,
scrollHeadRef,
scrollBodyRef,
scrollContentRef,
Expand All @@ -80,6 +103,7 @@ export function useScroll(props: TableProps, { setStickyScrollLeft }: StickyCont

export interface ScrollContext {
virtualScrollRef: Ref<VirtualScrollInstance | undefined>
headerVirtualScrollRef: Ref<VirtualScrollInstance | undefined>
scrollHeadRef: Ref<HTMLDivElement | undefined>
scrollBodyRef: Ref<HTMLDivElement | undefined>
scrollContentRef: Ref<HTMLDivElement | undefined>
Expand All @@ -103,6 +127,8 @@ export interface ScrollOptions {

function useScrollRef(
props: TableProps,
virtualScrollRef: Ref<VirtualScrollInstance | undefined>,
headerVirtualScrollRef: Ref<VirtualScrollInstance | undefined>,
scrollHeadRef: Ref<HTMLDivElement | undefined>,
scrollBodyRef: Ref<HTMLDivElement | undefined>,
scrollFootRef: Ref<HTMLDivElement | undefined>,
Expand Down Expand Up @@ -143,6 +169,13 @@ function useScrollRef(
target.scrollLeft = scrollLeft
}
}
const forceVirtualScroll = (scrollLeft: number, target: VirtualScrollInstance | undefined) => {
if (!target) {
return
}

target.scrollTo({ left: scrollLeft })
}

const changeStickyScrollLeft = (scrollLeft: number) => {
const scrollBodyElement = convertElement(scrollBodyRef)
Expand All @@ -160,8 +193,12 @@ function useScrollRef(
const lockedTarget = lockedScrollTargetRef.value
if (!lockedTarget || lockedTarget === currentTarget) {
lockScrollTarget(currentTarget)
forceScroll(mergedScrollLeft, scrollHeadRef.value)
forceScroll(mergedScrollLeft, convertElement(scrollBodyRef))
headerVirtualScrollRef.value
? forceVirtualScroll(mergedScrollLeft, headerVirtualScrollRef.value)
: forceScroll(mergedScrollLeft, scrollHeadRef.value)
virtualScrollRef.value
? forceVirtualScroll(mergedScrollLeft, virtualScrollRef.value)
: forceScroll(mergedScrollLeft, convertElement(scrollBodyRef))
forceScroll(mergedScrollLeft, scrollFootRef.value)
changeStickyScrollLeft(mergedScrollLeft)
}
Expand Down
35 changes: 4 additions & 31 deletions packages/components/table/src/main/FixedHolder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,13 @@

import type { FlattedData } from '../composables/useDataSource'

import {
type CSSProperties,
type Ref,
computed,
defineComponent,
inject,
onBeforeUnmount,
onMounted,
ref,
watch,
} from 'vue'
import { type CSSProperties, type Ref, computed, defineComponent, inject, onBeforeUnmount, onMounted, watch } from 'vue'

import {
CdkVirtualScroll,
type VirtualColRenderFn,
type VirtualContentRenderFn,
type VirtualRowRenderFn,
type VirtualScrollInstance,
type VirtualScrollRowData,
} from '@idux/cdk/scroll'
import { convertCssPixel, off, on } from '@idux/cdk/utils'
Expand All @@ -49,8 +38,9 @@ export default defineComponent({
mergedVirtual,
mergedVirtualColWidth,
getVirtualColWidth,
scrollHeadRef,
headerVirtualScrollRef,
handleScroll,
scrollHeadRef,
scrollWidth,
flattedData,
flattedColumns,
Expand All @@ -59,23 +49,6 @@ export default defineComponent({
isSticky,
} = inject(TABLE_TOKEN)!

const virtualScrollRef = ref<VirtualScrollInstance>()

onMounted(() => {
watch(
virtualScrollRef,
virtualScroll => {
if (virtualScroll) {
scrollHeadRef.value = virtualScroll.getHolderElement()
scrollHeadRef.value.style.overflow = 'hidden'
}
},
{
immediate: true,
},
)
})

useScrollEvents(scrollHeadRef, handleScroll)

const isMaxContent = computed(() => scrollWidth.value === 'max-content')
Expand Down Expand Up @@ -160,7 +133,7 @@ export default defineComponent({
<div class={classes.value} style={style.value}>
{
<CdkVirtualScroll
ref={virtualScrollRef}
ref={headerVirtualScrollRef}
dataSource={virtualData.value}
colModifier={colModifier}
getKey="key"
Expand Down
2 changes: 1 addition & 1 deletion packages/components/table/src/main/head/HeadCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export default defineComponent({
})

const activeSortOrderBy = computed(() => activeOrderByMap[props.column.key])
const activeFilterBy = computed(() => activeFilterByMap[props.column.key] || NoopArray)
const activeFilterBy = computed(() => activeFilterByMap[props.column.key] || (NoopArray as unknown as VKey[]))
const onUpdateFilterBy = (filterBy: VKey[]) => {
const { key, filterable } = props.column
handleFilter(key, filterable!, filterBy)
Expand Down
2 changes: 2 additions & 0 deletions typings/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference types="vue/jsx" />

export {}

declare global {
Expand Down