Skip to content

Commit

Permalink
fix(data-table): header and body's scrolling not sync when using the …
Browse files Browse the repository at this point in the history
…keyboard, closes tusen-ai#3941 (tusen-ai#4051)

Co-authored-by: 07akioni <[email protected]>
  • Loading branch information
jizai1125 and 07akioni authored Jun 19, 2023
1 parent 8900300 commit e181f9c
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 50 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- Fix `n-input` doesn't display the horizontal scroll bar when `type` is `textarea`, closes [#4570](https://github.com/tusen-ai/naive-ui/issues/4570).
- Fix `n-alert`'s content style problem, when there is no title and use closable, closes [#4588](https://github.com/tusen-ai/naive-ui/issues/4588).
- Fix `n-select`'s `empty` slot action then it is an interactive component, closes [#4700](https://github.com/tusen-ai/naive-ui/issues/4700).
- Fix `n-data-table` header and body's scrolling are not sync when using the keyboard, closes [#3941](https://github.com/tusen-ai/naive-ui/issues/3941).

### Features

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
- 修复 `n-input``type``textarea` 时不显示纵向滚动条的问题,关闭 [#4570](https://github.com/tusen-ai/naive-ui/issues/4570)
- 修复 `n-alert` 在没有标题且可关闭时,内容样式出现的问题,关闭 [#4588](https://github.com/tusen-ai/naive-ui/issues/4588)
- 修复 `n-select``empty` slot 为可交互组件时的行为,关闭 [#4700](https://github.com/tusen-ai/naive-ui/issues/4700)
- 修复 `n-data-table` 使用按键滚动时 header 和 body 不同步,关闭 [#3941](https://github.com/tusen-ai/naive-ui/issues/3941)

### Features

Expand Down
9 changes: 1 addition & 8 deletions src/data-table/src/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
toRef,
type CSSProperties,
Transition,
watchEffect,
onDeactivated
watchEffect
} from 'vue'
import { createId } from 'seemly'
import { useConfig, useLocale, useTheme, useThemeClass } from '../../_mixins'
Expand Down Expand Up @@ -86,10 +85,6 @@ export default defineComponent({
mergedClsPrefixRef
)
const bodyWidthRef = ref<number | null>(null)
const scrollPartRef = ref<'head' | 'body'>('body')
onDeactivated(() => {
scrollPartRef.value = 'body'
})
const mainTableInstRef = ref<MainTableRef | null>(null)
const { getResizableWidth, clearResizableWidth, doUpdateResizableWidth } =
useResizable()
Expand Down Expand Up @@ -154,7 +149,6 @@ export default defineComponent({
fixedColumnLeftMapRef,
fixedColumnRightMapRef
} = useScroll(props, {
scrollPartRef,
bodyWidthRef,
mainTableInstRef,
mergedCurrentPageRef
Expand Down Expand Up @@ -210,7 +204,6 @@ export default defineComponent({
mergedExpandedRowKeysRef,
mergedInderminateRowKeySetRef,
localeRef,
scrollPartRef,
expandableRef,
stickyExpandedRowsRef,
rowKeyRef: toRef(props, 'rowKey'),
Expand Down
8 changes: 0 additions & 8 deletions src/data-table/src/TableParts/Body.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ export default defineComponent({
mergedSortStateRef,
virtualScrollRef,
componentId,
scrollPartRef,
mergedTableLayoutRef,
childTriggerColIndexRef,
indentRef,
Expand Down Expand Up @@ -321,9 +320,6 @@ export default defineComponent({
function handleMouseleaveTable (): void {
hoverKeyRef.value = null
}
function handleMouseenterTable (): void {
scrollPartRef.value = 'body'
}
function virtualListContainer (): HTMLElement {
const { value } = virtualListRef
return value?.listElRef as HTMLElement
Expand Down Expand Up @@ -498,7 +494,6 @@ export default defineComponent({
renderExpandIcon: renderExpandIconRef,
scrollbarProps: scrollbarPropsRef,
setHeaderScrollLeft,
handleMouseenterTable,
handleVirtualListScroll,
handleVirtualListResize,
handleMouseleaveTable,
Expand Down Expand Up @@ -580,7 +575,6 @@ export default defineComponent({
childTriggerColIndex,
expandable,
rowProps,
handleMouseenterTable,
handleMouseleaveTable,
renderExpand,
summary,
Expand Down Expand Up @@ -935,7 +929,6 @@ export default defineComponent({
<table
class={`${mergedClsPrefix}-data-table-table`}
onMouseleave={handleMouseleaveTable}
onMouseenter={handleMouseenterTable}
style={{
tableLayout: this.mergedTableLayout
}}
Expand Down Expand Up @@ -969,7 +962,6 @@ export default defineComponent({
clsPrefix: mergedClsPrefix,
id: componentId,
cols,
onMouseenter: handleMouseenterTable,
onMouseleave: handleMouseleaveTable
}}
showScrollbar={false}
Expand Down
20 changes: 2 additions & 18 deletions src/data-table/src/TableParts/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export default defineComponent({
checkOptionsRef,
mergedSortStateRef,
componentId,
scrollPartRef,
mergedTableLayoutRef,
headerCheckboxDisabledRef,
onUnstableColumnResize,
Expand Down Expand Up @@ -104,13 +103,7 @@ export default defineComponent({
const nextSorter = createNextSorter(column, activeSorter)
deriveNextSorter(nextSorter)
}
function handleMouseenter (): void {
scrollPartRef.value = 'head'
}
function handleMouseleave (): void {
scrollPartRef.value = 'body'
}
const resizeStartWidthMap = new Map<ColumnKey, number | undefined>()
const resizeStartWidthMap: Map<ColumnKey, number | undefined> = new Map()
function handleColumnResizeStart (column: TableBaseColumn): void {
resizeStartWidthMap.set(column.key, getCellActualWidth(column.key))
}
Expand Down Expand Up @@ -153,8 +146,6 @@ export default defineComponent({
checkOptions: checkOptionsRef,
mergedTableLayout: mergedTableLayoutRef,
headerCheckboxDisabled: headerCheckboxDisabledRef,
handleMouseenter,
handleMouseleave,
handleCheckboxUpdateChecked,
handleColHeaderClick,
handleTableHeaderScroll,
Expand Down Expand Up @@ -327,18 +318,11 @@ export default defineComponent({
if (!discrete) {
return theadVNode
}
const {
handleTableHeaderScroll,
handleMouseenter,
handleMouseleave,
scrollX
} = this
const { handleTableHeaderScroll, scrollX } = this
return (
<div
class={`${mergedClsPrefix}-data-table-base-table-header`}
onScroll={handleTableHeaderScroll}
onMouseenter={handleMouseenter}
onMouseleave={handleMouseleave}
>
<table
ref="body"
Expand Down
1 change: 0 additions & 1 deletion src/data-table/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,6 @@ export interface DataTableInjection {
rawPaginatedDataRef: Ref<InternalRowData[]>
virtualScrollRef: Ref<boolean>
bodyWidthRef: Ref<number | null>
scrollPartRef: Ref<'head' | 'body'>
mergedTableLayoutRef: Ref<'auto' | 'fixed'>
maxHeightRef: Ref<string | number | undefined>
minHeightRef: Ref<string | number | undefined>
Expand Down
34 changes: 19 additions & 15 deletions src/data-table/src/use-scroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ export function useScroll (
{
mainTableInstRef,
mergedCurrentPageRef,
bodyWidthRef,
scrollPartRef
bodyWidthRef
}: {
scrollPartRef: Ref<'head' | 'body'>
bodyWidthRef: Ref<null | number>
mainTableInstRef: Ref<MainTableRef | null>
mergedCurrentPageRef: ComputedRef<number>
}
) {
let scrollLeft = 0
let lastScrollLeft = 0
const scrollPartRef = ref<'head' | 'body' | undefined>()
const leftActiveFixedColKeyRef = ref<ColumnKey | null>(null)
const leftActiveFixedChildrenColKeysRef = ref<ColumnKey[]>([])
const rightActiveFixedColKeyRef = ref<ColumnKey | null>(null)
Expand Down Expand Up @@ -91,7 +90,7 @@ export function useScroll (
let leftActiveFixedColKey: string | number | null = null
for (let i = 0; i < leftFixedColumns.length; ++i) {
const key = getColKey(leftFixedColumns[i])
if (scrollLeft > (fixedColumnLeftMap[key]?.start || 0) - leftWidth) {
if (lastScrollLeft > (fixedColumnLeftMap[key]?.start || 0) - leftWidth) {
leftActiveFixedColKey = key
leftWidth = fixedColumnLeftMap[key]?.end || 0
} else {
Expand Down Expand Up @@ -129,7 +128,7 @@ export function useScroll (
const key = getColKey(rightFixedColumns[i])
if (
Math.round(
scrollLeft +
lastScrollLeft +
(fixedColumnRightMap[key]?.start || 0) +
tableWidth -
rightWidth
Expand Down Expand Up @@ -183,14 +182,18 @@ export function useScroll (
}
}
function handleTableHeaderScroll (): void {
if (scrollPartRef.value === 'head') {
if (scrollPartRef.value !== 'body') {
beforeNextFrameOnce(syncScrollState)
} else {
scrollPartRef.value = undefined
}
}
function handleTableBodyScroll (e: Event): void {
props.onScroll?.(e)
if (scrollPartRef.value === 'body') {
if (scrollPartRef.value !== 'head') {
beforeNextFrameOnce(syncScrollState)
} else {
scrollPartRef.value = undefined
}
}
function syncScrollState (): void {
Expand All @@ -201,19 +204,20 @@ export function useScroll (
if (!body) return
const { value: tableWidth } = bodyWidthRef
if (tableWidth === null) return
const { value: scrollPart } = scrollPartRef
if (props.maxHeight || props.flexHeight) {
if (!header) return
// we need to deal with overscroll
if (scrollPart === 'head') {
scrollLeft = header.scrollLeft
body.scrollLeft = scrollLeft
const directionHead = lastScrollLeft - header.scrollLeft
scrollPartRef.value = directionHead !== 0 ? 'head' : 'body'
if (scrollPartRef.value === 'head') {
lastScrollLeft = header.scrollLeft
body.scrollLeft = lastScrollLeft
} else {
scrollLeft = body.scrollLeft
header.scrollLeft = scrollLeft
lastScrollLeft = body.scrollLeft
header.scrollLeft = lastScrollLeft
}
} else {
scrollLeft = body.scrollLeft
lastScrollLeft = body.scrollLeft
}
deriveActiveLeftFixedColumn()
deriveActiveLeftFixedChildrenColumns()
Expand Down

0 comments on commit e181f9c

Please sign in to comment.