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

feat(table): 虚拟列表下,支持设置滚动位置 (#3044) #3045

Merged
merged 3 commits into from
Nov 19, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/moody-bikes-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@hi-ui/table": minor
"@hi-ui/hiui": minor
---

feat(table): 虚拟列表下,支持设置滚动位置(#3044)
12 changes: 10 additions & 2 deletions packages/ui/table/src/TableBody.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { forwardRef, useMemo, useRef } from 'react'
import VirtualList from 'rc-virtual-list'
import React, { forwardRef, useMemo, useRef, useImperativeHandle } from 'react'
import VirtualList, { ListRef } from 'rc-virtual-list'
import { cx, getPrefixCls } from '@hi-ui/classname'
import { __DEV__ } from '@hi-ui/env'
import { useLatestCallback } from '@hi-ui/use-latest'
Expand Down Expand Up @@ -32,8 +32,11 @@ export const TableBody = forwardRef<HTMLDivElement | null, TableBodyProps>(
measureRowElementRef,
scrollbar,
scrollLeft,
innerRef,
} = useTableContext()
const virtualListRef = useRef(null)
const listRef = useRef<ListRef>(null)

const cls = cx(`${prefixCls}-body`)

const getRequiredProps = useLatestCallback(
Expand All @@ -56,6 +59,10 @@ export const TableBody = forwardRef<HTMLDivElement | null, TableBodyProps>(
return tmpWidth
}, [colWidths])

useImperativeHandle(innerRef, () => ({
scrollTo: listRef.current?.scrollTo,
}))

if (virtual) {
// TODO: avg和summay row的逻辑
const realHeight = (virtualListRef.current as HTMLTableElement | null)?.getBoundingClientRect()
Expand Down Expand Up @@ -92,6 +99,7 @@ export const TableBody = forwardRef<HTMLDivElement | null, TableBodyProps>(
style={{ width: '100%', position: 'sticky', left: 0, maxHeight }}
>
<VirtualList
ref={listRef}
prefixCls={`${cls}--virtual`}
data={transitionData}
height={vMaxHeight}
Expand Down
18 changes: 18 additions & 0 deletions packages/ui/table/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,21 @@ export interface TableRowRequiredProps {
}

export type TableRowRecord = Record<string, any>

export type ScrollAlign = 'top' | 'bottom' | 'auto'

export type ScrollConfig =
| {
index: number
align?: ScrollAlign
offset?: number
}
| {
key: React.Key
align?: ScrollAlign
offset?: number
}

export interface TableHelper {
scrollTo?: (arg: number | ScrollConfig) => void
}
7 changes: 7 additions & 0 deletions packages/ui/table/src/use-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
TableRowSelection,
FlattedTableColumnItemData,
FlattedTableRowData,
TableHelper,
} from './types'
import { SELECTION_DATA_KEY } from './Table'

Expand Down Expand Up @@ -87,6 +88,7 @@ export const useTable = ({
cellClassName,
onChange,
onHighlightedCol,
innerRef,
...rootProps
}: UseTableProps) => {
/**
Expand Down Expand Up @@ -687,6 +689,7 @@ export const useTable = ({
rowClassName,
cellClassName,
onHighlightedCol,
innerRef,
}
}

Expand Down Expand Up @@ -904,6 +907,10 @@ export interface UseTableProps {
},
highlightedColKeys: string[]
) => void
/**
* 提供辅助方法的内部引用
*/
innerRef?: React.Ref<TableHelper>
}

export type UseTableReturn = ReturnType<typeof useTable>
16 changes: 14 additions & 2 deletions packages/ui/table/stories/virtual.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react'
import Table from '../src'

import Table, { TableHelper } from '../src'
import Button from '@hi-ui/button'
/**
* @title 虚拟列表
*/
Expand Down Expand Up @@ -45,16 +45,28 @@ export const Virtual = () => {
},
])
const [data] = React.useState(MockData)
const tableRef = React.useRef<TableHelper>(null)

return (
<>
<h1>Virtual for Table</h1>
<div className="table-virtual__wrap" style={{ minWidth: 660, background: '#fff' }}>
<div style={{ marginBottom: '1em' }}>
<Button
onClick={() => {
// key 为节点 id
tableRef.current?.scrollTo?.({ key: '小米-1000', align: 'top' })
}}
>
scroll to key: 小米-1000
</Button>
</div>
<Table
fieldKey="name"
columns={column}
data={data}
virtual={true}
innerRef={tableRef}
// virtual={{
// onVisibleChange(...args) {
// console.log('onVisibleChange', ...args)
Expand Down