forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table_basic.tsx
368 lines (331 loc) · 11.5 KB
/
table_basic.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import './table_basic.scss';
import React, { useCallback, useMemo, useRef, useState, useContext } from 'react';
import { i18n } from '@kbn/i18n';
import useDeepCompareEffect from 'react-use/lib/useDeepCompareEffect';
import {
EuiButtonIcon,
EuiDataGrid,
EuiDataGridControlColumn,
EuiDataGridColumn,
EuiDataGridSorting,
EuiDataGridStyle,
} from '@elastic/eui';
import type { LensFilterEvent, LensTableRowContextMenuEvent } from '../../types';
import type { FormatFactory } from '../../../common';
import type { LensGridDirection } from '../../../common/expressions';
import { VisualizationContainer } from '../../visualization_container';
import { EmptyPlaceholder, findMinMaxByColumnId } from '../../shared_components';
import { LensIconChartDatatable } from '../../assets/chart_datatable';
import type {
DataContextType,
DatatableRenderProps,
LensSortAction,
LensResizeAction,
LensToggleAction,
} from './types';
import { createGridColumns } from './columns';
import { createGridCell } from './cell_value';
import {
createGridFilterHandler,
createGridHideHandler,
createGridResizeHandler,
createGridSortingConfig,
createTransposeColumnFilterHandler,
} from './table_actions';
import { CUSTOM_PALETTE } from '../../shared_components/coloring/constants';
import { getOriginalId, getFinalSummaryConfiguration } from '../../../common/expressions';
export const DataContext = React.createContext<DataContextType>({});
const gridStyle: EuiDataGridStyle = {
border: 'horizontal',
header: 'underline',
};
export const DatatableComponent = (props: DatatableRenderProps) => {
const [firstTable] = Object.values(props.data.tables);
const [columnConfig, setColumnConfig] = useState({
columns: props.args.columns,
sortingColumnId: props.args.sortingColumnId,
sortingDirection: props.args.sortingDirection,
});
const [firstLocalTable, updateTable] = useState(firstTable);
useDeepCompareEffect(() => {
setColumnConfig({
columns: props.args.columns,
sortingColumnId: props.args.sortingColumnId,
sortingDirection: props.args.sortingDirection,
});
}, [props.args.columns, props.args.sortingColumnId, props.args.sortingDirection]);
useDeepCompareEffect(() => {
updateTable(firstTable);
}, [firstTable]);
const firstTableRef = useRef(firstLocalTable);
firstTableRef.current = firstLocalTable;
const untransposedDataRef = useRef(props.untransposedData);
untransposedDataRef.current = props.untransposedData;
const hasAtLeastOneRowClickAction = props.rowHasRowClickTriggerActions?.some((x) => x);
const { getType, dispatchEvent, renderMode, formatFactory } = props;
const formatters: Record<string, ReturnType<FormatFactory>> = useMemo(
() =>
firstLocalTable.columns.reduce(
(map, column) => ({
...map,
[column.id]: formatFactory(column.meta?.params),
}),
{}
),
[firstLocalTable, formatFactory]
);
const onClickValue = useCallback(
(data: LensFilterEvent['data']) => {
dispatchEvent({ name: 'filter', data });
},
[dispatchEvent]
);
const onEditAction = useCallback(
(data: LensSortAction['data'] | LensResizeAction['data'] | LensToggleAction['data']) => {
if (renderMode === 'edit') {
dispatchEvent({ name: 'edit', data });
}
},
[dispatchEvent, renderMode]
);
const onRowContextMenuClick = useCallback(
(data: LensTableRowContextMenuEvent['data']) => {
dispatchEvent({ name: 'tableRowContextMenuClick', data });
},
[dispatchEvent]
);
const handleFilterClick = useMemo(
() => createGridFilterHandler(firstTableRef, onClickValue),
[firstTableRef, onClickValue]
);
const handleTransposedColumnClick = useMemo(
() => createTransposeColumnFilterHandler(onClickValue, untransposedDataRef),
[onClickValue, untransposedDataRef]
);
const bucketColumns = useMemo(
() =>
columnConfig.columns
.filter((_col, index) => {
const col = firstTableRef.current.columns[index];
return (
col?.meta?.sourceParams?.type &&
getType(col.meta.sourceParams.type as string)?.type === 'buckets'
);
})
.map((col) => col.columnId),
[firstTableRef, columnConfig, getType]
);
const isEmpty =
firstLocalTable.rows.length === 0 ||
(bucketColumns.length &&
firstTable.rows.every((row) => bucketColumns.every((col) => row[col] == null)));
const visibleColumns = useMemo(
() =>
columnConfig.columns
.filter((col) => !!col.columnId && !col.hidden)
.map((col) => col.columnId),
[columnConfig]
);
const { sortingColumnId: sortBy, sortingDirection: sortDirection } = props.args;
const isReadOnlySorted = renderMode !== 'edit';
const onColumnResize = useMemo(
() => createGridResizeHandler(columnConfig, setColumnConfig, onEditAction),
[onEditAction, setColumnConfig, columnConfig]
);
const onColumnHide = useMemo(
() => createGridHideHandler(columnConfig, setColumnConfig, onEditAction),
[onEditAction, setColumnConfig, columnConfig]
);
const isNumericMap: Record<string, boolean> = useMemo(() => {
const numericMap: Record<string, boolean> = {};
for (const column of firstLocalTable.columns) {
numericMap[column.id] = column.meta.type === 'number';
}
return numericMap;
}, [firstLocalTable]);
const alignments: Record<string, 'left' | 'right' | 'center'> = useMemo(() => {
const alignmentMap: Record<string, 'left' | 'right' | 'center'> = {};
columnConfig.columns.forEach((column) => {
if (column.alignment) {
alignmentMap[column.columnId] = column.alignment;
} else {
alignmentMap[column.columnId] = isNumericMap[column.columnId] ? 'right' : 'left';
}
});
return alignmentMap;
}, [columnConfig, isNumericMap]);
const minMaxByColumnId: Record<string, { min: number; max: number }> = useMemo(() => {
return findMinMaxByColumnId(
columnConfig.columns
.filter(({ columnId }) => isNumericMap[columnId])
.map(({ columnId }) => columnId),
firstTable,
getOriginalId
);
}, [firstTable, isNumericMap, columnConfig]);
const columns: EuiDataGridColumn[] = useMemo(
() =>
createGridColumns(
bucketColumns,
firstLocalTable,
handleFilterClick,
handleTransposedColumnClick,
isReadOnlySorted,
columnConfig,
visibleColumns,
formatFactory,
onColumnResize,
onColumnHide,
alignments
),
[
bucketColumns,
firstLocalTable,
handleFilterClick,
handleTransposedColumnClick,
isReadOnlySorted,
columnConfig,
visibleColumns,
formatFactory,
onColumnResize,
onColumnHide,
alignments,
]
);
const trailingControlColumns: EuiDataGridControlColumn[] = useMemo(() => {
if (!hasAtLeastOneRowClickAction || !onRowContextMenuClick) {
return [];
}
return [
{
headerCellRender: () => null,
width: 40,
id: 'trailingControlColumn',
rowCellRender: function RowCellRender({ rowIndex }) {
const { rowHasRowClickTriggerActions } = useContext(DataContext);
return (
<EuiButtonIcon
aria-label={i18n.translate('xpack.lens.table.actionsLabel', {
defaultMessage: 'Show actions',
})}
iconType={
!!rowHasRowClickTriggerActions && !rowHasRowClickTriggerActions[rowIndex]
? 'empty'
: 'boxesVertical'
}
color="text"
onClick={() => {
onRowContextMenuClick({
rowIndex,
table: firstTableRef.current,
columns: columnConfig.columns.map((col) => col.columnId),
});
}}
/>
);
},
},
];
}, [firstTableRef, onRowContextMenuClick, columnConfig, hasAtLeastOneRowClickAction]);
const renderCellValue = useMemo(
() => createGridCell(formatters, columnConfig, DataContext, props.uiSettings),
[formatters, columnConfig, props.uiSettings]
);
const columnVisibility = useMemo(
() => ({
visibleColumns,
setVisibleColumns: () => {},
}),
[visibleColumns]
);
const sorting = useMemo<EuiDataGridSorting>(
() => createGridSortingConfig(sortBy, sortDirection as LensGridDirection, onEditAction),
[onEditAction, sortBy, sortDirection]
);
const renderSummaryRow = useMemo(() => {
const columnsWithSummary = columnConfig.columns
.filter((col) => !!col.columnId && !col.hidden)
.map((config) => ({
columnId: config.columnId,
summaryRowValue: config.summaryRowValue,
...getFinalSummaryConfiguration(config.columnId, config, firstTable),
}))
.filter(({ summaryRow }) => summaryRow !== 'none');
if (columnsWithSummary.length) {
const summaryLookup = Object.fromEntries(
columnsWithSummary.map(({ summaryRowValue, summaryLabel, columnId }) => [
columnId,
summaryLabel === '' ? `${summaryRowValue}` : `${summaryLabel}: ${summaryRowValue}`,
])
);
return ({ columnId }: { columnId: string }) => {
const currentAlignment = alignments && alignments[columnId];
const alignmentClassName = `lnsTableCell--${currentAlignment}`;
const columnName =
columns.find(({ id }) => id === columnId)?.displayAsText?.replace(/ /g, '-') || columnId;
return summaryLookup[columnId] != null ? (
<div
className={`lnsTableCell ${alignmentClassName}`}
data-test-subj={`lnsDataTable-footer-${columnName}`}
>
{summaryLookup[columnId]}
</div>
) : null;
};
}
}, [columnConfig.columns, alignments, firstTable, columns]);
if (isEmpty) {
return (
<VisualizationContainer
className="lnsDataTableContainer"
reportTitle={props.args.title}
reportDescription={props.args.description}
>
<EmptyPlaceholder icon={LensIconChartDatatable} />
</VisualizationContainer>
);
}
const dataGridAriaLabel =
props.args.title ||
i18n.translate('xpack.lens.table.defaultAriaLabel', {
defaultMessage: 'Data table visualization',
});
return (
<VisualizationContainer
className="lnsDataTableContainer"
reportTitle={props.args.title}
reportDescription={props.args.description}
>
<DataContext.Provider
value={{
table: firstLocalTable,
rowHasRowClickTriggerActions: props.rowHasRowClickTriggerActions,
alignments,
minMaxByColumnId,
getColorForValue: props.paletteService.get(CUSTOM_PALETTE).getColorForValue!,
}}
>
<EuiDataGrid
aria-label={dataGridAriaLabel}
data-test-subj="lnsDataTable"
columns={columns}
columnVisibility={columnVisibility}
trailingControlColumns={trailingControlColumns}
rowCount={firstLocalTable.rows.length}
renderCellValue={renderCellValue}
gridStyle={gridStyle}
sorting={sorting}
onColumnResize={onColumnResize}
toolbarVisibility={false}
renderFooterCellValue={renderSummaryRow}
/>
</DataContext.Provider>
</VisualizationContainer>
);
};