-
Notifications
You must be signed in to change notification settings - Fork 540
Expand file tree
/
Copy pathcreateTableColumns.ts
More file actions
158 lines (132 loc) · 4.9 KB
/
createTableColumns.ts
File metadata and controls
158 lines (132 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import type {MouseEvent, ReactNode} from "react"
import type {ColumnsType} from "antd/es/table"
import clsx from "clsx"
import type {TableColumnConfig, TableColumnGroup, TableColumnCell} from "./types"
type ColumnWithChildren<Row extends object> = ColumnsType<Row>[number] & {
children?: ColumnsType<Row>
}
type OnHeaderCell<Row extends object> = ColumnsType<Row>[number]["onHeaderCell"]
type OnHeaderCellArgs<Row extends object> = Parameters<NonNullable<OnHeaderCell<Row>>>
type OnHeaderCellResult<Row extends object> = ReturnType<NonNullable<OnHeaderCell<Row>>>
const normalizeGroups = <Row extends object>(
groups: TableColumnGroup<Row>[],
): TableColumnConfig<Row>[] =>
groups.flatMap((group) => {
if (Array.isArray(group)) {
return group
}
return [group]
})
const resolveTitle = <Row extends object>(
config: TableColumnConfig<Row>,
depth: number,
): ReactNode => {
if (typeof config.title === "function") {
return config.title({column: config, depth})
}
return config.title
}
const applyCellRenderer = <Row extends object>(
column: ColumnsType<Row>[number],
cell?: TableColumnCell<Row>,
) => {
if (!cell) return
column.render = (_value, record: Row, index) => cell.render(record, index)
column.align = cell.align ?? column.align
column.className = clsx(column.className, cell.className)
}
const buildColumn = <Row extends object>(
config: TableColumnConfig<Row>,
depth = 0,
): ColumnsType<Row>[number] => {
const column: ColumnWithChildren<Row> = {
key: config.key,
title: resolveTitle(config, depth),
width: config.width,
fixed: config.fixed,
align: config.align,
ellipsis: config.ellipsis,
className: clsx(config.className),
shouldCellUpdate: config.shouldCellUpdate,
}
applyCellRenderer(column, config.cell)
if (config.children?.length) {
column.children = config.children.map((child) => buildColumn(child, depth + 1))
}
if (config.minWidth || config.flex) {
const prev = config.columnProps?.onHeaderCell
column.onHeaderCell = (...args: OnHeaderCellArgs<Row>): OnHeaderCellResult<Row> => {
const baseStyle: React.CSSProperties = {
minWidth: config.minWidth,
flex: config.flex,
}
const prevResult = typeof prev === "function" ? prev(...args) : undefined
return {
...(prevResult ?? {}),
style: {...baseStyle, ...(prevResult?.style ?? {})},
}
}
}
if (config.columnProps) {
const {className, render, ...rest} = config.columnProps
column.className = clsx(column.className, className)
Object.assign(column, rest)
if (!column.render && render) {
column.render = render
}
}
if (config.visibilityKey) {
;(column as any)["data-column-visibility-key"] = config.visibilityKey
}
if (config.visibilityLabel) {
;(column as any).columnVisibilityLabel = config.visibilityLabel
}
if (config.visibilityLocked) {
;(column as any).columnVisibilityLocked = true
}
if (config.visibilityTitle) {
;(column as any).columnVisibilityTitle = config.visibilityTitle
}
if (config.defaultHidden) {
;(column as any).defaultHidden = true
}
if (config.exportLabel) {
;(column as any).exportLabel = config.exportLabel
}
if (config.exportEnabled === false) {
;(column as any).exportEnabled = false
}
if (config.exportDataIndex) {
;(column as any).exportDataIndex = config.exportDataIndex
}
if (config.exportValue) {
;(column as any).exportValue = config.exportValue
}
if (config.exportFormatter) {
;(column as any).exportFormatter = config.exportFormatter
}
if (config.exportMetadata !== undefined) {
;(column as any).exportMetadata = config.exportMetadata
}
// Auto-stop click propagation in action columns so clicks on empty cell area
// don't bubble to the row navigation handler.
if (config.key === "actions") {
const prevOnCell = column.onCell as ((record: Row, index?: number) => any) | undefined
column.onCell = (record: Row, index?: number) => {
const base = prevOnCell ? prevOnCell(record, index) : {}
const prevClick = (base as any)?.onClick
return {
...base,
className: clsx((base as any)?.className, "ag-table-actions-cell"),
onClick: (e: MouseEvent<HTMLTableDataCellElement>) => {
e.stopPropagation()
prevClick?.(e)
},
}
}
}
return column
}
export const createTableColumns = <Row extends object>(
groups: TableColumnGroup<Row>[],
): ColumnsType<Row> => normalizeGroups(groups).map((config) => buildColumn(config))