-
Notifications
You must be signed in to change notification settings - Fork 125
/
index.tsx
241 lines (219 loc) · 5.66 KB
/
index.tsx
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { Checkbox, Alert } from 'antd'
import cx from 'classnames'
import {
ColumnActionsMode,
ConstrainMode,
DetailsList,
DetailsListLayoutMode,
IColumn,
IDetailsListProps,
SelectionMode,
} from 'office-ui-fabric-react/lib/DetailsList'
import { Sticky, StickyPositionType } from 'office-ui-fabric-react/lib/Sticky'
import React, { useCallback, useMemo } from 'react'
import { usePersistFn } from '@umijs/hooks'
import AnimatedSkeleton from '../AnimatedSkeleton'
import Card from '../Card'
import styles from './index.module.less'
export { AntCheckboxGroupHeader } from './GroupHeader'
DetailsList['whyDidYouRender'] = {
customName: 'DetailsList',
}
function renderStickyHeader(props, defaultRender) {
if (!props) {
return null
}
return (
<Sticky stickyPosition={StickyPositionType.Header} isScrollSynced>
<div className={styles.tableHeader}>{defaultRender!(props)}</div>
</Sticky>
)
}
function renderCheckbox(props) {
return <Checkbox checked={props?.checked} />
}
export function ImprovedDetailsList(props: IDetailsListProps) {
return (
<DetailsList
onRenderDetailsHeader={renderStickyHeader}
onRenderCheckbox={renderCheckbox}
{...props}
/>
)
}
ImprovedDetailsList.whyDidYouRender = true
export const MemoDetailsList = React.memo(ImprovedDetailsList)
function copyAndSort<T>(
items: T[],
columnKey: string,
isSortedDescending?: boolean
): T[] {
const key = columnKey as keyof T
return items
.slice(0)
.sort((a: T, b: T) =>
(isSortedDescending ? a[key] < b[key] : a[key] > b[key]) ? 1 : -1
)
}
export interface ICardTableV2Props extends IDetailsListProps {
title?: React.ReactNode
subTitle?: React.ReactNode
className?: string
style?: object
loading?: boolean
errorMsg?: string
cardExtra?: React.ReactNode
cardNoMargin?: boolean
cardNoMarginTop?: boolean
extendLastColumn?: boolean
// The keys of visible columns. If null, all columns will be shown.
visibleColumnKeys?: { [key: string]: boolean }
visibleItemsCount?: number
// Handle sort
orderBy?: string
desc?: boolean
onChangeOrder?: (orderBy: string, desc: boolean) => void
// Event triggered when a row is clicked.
onRowClicked?: (
item: any,
itemIndex: number,
ev: React.MouseEvent<HTMLElement>
) => void
}
function useRenderClickableRow(onRowClicked) {
return useCallback(
(props, defaultRender) => {
if (!props) {
return null
}
return (
<div
className={styles.clickableTableRow}
onClick={(ev) => onRowClicked?.(props.item, props.itemIndex, ev)}
>
{defaultRender!(props)}
</div>
)
},
[onRowClicked]
)
}
function dummyColumn(): IColumn {
return {
name: '',
key: 'dummy',
minWidth: 28,
maxWidth: 28,
onRender: (_rec) => null,
}
}
function CardTableV2(props: ICardTableV2Props) {
const {
title,
subTitle,
className,
style,
loading = false,
errorMsg,
cardExtra,
cardNoMargin,
cardNoMarginTop,
extendLastColumn,
visibleColumnKeys,
visibleItemsCount,
orderBy,
desc = true,
onChangeOrder,
onRowClicked,
columns,
items,
...restProps
} = props
const renderClickableRow = useRenderClickableRow(onRowClicked)
const onColumnClick = usePersistFn(
(_ev: React.MouseEvent<HTMLElement>, column: IColumn) => {
if (!onChangeOrder) {
return
}
if (column.key === orderBy) {
onChangeOrder(orderBy, !desc)
} else {
onChangeOrder(column.key, true)
}
}
)
const finalColumns = useMemo(() => {
let newColumns: IColumn[] = columns || []
if (visibleColumnKeys != null) {
newColumns = newColumns.filter((c) => visibleColumnKeys[c.key])
}
newColumns = newColumns.map((c) => ({
...c,
isResizable: c.isResizable ?? true,
isSorted: c.key === orderBy,
isSortedDescending: desc,
onColumnClick,
columnActionsMode: c.columnActionsMode || ColumnActionsMode.disabled,
}))
if (!extendLastColumn) {
newColumns.push(dummyColumn())
}
return newColumns
}, [
onColumnClick,
columns,
visibleColumnKeys,
orderBy,
desc,
extendLastColumn,
])
const finalItems = useMemo(() => {
let newItems = items || []
const curColumn = finalColumns.find((col) => col.key === orderBy)
if (curColumn) {
newItems = copyAndSort(
newItems,
curColumn.fieldName!,
curColumn.isSortedDescending
)
}
if (visibleItemsCount != null) {
newItems = newItems.slice(0, visibleItemsCount)
}
return newItems
}, [visibleItemsCount, items, orderBy, finalColumns])
return (
<Card
title={title}
subTitle={subTitle}
style={style}
className={cx(styles.cardTable, className, {
[styles.contentExtended]: extendLastColumn,
})}
noMargin={cardNoMargin}
noMarginTop={cardNoMarginTop}
extra={cardExtra}
>
<AnimatedSkeleton
showSkeleton={items.length === 0 && loading && !errorMsg}
>
{errorMsg ? (
<Alert message={errorMsg} type="error" showIcon />
) : (
<div className={styles.cardTableContent}>
<MemoDetailsList
selectionMode={SelectionMode.none}
constrainMode={ConstrainMode.unconstrained}
layoutMode={DetailsListLayoutMode.justified}
onRenderRow={onRowClicked ? renderClickableRow : undefined}
columns={finalColumns}
items={finalItems}
{...restProps}
/>
</div>
)}
</AnimatedSkeleton>
</Card>
)
}
export default CardTableV2