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): 将表格中的交互统一加上回调事件 (#2977) #2978

Merged
merged 5 commits into from
Nov 6, 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
5 changes: 5 additions & 0 deletions .changeset/fuzzy-radios-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/table": minor
---

feat: 将表格中的交互统一加上回调事件
5 changes: 5 additions & 0 deletions .changeset/shy-coats-crash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

feat(table): 将表格中的交互统一加上回调事件
9 changes: 9 additions & 0 deletions packages/ui/table/src/TableColumnMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export const TableColumnMenu = forwardRef<HTMLDivElement | null, TableColumnMenu
setLeftFreezeColumn,
isHighlightedCol,
onHighlightedColChange,
highlightedColKeys,
onHighlightedCol,
} = useTableContext()

const { id: dataKey, raw: columnRaw } = column
Expand Down Expand Up @@ -111,6 +113,13 @@ export const TableColumnMenu = forwardRef<HTMLDivElement | null, TableColumnMenu
onSwitch={(shouldActive) => {
onHighlightedColChange(column, shouldActive)

const latestHighlightedColKeys = shouldActive
? [...highlightedColKeys, column.raw.dataKey || ''].filter(Boolean)
: [...highlightedColKeys.filter((keys: string) => keys !== column.raw.dataKey)]
onHighlightedCol?.(
{ active: shouldActive, column: column.raw },
latestHighlightedColKeys
)
menuVisibleAction.off()
}}
/>
Expand Down
56 changes: 56 additions & 0 deletions packages/ui/table/src/hooks/use-change.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { useEffect, useRef } from 'react'
import { TableColumnItem, FlattedTableRowData } from '../types'

export interface Sorter {
order: string | null
column: TableColumnItem
}

export interface Action {
sorter?: Sorter
}

export interface Extra {
action: keyof Action
currentDataSource: object[]
}

export interface UseChangeProps {
activeSorterColumn: string | null
activeSorterType: string | null
columns: TableColumnItem[]
showData: FlattedTableRowData[]
onChange?: (action: Action, extra: Extra) => void
}

export const useChange = ({
activeSorterColumn,
activeSorterType,
columns,
showData,
onChange,
}: UseChangeProps) => {
const changeRef = useRef({
sort: { activeSorterColumn, activeSorterType },
})

useEffect(() => {
const { sort } = changeRef.current
const sortNotChange =
sort.activeSorterColumn === activeSorterColumn && sort.activeSorterType === activeSorterType
if (sortNotChange) return
const changeObj = {
sorter: {
order: activeSorterType,
column: columns.filter((d) => d.dataKey === activeSorterColumn)[0],
},
extra: {
action: 'sorter',
currentDataSource: showData.map((d) => d.raw),
} as Extra,
}
onChange?.({ sorter: changeObj.sorter }, changeObj.extra)
sort.activeSorterColumn = activeSorterColumn
sort.activeSorterType = activeSorterType
}, [activeSorterColumn, activeSorterType, columns, showData, onChange])
}
25 changes: 25 additions & 0 deletions packages/ui/table/src/use-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { PaginationProps } from '@hi-ui/pagination'
import { ScrollbarProps } from '@hi-ui/scrollbar'
import { parseFixedColumns, setColumnsDefaultWidth } from './utils'
import { useAsyncSwitch, useExpand } from './hooks'
import { useChange, Action, Extra } from './hooks/use-change'
import { useColWidth } from './hooks/use-col-width'
import { useColumns } from './hooks/use-colgroup'
import { useTableDrag } from './hooks/use-drag'
Expand Down Expand Up @@ -84,6 +85,8 @@ export const useTable = ({
scrollbar,
rowClassName,
cellClassName,
onChange,
onHighlightedCol,
...rootProps
}: UseTableProps) => {
/**
Expand Down Expand Up @@ -593,6 +596,13 @@ export const useTable = ({
return _data
}, [activeSorterColumn, activeSorterType, transitionData, columns])

useChange({
activeSorterColumn,
activeSorterType,
columns,
showData,
onChange,
})
return {
measureRowElementRef,
rootProps,
Expand Down Expand Up @@ -676,6 +686,7 @@ export const useTable = ({
scrollbar,
rowClassName,
cellClassName,
onHighlightedCol,
}
}

Expand Down Expand Up @@ -866,6 +877,20 @@ export interface UseTableProps {
column: Record<string, any>,
index: number
) => string
/**
* 设置排序变化回调
*/
onChange?: (action: Action, extra: Extra) => void
/**
* 设置列高亮回调
*/
onHighlightedCol?: (
changedColInfo: {
active: boolean
column: TableColumnItem
},
highlightedColKeys: string[]
) => void
}

export type UseTableReturn = ReturnType<typeof useTable>
16 changes: 15 additions & 1 deletion packages/ui/table/stories/col-menu.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,25 @@ export const ColMenu = () => {
},
])

const onHighlightedCol = (changedColInfo, highlightedColKeys) => {
console.log(changedColInfo, highlightedColKeys)
}

const onChange = (pagination, sorter, extra) => {
console.log(pagination, sorter, extra)
}

return (
<>
<h1>ColMenu for Table</h1>
<div className="table-col-menu__wrap" style={{ minWidth: 660, background: '#fff' }}>
<Table columns={columns} data={data} showColMenu />
<Table
columns={columns}
data={data}
showColMenu
onHighlightedCol={onHighlightedCol}
onChange={onChange}
/>
</div>
</>
)
Expand Down
6 changes: 5 additions & 1 deletion packages/ui/table/stories/data-sorter.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,15 @@ export const DataSorter = () => {
},
])

const onChange = (pagination, sorter, extra) => {
console.log(pagination, sorter, extra)
}

return (
<>
<h1>DataSorter for Table</h1>
<div className="table-data-sorter__wrap" style={{ minWidth: 660, background: '#fff' }}>
<Table columns={columns} data={data} />
<Table columns={columns} data={data} onChange={onChange} />
</div>
</>
)
Expand Down