@azrico/reactgrid 1.1.12 → 1.1.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/index.js +3 -21
- package/dist/src/example.js +95 -123
- package/dist/src/func/helpers.js +75 -80
- package/dist/src/index.js +5 -24
- package/dist/src/views/DataGrid.d.ts +2 -0
- package/dist/src/views/DataGrid.js +403 -344
- package/dist/src/views/index.js +3 -23
- package/index.ts +4 -4
- package/package.json +62 -62
- package/public/index.html +19 -19
- package/public/manifest.json +9 -9
- package/src/example.tsx +117 -117
- package/src/func/helpers.ts +74 -74
- package/src/index.tsx +11 -11
- package/src/themes/base_styles.css +26 -26
- package/src/themes/grid.css +20 -20
- package/src/views/DataGrid.tsx +552 -478
- package/src/views/index.ts +4 -4
- package/yarn-error.log +79 -79
- package/dist/src/themes/base_styles.css +0 -26
- package/dist/src/themes/grid.css +0 -20
package/src/views/DataGrid.tsx
CHANGED
|
@@ -1,478 +1,552 @@
|
|
|
1
|
-
import '@inovua/reactdatagrid-enterprise/index.css';
|
|
2
|
-
import '@inovua/reactdatagrid-enterprise/theme/default-dark.css';
|
|
3
|
-
import '@inovua/reactdatagrid-enterprise/theme/amber-dark.css';
|
|
4
|
-
import '../themes/base_styles.css';
|
|
5
|
-
import '../themes/grid.css';
|
|
6
|
-
|
|
7
|
-
import { ContextMenu, ConvertableToMenuItem } from '@azrico/controls';
|
|
8
|
-
import React, { useCallback, useState } from 'react';
|
|
9
|
-
import {
|
|
10
|
-
TypeColumn,
|
|
11
|
-
TypeDataGridProps,
|
|
12
|
-
TypeDataSource,
|
|
13
|
-
TypeFilterTypes,
|
|
14
|
-
TypeRowDetailsInfo,
|
|
15
|
-
TypeRowProps,
|
|
16
|
-
TypeRowSelection,
|
|
17
|
-
} from '@inovua/reactdatagrid-community/types';
|
|
18
|
-
import { fix_parent_child_relations, func_selection } from '../func/helpers';
|
|
19
|
-
|
|
20
|
-
import
|
|
21
|
-
import
|
|
22
|
-
import
|
|
23
|
-
import
|
|
24
|
-
import {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
export type
|
|
36
|
-
export type
|
|
37
|
-
export type
|
|
38
|
-
export type
|
|
39
|
-
export type
|
|
40
|
-
export type
|
|
41
|
-
export type
|
|
42
|
-
|
|
43
|
-
type
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
type
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
};
|
|
57
|
-
type
|
|
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
|
-
props
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
//
|
|
195
|
-
if (!
|
|
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
|
-
const
|
|
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
|
-
const
|
|
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
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
1
|
+
import '@inovua/reactdatagrid-enterprise/index.css';
|
|
2
|
+
import '@inovua/reactdatagrid-enterprise/theme/default-dark.css';
|
|
3
|
+
import '@inovua/reactdatagrid-enterprise/theme/amber-dark.css';
|
|
4
|
+
import '../themes/base_styles.css';
|
|
5
|
+
import '../themes/grid.css';
|
|
6
|
+
|
|
7
|
+
import { ContextMenu, ConvertableToMenuItem } from '@azrico/controls';
|
|
8
|
+
import React, { useCallback, useState } from 'react';
|
|
9
|
+
import {
|
|
10
|
+
TypeColumn,
|
|
11
|
+
TypeDataGridProps,
|
|
12
|
+
TypeDataSource,
|
|
13
|
+
TypeFilterTypes,
|
|
14
|
+
TypeRowDetailsInfo,
|
|
15
|
+
TypeRowProps,
|
|
16
|
+
TypeRowSelection,
|
|
17
|
+
} from '@inovua/reactdatagrid-community/types';
|
|
18
|
+
import { fix_parent_child_relations, func_selection } from '../func/helpers';
|
|
19
|
+
|
|
20
|
+
import BoolEditor from '@inovua/reactdatagrid-community/BoolEditor';
|
|
21
|
+
import NumericEditor from '@inovua/reactdatagrid-community/NumericEditor';
|
|
22
|
+
import ReactDataGrid from '@inovua/reactdatagrid-enterprise';
|
|
23
|
+
import SelectEditor from '@inovua/reactdatagrid-community/SelectEditor';
|
|
24
|
+
import { TypeOnSelectionChangeArg } from '@inovua/reactdatagrid-community/types/TypeDataGridProps';
|
|
25
|
+
import clsx from 'clsx';
|
|
26
|
+
import { object_equals } from '@azrico/object';
|
|
27
|
+
import { useResizeDetector } from 'react-resize-detector';
|
|
28
|
+
|
|
29
|
+
/* -------------------------------------------------------------------------- */
|
|
30
|
+
type ModifyFields<T, R> = Omit<T, keyof R> & R;
|
|
31
|
+
type MakeWriteable<Type> = {
|
|
32
|
+
-readonly [Key in keyof Type]: Type[Key];
|
|
33
|
+
};
|
|
34
|
+
/* -------------------------------------------------------------------------- */
|
|
35
|
+
export type ColumnDefinition = TypeColumn;
|
|
36
|
+
export type CellDefinition = any;
|
|
37
|
+
export type RowDefinition = any;
|
|
38
|
+
export type RowProps = TypeRowProps;
|
|
39
|
+
export type RowSelectionValue = TypeRowSelection;
|
|
40
|
+
export type DataRowDefinition = any;
|
|
41
|
+
export type FilterTypes = TypeFilterTypes;
|
|
42
|
+
export type DataSourceDefinition = TypeDataSource;
|
|
43
|
+
export type OnSelectionChangeFunc = (config: TypeOnSelectionChangeArg) => void;
|
|
44
|
+
export type RowDetailsFunc = (rowDetailsInfo: TypeRowDetailsInfo) => React.ReactNode;
|
|
45
|
+
/* -------------------------------------------------------------------------- */
|
|
46
|
+
type loose_column_names = {
|
|
47
|
+
key: string;
|
|
48
|
+
field: string;
|
|
49
|
+
columnName: string;
|
|
50
|
+
name: string;
|
|
51
|
+
};
|
|
52
|
+
type loose_column_fields = loose_column_names & {
|
|
53
|
+
title: string;
|
|
54
|
+
type: string;
|
|
55
|
+
items: any[];
|
|
56
|
+
};
|
|
57
|
+
export type LooseColumnDefinition = Partial<loose_column_fields & ColumnDefinition>;
|
|
58
|
+
//
|
|
59
|
+
type columnOrString = string | LooseColumnDefinition;
|
|
60
|
+
type customColumnData = {
|
|
61
|
+
name: string;
|
|
62
|
+
render: (cell: any, formatterParams, onRendered) => React.ReactNode | undefined | null;
|
|
63
|
+
};
|
|
64
|
+
type GridOptions = Partial<TypeDataGridProps>;
|
|
65
|
+
/* -------------------------------------------------------------------------- */
|
|
66
|
+
export type AzGridProps = {
|
|
67
|
+
columns?: columnOrString[];
|
|
68
|
+
dataSource?: any[] | ((args: any) => any[] | Promise<any[] | any>);
|
|
69
|
+
|
|
70
|
+
licenseKey?: string;
|
|
71
|
+
|
|
72
|
+
gridOptions?: GridOptions;
|
|
73
|
+
|
|
74
|
+
columnExtensions?: Array<{ name: string } & Partial<ColumnDefinition>>;
|
|
75
|
+
editableColumns?: string[];
|
|
76
|
+
leftColumns?: string[];
|
|
77
|
+
rightColumns?: string[];
|
|
78
|
+
customColumns?: customColumnData[];
|
|
79
|
+
|
|
80
|
+
contextMenuItems?: ConvertableToMenuItem[];
|
|
81
|
+
|
|
82
|
+
highlightedRow?: any;
|
|
83
|
+
RowDetail?: RowDetailsFunc;
|
|
84
|
+
|
|
85
|
+
selected?: RowSelectionValue;
|
|
86
|
+
/* -------------------------------------------------------------------------- */
|
|
87
|
+
onSelectionChange?: OnSelectionChangeFunc;
|
|
88
|
+
|
|
89
|
+
onRowClick?: (rowIndex: number, row: any) => void;
|
|
90
|
+
onRowDoubleClick?: (rowIndex: number, row: any) => void;
|
|
91
|
+
onLimitChange?: (v: number) => void;
|
|
92
|
+
onSkipChange?: (v: number) => void;
|
|
93
|
+
/* -------------------------------------------------------------------------- */
|
|
94
|
+
skip?: number;
|
|
95
|
+
limit?: number;
|
|
96
|
+
//
|
|
97
|
+
loading?: boolean;
|
|
98
|
+
dark?: boolean;
|
|
99
|
+
allowChildRows?: boolean;
|
|
100
|
+
looseCacheCheck?: boolean;
|
|
101
|
+
className?: string;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
type DataGridProps = ModifyFields<
|
|
105
|
+
AzGridProps,
|
|
106
|
+
{
|
|
107
|
+
columns: ColumnDefinition[];
|
|
108
|
+
dataSource: any;
|
|
109
|
+
}
|
|
110
|
+
>;
|
|
111
|
+
/* -------------------------------------------------------------------------- */
|
|
112
|
+
// const defaultfilterTypes: FilterTypes = {
|
|
113
|
+
// string: {
|
|
114
|
+
// type: 'string',
|
|
115
|
+
// emptyValue: '',
|
|
116
|
+
// operators: [
|
|
117
|
+
// {
|
|
118
|
+
// name: 'contains',
|
|
119
|
+
// fn: ({ value, filterValue, data }) => {
|
|
120
|
+
// console.log('fn');
|
|
121
|
+
// return !filterValue ? true : value.indexOf(filterValue) != -1;
|
|
122
|
+
// },
|
|
123
|
+
// },
|
|
124
|
+
// ],
|
|
125
|
+
// },
|
|
126
|
+
// };
|
|
127
|
+
const defaultOptions: GridOptions = {
|
|
128
|
+
//filterTypes: defaultfilterTypes,
|
|
129
|
+
remoteFilter: false,
|
|
130
|
+
remoteSort: false,
|
|
131
|
+
|
|
132
|
+
rowReorderColumn: false,
|
|
133
|
+
resizable: true,
|
|
134
|
+
rowIndexColumn: true,
|
|
135
|
+
};
|
|
136
|
+
const forcedOptions: GridOptions = {
|
|
137
|
+
idProperty: '__table_id',
|
|
138
|
+
nodesProperty: '__children',
|
|
139
|
+
};
|
|
140
|
+
const defaultColumnOptions: ColumnDefinition = {
|
|
141
|
+
draggable: false,
|
|
142
|
+
lockable: false,
|
|
143
|
+
};
|
|
144
|
+
const dynamic_option_names = ['selected', 'loading', 'limit', 'skip'];
|
|
145
|
+
/* -------------------------------------------------------------------------- */
|
|
146
|
+
export function AzGrid(props: AzGridProps) {
|
|
147
|
+
/* -------------------------------------------------------------------------- */
|
|
148
|
+
/* Columns */
|
|
149
|
+
/* -------------------------------------------------------------------------- */
|
|
150
|
+
const cached_columns = React.useMemo(() => {
|
|
151
|
+
return ApplyColumnData(props);
|
|
152
|
+
}, [
|
|
153
|
+
props.columns,
|
|
154
|
+
props.leftColumns,
|
|
155
|
+
props.rightColumns,
|
|
156
|
+
props.editableColumns,
|
|
157
|
+
props.columnExtensions,
|
|
158
|
+
props.customColumns,
|
|
159
|
+
]);
|
|
160
|
+
/* -------------------------------------------------------------------------- */
|
|
161
|
+
/* Row changes */
|
|
162
|
+
/* -------------------------------------------------------------------------- */
|
|
163
|
+
|
|
164
|
+
const cached_data = React.useRef<any>();
|
|
165
|
+
const last_cache_info = React.useRef<any>();
|
|
166
|
+
|
|
167
|
+
const dataSource = React.useCallback(
|
|
168
|
+
async (dt) => {
|
|
169
|
+
let result_data: any = undefined;
|
|
170
|
+
|
|
171
|
+
let isUsingCache = false;
|
|
172
|
+
|
|
173
|
+
const cacheInfo = { ds: props.dataSource, skip: dt.skip, limit: dt.limit };
|
|
174
|
+
//check if we can used cached data
|
|
175
|
+
//if the datasource is changed since last render, we should invalidate cache
|
|
176
|
+
if (!validateCache(last_cache_info.current, cacheInfo, props.looseCacheCheck)) {
|
|
177
|
+
cached_data.current = undefined;
|
|
178
|
+
last_cache_info.current = cacheInfo;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (cached_data.current) {
|
|
182
|
+
//cache is valid, use the cache
|
|
183
|
+
isUsingCache = true;
|
|
184
|
+
result_data = cached_data.current;
|
|
185
|
+
} else {
|
|
186
|
+
//cache is invalid, use the datasource again
|
|
187
|
+
|
|
188
|
+
isUsingCache = false;
|
|
189
|
+
if (typeof props.dataSource === 'function') {
|
|
190
|
+
result_data = await props.dataSource(dt);
|
|
191
|
+
} else result_data = props.dataSource;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
//making sure the datasource is a valid object :
|
|
195
|
+
if (!result_data) result_data = [];
|
|
196
|
+
if (Array.isArray(result_data) || typeof result_data !== 'object')
|
|
197
|
+
result_data = { data: result_data };
|
|
198
|
+
|
|
199
|
+
if (!Array.isArray(result_data.data)) result_data.data = [result_data.data];
|
|
200
|
+
|
|
201
|
+
//no need to fix parent/child when using cache (it should be already fixed)
|
|
202
|
+
if (!isUsingCache && props.allowChildRows)
|
|
203
|
+
result_data.data = fix_parent_child_relations(result_data.data);
|
|
204
|
+
|
|
205
|
+
if (!result_data.count) result_data.count = (result_data.data as Array<any>).length;
|
|
206
|
+
|
|
207
|
+
//update cache
|
|
208
|
+
cached_data.current = result_data;
|
|
209
|
+
return result_data;
|
|
210
|
+
},
|
|
211
|
+
[props.dataSource]
|
|
212
|
+
);
|
|
213
|
+
|
|
214
|
+
return (
|
|
215
|
+
<DataGrid
|
|
216
|
+
{...props}
|
|
217
|
+
columns={cached_columns}
|
|
218
|
+
dataSource={dataSource}
|
|
219
|
+
highlightedRow={props.highlightedRow}
|
|
220
|
+
/>
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/* -------------------------------------------------------------------------- */
|
|
225
|
+
function DataGrid(props: DataGridProps) {
|
|
226
|
+
const { columns, dataSource, dark, contextMenuItems } = props;
|
|
227
|
+
|
|
228
|
+
/* -------------------------------------------------------------------------- */
|
|
229
|
+
const gridRef = React.useRef<any>(null);
|
|
230
|
+
const click_data = React.useRef<any>(null);
|
|
231
|
+
|
|
232
|
+
const [menuAnchorEl, set_menuAnchorEl] = React.useState<any>(null);
|
|
233
|
+
const [menuRow, set_menuRow] = React.useState<any>(null);
|
|
234
|
+
|
|
235
|
+
React.useEffect(() => {
|
|
236
|
+
click_data.current = {};
|
|
237
|
+
}, [dataSource]);
|
|
238
|
+
|
|
239
|
+
/* --------------------------------- events --------------------------------- */
|
|
240
|
+
|
|
241
|
+
const onRowContextMenu = React.useCallback((rowProps, event) => {
|
|
242
|
+
if (func_selection.has_selection()) return;
|
|
243
|
+
update_menuRow(rowProps.data);
|
|
244
|
+
update_menuAnchorEl(func_selection.getVirtualElement(event.clientX, event.clientY));
|
|
245
|
+
event.preventDefault();
|
|
246
|
+
}, []);
|
|
247
|
+
const onRowClick = React.useCallback((rowProps, event) => {
|
|
248
|
+
const nowDate = Date.now();
|
|
249
|
+
const lastclick: number = click_data.current[get_row_id(rowProps)];
|
|
250
|
+
if (lastclick) {
|
|
251
|
+
const diffTime = Math.abs(nowDate - lastclick);
|
|
252
|
+
if (diffTime < 400) {
|
|
253
|
+
props.onRowDoubleClick &&
|
|
254
|
+
props.onRowDoubleClick(rowProps.rowIndex, rowProps.data);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
click_data.current[get_row_id(rowProps)] = nowDate;
|
|
259
|
+
props.onRowClick && props.onRowClick(rowProps.rowIndex, rowProps.data);
|
|
260
|
+
}, []);
|
|
261
|
+
/* ------------------------------- ContextMenu ------------------------------ */
|
|
262
|
+
|
|
263
|
+
const update_menuAnchorEl = React.useCallback((el) => {
|
|
264
|
+
if (el) {
|
|
265
|
+
set_menuAnchorEl(el);
|
|
266
|
+
} else set_menuAnchorEl(null);
|
|
267
|
+
}, []);
|
|
268
|
+
const update_menuRow = React.useCallback((r) => {
|
|
269
|
+
if (r) {
|
|
270
|
+
set_menuRow(r);
|
|
271
|
+
} else set_menuRow(null);
|
|
272
|
+
}, []);
|
|
273
|
+
|
|
274
|
+
/* -------------------------------------------------------------------------- */
|
|
275
|
+
/* Create the grid */
|
|
276
|
+
/* -------------------------------------------------------------------------- */
|
|
277
|
+
|
|
278
|
+
const base_gridOptions = React.useMemo<GridOptions>(() => {
|
|
279
|
+
const firstcolumn = columns[0];
|
|
280
|
+
const licenseKey = props.licenseKey || get_default_key();
|
|
281
|
+
const events: GridOptions = {
|
|
282
|
+
onRowClick: onRowClick,
|
|
283
|
+
onSkipChange: props.onSkipChange,
|
|
284
|
+
onLimitChange: props.onLimitChange,
|
|
285
|
+
onRowContextMenu: onRowContextMenu,
|
|
286
|
+
};
|
|
287
|
+
const extra_ops: GridOptions = {
|
|
288
|
+
onSelectionChange: props.onSelectionChange,
|
|
289
|
+
licenseKey: licenseKey,
|
|
290
|
+
treeColumn: props.allowChildRows ? firstcolumn.name : undefined,
|
|
291
|
+
renderRowDetails: props.RowDetail,
|
|
292
|
+
theme: dark ? 'amber-dark' : 'default-light',
|
|
293
|
+
};
|
|
294
|
+
return { ...defaultOptions, ...events, ...extra_ops };
|
|
295
|
+
}, [columns, props.allowChildRows, props.RowDetail]);
|
|
296
|
+
|
|
297
|
+
const static_gridOptions = React.useMemo<GridOptions>(() => {
|
|
298
|
+
return { ...base_gridOptions, ...props.gridOptions, ...forcedOptions };
|
|
299
|
+
}, [props.gridOptions, base_gridOptions, forcedOptions]);
|
|
300
|
+
|
|
301
|
+
const dynamic_gridOptions = React.useMemo<GridOptions>(
|
|
302
|
+
() => {
|
|
303
|
+
const res = {};
|
|
304
|
+
dynamic_option_names.map((r) => {
|
|
305
|
+
res[r] = props[r];
|
|
306
|
+
});
|
|
307
|
+
return res;
|
|
308
|
+
},
|
|
309
|
+
dynamic_option_names.map((r) => props[r])
|
|
310
|
+
);
|
|
311
|
+
|
|
312
|
+
// const { width, height, ref: resize_ref } = useResizeDetector();
|
|
313
|
+
|
|
314
|
+
return (
|
|
315
|
+
<div className={clsx(props.className, 'azgrid-nooverflow', 'azgrid-filler')}>
|
|
316
|
+
<ReactDataGrid
|
|
317
|
+
{...static_gridOptions}
|
|
318
|
+
{...dynamic_gridOptions}
|
|
319
|
+
handle={(ref) => (gridRef.current = ref ? ref.current : null)}
|
|
320
|
+
className={'azgrid-filler'}
|
|
321
|
+
columns={columns}
|
|
322
|
+
dataSource={dataSource}
|
|
323
|
+
/>
|
|
324
|
+
<ContextMenu
|
|
325
|
+
menuAnchorEl={menuAnchorEl}
|
|
326
|
+
set_menuAnchorEl={update_menuAnchorEl}
|
|
327
|
+
menuRow={menuRow}
|
|
328
|
+
items={contextMenuItems}
|
|
329
|
+
/>
|
|
330
|
+
</div>
|
|
331
|
+
);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/* -------------------------------------------------------------------------- */
|
|
335
|
+
/* -------------------------------------------------------------------------- */
|
|
336
|
+
/* -------------------------------------------------------------------------- */
|
|
337
|
+
/* -------------------------------------------------------------------------- */
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* merges together all different props for columns
|
|
341
|
+
* to build a single final column data array
|
|
342
|
+
* @param props
|
|
343
|
+
* @returns
|
|
344
|
+
*/
|
|
345
|
+
function ApplyColumnData(props: any) {
|
|
346
|
+
const final_columns: MakeWriteable<ColumnDefinition>[] = [];
|
|
347
|
+
let mcols = props.columns || [];
|
|
348
|
+
let mcolExts = props.columnExtensions || [];
|
|
349
|
+
let rightColumns = props.rightColumns || [];
|
|
350
|
+
let leftColumns = props.leftColumns || [];
|
|
351
|
+
let editableColumns = props.editableColumns || [];
|
|
352
|
+
let customColumns = props.customColumns || [];
|
|
353
|
+
|
|
354
|
+
if (!Array.isArray(mcols)) return [];
|
|
355
|
+
/* ------------------------------- fix columns ------------------------------ */
|
|
356
|
+
|
|
357
|
+
mcols
|
|
358
|
+
.map((r) => {
|
|
359
|
+
if (r == null) return null;
|
|
360
|
+
if (typeof r === 'string') return { field: r, header: r };
|
|
361
|
+
if (typeof r === 'object') {
|
|
362
|
+
let final_cl: MakeWriteable<ColumnDefinition> = {
|
|
363
|
+
name: get_col_name(r),
|
|
364
|
+
header: get_col_header(r),
|
|
365
|
+
width: r.width || undefined,
|
|
366
|
+
};
|
|
367
|
+
final_cl = {
|
|
368
|
+
...defaultColumnOptions,
|
|
369
|
+
...r,
|
|
370
|
+
...final_cl,
|
|
371
|
+
};
|
|
372
|
+
return final_cl;
|
|
373
|
+
}
|
|
374
|
+
return null;
|
|
375
|
+
})
|
|
376
|
+
.forEach((r) => {
|
|
377
|
+
if (r != null) final_columns.push(r);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
/* ---------------------------- apply extensions ---------------------------- */
|
|
381
|
+
if (mcolExts && Array.isArray(mcolExts))
|
|
382
|
+
for (let index = 0; index < mcolExts.length; index++) {
|
|
383
|
+
const ext = mcolExts[index];
|
|
384
|
+
const ext_cl = get_col_name(ext);
|
|
385
|
+
const mcol = final_columns.find((s) => get_col_name(s) === ext_cl);
|
|
386
|
+
if (mcol) {
|
|
387
|
+
for (const key in ext) {
|
|
388
|
+
if (!mcol[key] && key != 'name') mcol[key] = ext[key];
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
// if (ext.widthGrow) mcol.widthGrow = ext.widthGrow;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/* ------------------------------- frozen cols ------------------------------ */
|
|
395
|
+
if (Array.isArray(leftColumns)) {
|
|
396
|
+
for (let index = 0; index < leftColumns.length; index++) {
|
|
397
|
+
const r = get_col_name(leftColumns[index]);
|
|
398
|
+
const mcol = final_columns.find((s) => get_col_name(s) === r);
|
|
399
|
+
if (mcol) {
|
|
400
|
+
mcol.locked = 'start';
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
if (Array.isArray(rightColumns)) {
|
|
405
|
+
for (let index = 0; index < rightColumns.length; index++) {
|
|
406
|
+
const r = get_col_name(rightColumns[index]);
|
|
407
|
+
const mcol = final_columns.find((s) => get_col_name(s) === r);
|
|
408
|
+
if (mcol) {
|
|
409
|
+
mcol.locked = 'end';
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
/* -------------------------------- ediatable ------------------------------- */
|
|
414
|
+
if (Array.isArray(editableColumns)) {
|
|
415
|
+
for (let index = 0; index < editableColumns.length; index++) {
|
|
416
|
+
const r = get_col_name(editableColumns[index]);
|
|
417
|
+
const mcol = final_columns.find((s) => get_col_name(s) === r);
|
|
418
|
+
if (mcol) {
|
|
419
|
+
// mcol.editor = editorWrapper;
|
|
420
|
+
// mcol.editorOptions = {
|
|
421
|
+
// editOnClick: true,
|
|
422
|
+
// };
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
for (let index = 0; index < final_columns.length; index++) {
|
|
427
|
+
const col: ColumnDefinition = final_columns[index];
|
|
428
|
+
|
|
429
|
+
if (col.type) {
|
|
430
|
+
switch (col.type) {
|
|
431
|
+
case 'combo':
|
|
432
|
+
case 'select':
|
|
433
|
+
col.editor = SelectEditor;
|
|
434
|
+
break;
|
|
435
|
+
case 'number':
|
|
436
|
+
case 'numberic':
|
|
437
|
+
col.editor = NumericEditor;
|
|
438
|
+
break;
|
|
439
|
+
case 'truefalse':
|
|
440
|
+
case 'bool':
|
|
441
|
+
col.editor = BoolEditor;
|
|
442
|
+
break;
|
|
443
|
+
}
|
|
444
|
+
delete col.type;
|
|
445
|
+
}
|
|
446
|
+
if (!col.editorProps) col.editorProps = {};
|
|
447
|
+
|
|
448
|
+
if (col.items) {
|
|
449
|
+
if (!col.editorProps.idProperty) col.editorProps.idProperty = 'id';
|
|
450
|
+
if (!col.editorProps.collapseOnSelect) col.editorProps.collapseOnSelect = true;
|
|
451
|
+
col.editorProps.dataSource = to_datasource(col.items, col.editorProps.idProperty);
|
|
452
|
+
|
|
453
|
+
delete col.items;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/* -------------------------------------------------------------------------- */
|
|
458
|
+
if (Array.isArray(customColumns)) {
|
|
459
|
+
for (let index = 0; index < customColumns.length; index++) {
|
|
460
|
+
const r = customColumns[index];
|
|
461
|
+
const rn = get_col_name(r);
|
|
462
|
+
const mcol = final_columns.find((s) => get_col_name(s) === rn);
|
|
463
|
+
if (mcol) {
|
|
464
|
+
//@ts-ignore
|
|
465
|
+
mcol.render = r.render;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/* ------------------------------ apply renames ----------------------------- */
|
|
471
|
+
for (let index = 0; index < final_columns.length; index++) {
|
|
472
|
+
const col = final_columns[index];
|
|
473
|
+
if (!col.name) {
|
|
474
|
+
col.name = get_col_name(col);
|
|
475
|
+
delete col.field;
|
|
476
|
+
delete col.key;
|
|
477
|
+
delete col.columnName;
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
/* -------------------------------------------------------------------------- */
|
|
481
|
+
|
|
482
|
+
return final_columns;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function to_datasource(arr: any[], idProperty: string) {
|
|
486
|
+
const res: any[] = [];
|
|
487
|
+
let index = 0;
|
|
488
|
+
if (Array.isArray(arr)) {
|
|
489
|
+
arr.map((s: any) => {
|
|
490
|
+
let elem = {};
|
|
491
|
+
elem[idProperty] = '_cid_' + index;
|
|
492
|
+
elem = { ...elem, ...s };
|
|
493
|
+
res.push(elem);
|
|
494
|
+
index++;
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
return res;
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Gets name of a column from its column object
|
|
501
|
+
* @param obj
|
|
502
|
+
* @returns
|
|
503
|
+
*/
|
|
504
|
+
function get_col_name(obj: any): string {
|
|
505
|
+
if (!obj) return 'default';
|
|
506
|
+
if (typeof obj == 'string') return obj;
|
|
507
|
+
if (typeof obj != 'object') return 'default';
|
|
508
|
+
return obj.name || obj.field || obj.key || obj.columnName || 'default';
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* Gets header of a column
|
|
512
|
+
* @param obj
|
|
513
|
+
* @returns
|
|
514
|
+
*/
|
|
515
|
+
function get_col_header(obj: any): string {
|
|
516
|
+
if (!obj) return 'default';
|
|
517
|
+
if (typeof obj == 'string') return obj;
|
|
518
|
+
if (typeof obj != 'object') return 'default';
|
|
519
|
+
return obj.header || obj.title || get_col_name(obj);
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Gets name of a column from its column object
|
|
523
|
+
* @param obj
|
|
524
|
+
* @returns
|
|
525
|
+
*/
|
|
526
|
+
function get_row_id(obj: any): any {
|
|
527
|
+
if (!obj) return 'default';
|
|
528
|
+
if (typeof obj == 'string') return obj;
|
|
529
|
+
if (typeof obj != 'object') return 'default';
|
|
530
|
+
return obj.__table_id || obj.id || 'default';
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Gets default activation key for grid plugin
|
|
535
|
+
* @returns
|
|
536
|
+
*/
|
|
537
|
+
function get_default_key() {
|
|
538
|
+
return 'ExpiryDate=2999-01-01,Z=1040577552';
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
//check if two cache objects are the same
|
|
542
|
+
function validateCache(current: any, old: any, looseCheck = false) {
|
|
543
|
+
if (!old || !object_equals(current, old)) return false;
|
|
544
|
+
|
|
545
|
+
if (looseCheck) return true;
|
|
546
|
+
|
|
547
|
+
if (current.ds != old.ds) return false;
|
|
548
|
+
return true;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/* -------------------------------------------------------------------------- */
|
|
552
|
+
export default AzGrid;
|