Skip to content

Commit

Permalink
feat(table): Add onResize api (#2984)
Browse files Browse the repository at this point in the history
  • Loading branch information
zyprepare committed Sep 6, 2024
1 parent 878941f commit 2736cb1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-ads-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

feat(table): Add onResize api
5 changes: 5 additions & 0 deletions .changeset/sharp-days-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/table": minor
---

feat: Add onResize api
4 changes: 2 additions & 2 deletions packages/ui/table/src/BaseTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { useEmbedExpand, UseEmbedExpandProps } from './hooks/use-embed-expand'
import { TheadContent } from './TheadContent'
import { ColGroupContent } from './ColGroupContent'
import { TbodyContent } from './TbodyContent'
import { SELECTION_DATA_KEY } from './Table';
import { SELECTION_DATA_KEY } from './Table'

const _role = 'table'
const _prefix = getPrefixCls('table')
Expand Down Expand Up @@ -151,7 +151,7 @@ export const BaseTable = forwardRef<HTMLDivElement | null, BaseTableProps>(
let hasSumColumn = false

columns.forEach((column, index) => {
if (index === 0 || (index === 1 && columns[0].dataKey === SELECTION_DATA_KEY)) {
if (index === 0 || (index === 1 && columns[0].dataKey === SELECTION_DATA_KEY)) {
// @ts-ignore
sumRow.raw[column.dataKey] = i18n.get('table.total')
}
Expand Down
15 changes: 13 additions & 2 deletions packages/ui/table/src/hooks/use-col-width.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ import React, { useCallback } from 'react'
import { FlattedTableColumnItemData, TableColumnItem, TableRowRecord } from '../types'
import { getGroupItemWidth } from '../utils'
import { useUpdateEffect } from '@hi-ui/use-update-effect'
import { ResizeCallbackData } from 'react-resizable'

export const useColWidth = ({
resizable,
data,
columns,
virtual,
onResize,
}: {
resizable: boolean
data: TableRowRecord[]
columns: TableColumnItem[]
virtual?: boolean
onResize?: (
e: React.SyntheticEvent,
data: ResizeCallbackData,
index: number,
columnsWidth: number[]
) => void
}) => {
const measureRowElementRef = React.useRef<HTMLTableRowElement | null>(null)
// 是否重新设置过表格每列宽度
Expand Down Expand Up @@ -221,7 +229,7 @@ export const useColWidth = ({
* 列宽拖拽 resize,只处理拖拽线两边的列宽度
*/
const onColumnResizable = React.useCallback(
(_, { size }, index: number) => {
(evt, { size }, index: number) => {
const minWidth = minColWidth[index]
const anotherMinWidth = minColWidth[index + 1]
let nextWidth = size.width > minWidth ? size.width : minWidth
Expand All @@ -237,10 +245,13 @@ export const useColWidth = ({

nextColWidths[index] = nextWidth
nextColWidths[index + 1] = anotherWidth

onResize?.(evt, size, index, nextColWidths)

return nextColWidths
})
},
[minColWidth]
[minColWidth, onResize]
)

const getColgroupProps = React.useCallback(
Expand Down
12 changes: 12 additions & 0 deletions packages/ui/table/src/use-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
FlattedTableRowData,
} from './types'
import { SELECTION_DATA_KEY } from './Table'
import { ResizeCallbackData } from 'react-resizable'

const DEFAULT_COLUMNS = [] as []
const DEFAULT_DATA = [] as []
Expand Down Expand Up @@ -84,6 +85,7 @@ export const useTable = ({
scrollbar,
rowClassName,
cellClassName,
onResize,
...rootProps
}: UseTableProps) => {
/**
Expand Down Expand Up @@ -193,6 +195,7 @@ export const useTable = ({
columns,
resizable,
virtual: virtual,
onResize,
})

// ************************ 列冻结 ************************ //
Expand Down Expand Up @@ -866,6 +869,15 @@ export interface UseTableProps {
column: Record<string, any>,
index: number
) => string
/**
* resizable 模式下,设置列宽变化时触发的回调
*/
onResize?: (
e: React.SyntheticEvent,
data: ResizeCallbackData,
index: number,
columnsWidth: number[]
) => void
}

export type UseTableReturn = ReturnType<typeof useTable>
3 changes: 3 additions & 0 deletions packages/ui/table/stories/resizable.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const Resizable = () => {
<Table
fixedToColumn={{ left: 'type', right: 'address' }}
resizable
onResize={(e, data, index, columnsWidth) => {
console.log('onResize', e, data, index, columnsWidth)
}}
columns={[
{
title: '商品名',
Expand Down

0 comments on commit 2736cb1

Please sign in to comment.