-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathAgGrid.react.js
782 lines (671 loc) · 21.7 KB
/
AgGrid.react.js
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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
import PropTypes from 'prop-types';
import LazyLoader from '../LazyLoader';
import React, {Component, lazy, Suspense} from 'react';
const RealAgGrid = lazy(LazyLoader.agGrid);
const RealAgGridEnterprise = lazy(LazyLoader.agGridEnterprise);
function getGrid(enable) {
return enable ? RealAgGridEnterprise : RealAgGrid;
}
/**
* Dash interface to AG Grid, a powerful tabular data component.
*/
export default class DashAgGrid extends Component {
constructor(props) {
super(props);
this.state = {
mounted: false,
rowTransaction: null,
};
this.buildArray = this.buildArray.bind(this);
}
buildArray(arr1, arr2) {
if (arr1) {
if (!arr1.includes(arr2)) {
return [...arr1, arr2];
}
return arr1;
}
return [JSON.parse(JSON.stringify(arr2))];
}
UNSAFE_componentWillReceiveProps(nextProps) {
if (this.props.rowTransaction && !this.state.mounted) {
if (nextProps.rowTransaction !== this.props.rowTransaction) {
this.setState({
rowTransaction: this.buildArray(
this.state.rowTransaction,
this.props.rowTransaction
),
});
}
}
}
render() {
const {enableEnterpriseModules} = this.props;
const RealComponent = getGrid(enableEnterpriseModules);
return (
<Suspense fallback={null}>
<RealComponent parentState={this.state} {...this.props} />
</Suspense>
);
}
}
DashAgGrid.defaultProps = {
resetColumnState: false,
exportDataAsCsv: false,
selectAll: false,
deselectAll: false,
enableEnterpriseModules: false,
updateColumnState: false,
persisted_props: ['selectedRows'],
persistence_type: 'local',
suppressDragLeaveHidesColumns: true,
dangerously_allow_code: false,
rowModelType: 'clientSide',
dashGridOptions: {},
filterModel: {},
paginationGoTo: null,
selectedRows: [],
};
DashAgGrid.propTypes = {
/********************************
* DASH PROPS
*******************************/
/**
* The ID used to identify this component in Dash callbacks.
*/
id: PropTypes.string,
/**
* Dash-assigned callback that gets fired when the input changes
*/
setProps: PropTypes.func,
/**
* The CSS style for the component
*/
style: PropTypes.object,
/**
* The class for the ag-grid. Can specify the ag-grid theme here.
*/
className: PropTypes.string,
/**
* Used to allow user interactions in this component to be persisted when
* the component - or the page - is refreshed. If `persisted` is truthy and
* hasn't changed from its previous value, a `value` that the user has
* changed while using the app will keep that change, as long as
* the new `value` also matches what was given originally.
* Used in conjunction with `persistence_type`.
*/
persistence: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.string,
PropTypes.number,
]),
/**
* Properties whose user interactions will persist after refreshing the
* component or the page.
*/
persisted_props: PropTypes.arrayOf(PropTypes.string),
/**
* Where persisted user changes will be stored:
* memory: only kept in memory, reset on page refresh.
* local: window.localStorage, data is kept after the browser quit.
* session: window.sessionStorage, data is cleared once the browser quit.
*/
persistence_type: PropTypes.oneOf(['local', 'session', 'memory']),
/**
* Allow strings containing JavaScript code or HTML in certain props.
* If your app stores Dash layouts for later retrieval this is dangerous
* because it can lead to cross-site-scripting attacks.
*/
dangerously_allow_code: PropTypes.bool,
/********************************
* CUSTOM PROPS
*******************************/
/**
* If true, the internal method resetColumnState() will be called
*/
resetColumnState: PropTypes.bool,
/**
* If true, the internal method exportDataAsCsv() will be called
*/
exportDataAsCsv: PropTypes.bool,
/**
* Set to true to cause all rows to be selected,
* Or pass an object of options for which rows to select.
* Currently supports `filtered`, set to true to only select filtered rows.
*/
selectAll: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.exact({
filtered: PropTypes.bool,
}),
]),
/**
* If true, the internal method deselectAll() will be called
*/
deselectAll: PropTypes.bool,
/**
* If true, the internal method updateColumnState() will be called
*/
updateColumnState: PropTypes.bool,
/**
* If true, the internal method deleteSelectedRows() will be called
*/
deleteSelectedRows: PropTypes.bool,
/**
* If true, the internal method rowTransaction() will be used,
* if async provided as false, applyTransaction() will be called, else applyTransactionAsync()
*/
rowTransaction: PropTypes.shape({
async: PropTypes.bool,
add: PropTypes.array,
update: PropTypes.array,
remove: PropTypes.array,
addIndex: PropTypes.number,
}),
/**
* This is required for change detection in rowData
*/
getRowId: PropTypes.string,
/**
* Current state of the columns
*/
columnState: PropTypes.array,
/**
* Object with properties to pass to the exportDataAsCsv() method
*/
csvExportParams: PropTypes.shape({
/**
* Delimiter to insert between cell values.
*/
columnSeparator: PropTypes.string,
/**
* Pass true to insert the value into the CSV file without escaping. In this case it is your responsibility to ensure that no cells contain the columnSeparator character.
*/
suppressQuotes: PropTypes.bool,
/**
* Content to put at the top of the file export. A 2D array of CsvCell objects.
*/
prependContent: PropTypes.string,
/**
* Content to put at the bottom of the file export.
*/
appendContent: PropTypes.string,
/**
* If true, all columns will be exported in the order they appear in the columnDefs.
*/
allColumns: PropTypes.bool,
/**
* Provide a list (an array) of column keys or Column objects if you want to export specific columns.
*/
columnKeys: PropTypes.arrayOf(PropTypes.string),
/**
* String to use as the file name
*/
fileName: PropTypes.string,
/**
* Export only selected rows.
*/
onlySelected: PropTypes.bool,
/**
* Only export selected rows including other pages (only makes sense when using pagination).
*/
onlySelectedAllPages: PropTypes.bool,
/**
* Set to true to skip include header column groups.
*/
skipColumnGroupHeaders: PropTypes.bool,
/**
* Set to true if you don't want to export column headers.
*/
skipColumnHeaders: PropTypes.bool,
/**
* Set to true to skip row group headers if grouping rows. Only relevant when grouping rows.
*/
skipRowGroups: PropTypes.bool,
/**
* Set to true to suppress exporting rows pinned to the top of the grid.
*/
skipPinnedTop: PropTypes.bool,
/**
* Set to true to suppress exporting rows pinned to the bottom of the grid.
*/
skipPinnedBottom: PropTypes.bool,
}),
/**
* Size the columns autoSize changes the column sizes to fit the column's content,
* sizeToFit changes the column sizes to fit the width of the table
* responsiveSizeToFit changes the column sizes to fit the width of the table and also resizing upon grid or column changes
* and null bypasses the altering of the column widths
*/
columnSize: PropTypes.oneOf([
'sizeToFit',
'autoSize',
'responsiveSizeToFit',
null,
]),
/**
* Options to customize the columnSize operation.
* autoSize calls either autoSizeColumns or autoSizeAllColumns, see:
* https://www.ag-grid.com/react-data-grid/column-sizing/#autosize-column-api,
* and sizeToFit and responsiveSizeToFit call sizeColumnsToFit, see:
* https://www.ag-grid.com/react-data-grid/column-sizing/#size-columns-to-fit
*/
columnSizeOptions: PropTypes.exact({
/**
* for (responsive)sizeToFit: per-column minimum and maximum width, in pixels.
*/
columnLimits: PropTypes.arrayOf(
PropTypes.exact({
key: PropTypes.string,
minWidth: PropTypes.number,
maxWidth: PropTypes.number,
})
),
/**
* for (responsive)sizeToFit: default minimum width, in pixels, if not overridden by columnLimits
*/
defaultMinWidth: PropTypes.number,
/**
* for (responsive)sizeToFit: default maximum width, in pixels, if not overridden by columnLimits
*/
defaultMaxWidth: PropTypes.number,
/**
* for autoSize: list of column keys to autosize. If omitted, all columns will be autosized.
*/
keys: PropTypes.arrayOf(PropTypes.string),
/**
* for autoSize: If skipHeader=True, the header won't be included when calculating the column widths.
* default: False
*/
skipHeader: PropTypes.bool,
}),
/**
* Object used to perform the row styling. See AG-Grid Row Style.
*/
getRowStyle: PropTypes.shape({
styleConditions: PropTypes.arrayOf(
PropTypes.shape({
condition: PropTypes.string.isRequired,
style: PropTypes.object.isRequired,
})
),
defaultStyle: PropTypes.object,
}),
/**
* Infinite Scroll, Datasource interface
* See https://www.ag-grid.com/react-grid/infinite-scrolling/#datasource-interface
*/
getRowsRequest: PropTypes.shape({
/**
* The first row index to get.
*/
startRow: PropTypes.number,
/**
* The first row index to NOT get.
*/
endRow: PropTypes.number,
/**
* If sorting, what the sort model is
*/
sortModel: PropTypes.arrayOf(PropTypes.object),
/**
* If filtering, what the filter model is
*/
filterModel: PropTypes.object,
/**
* The grid context object
*/
context: PropTypes.any,
/**
* Callback to call when the request is successful.
*/
successCallback: PropTypes.func,
/**
* Callback to call when the request fails.
*/
failCallback: PropTypes.func,
}),
/**
* If in pagination mode, this will be populated with info from the pagination API:
* https://www.ag-grid.com/react-data-grid/grid-api/#reference-pagination
*/
paginationInfo: PropTypes.exact({
isLastPageFound: PropTypes.bool,
pageSize: PropTypes.number,
currentPage: PropTypes.number,
totalPages: PropTypes.number,
rowCount: PropTypes.number,
}),
/**
* If in pagination mode, this will navigate to: ['next', 'previous', 'last', 'first', number]
* https://www.ag-grid.com/react-data-grid/grid-api/#reference-pagination
*/
paginationGoTo: PropTypes.oneOfType([
PropTypes.oneOf(['first', 'last', 'next', 'previous', null]),
PropTypes.number,
]),
/**
* If filtering client-side rowModel, what the filter model is.
* Passing a model back to this prop will apply it to the grid.
*/
filterModel: PropTypes.object,
/**
* Request from Dash AgGrid when suppressCallback is disabled and a user opens a row with a detail grid
*/
getDetailRequest: PropTypes.shape({
/**
* Details about the row that was opened.
*/
data: PropTypes.any,
/**
* Datetime representing when the grid was requested.
*/
requestTime: PropTypes.any,
}),
/**
* RowData to populate the detail grid when callbacks are used to populate
*/
getDetailResponse: PropTypes.arrayOf(PropTypes.object),
/**
* Special prop to allow feedback from cell renderer to the grid.
*/
cellRendererData: PropTypes.shape({
/**
* Value set from the function
*/
value: PropTypes.any,
/**
* Column ID from where the event was fired
*/
colId: PropTypes.string,
/**
* Row Index from the grid, this is associated with the row count
*/
rowIndex: PropTypes.number,
/**
* Row Id from the grid, this could be a number automatically, or set via getRowId
*/
rowId: PropTypes.any,
/**
* Timestamp of when the event was fired
*/
timestamp: PropTypes.any,
}),
/**
* Serverside model data response object.
* See https://www.ag-grid.com/react-grid/server-side-model-datasource/
*/
getRowsResponse: PropTypes.shape({
/**
* Data retreived from the server
*/
rowData: PropTypes.arrayOf(PropTypes.object),
/**
* Current row count, if known
*/
rowCount: PropTypes.number,
/**
* Any extra info for the grid to associate with this load
*/
storeInfo: PropTypes.any,
}),
/**
* License key for ag-grid enterprise. If using Enterprise modules,
* enableEnterpriseModules must also be true.
*/
licenseKey: PropTypes.string,
/**
* If True, enable ag-grid Enterprise modules. Recommended to use with licenseKey.
*/
enableEnterpriseModules: PropTypes.bool,
/**
* The rowData in the grid after inline filters are applied.
*/
virtualRowData: PropTypes.arrayOf(PropTypes.object),
/**
* Scrolls to a specific position.
*/
scrollTo: PropTypes.shape({
/**
* rowIndex, typically a row number.
*/
rowIndex: PropTypes.number,
/**
* Id of the row to scroll to.
*/
rowId: PropTypes.string,
/**
* Data of the row to scroll to.
*/
data: PropTypes.object,
/**
* Position of the row in the grid after scrolling. Default `top`.
*/
rowPosition: PropTypes.oneOf(['top', 'bottom', 'middle']),
/**
* Column to scroll to, must be equal to one `field` in `columnDefs`.
*/
column: PropTypes.string,
/**
* Position of the column in the grid after scrolling. Default `auto`.
*/
columnPosition: PropTypes.oneOf(['auto', 'start', 'middle', 'end']),
}),
/**
* Object of Eventlisteners to add upon grid ready. These listeners are only added upon grid ready.
* To add or remove an event listener after this point, please utilize the `getApi` or `getApiAsync` methods.
*/
eventListeners: PropTypes.objectOf(PropTypes.array),
/********************************
* GRID PROPS
*******************************/
/**
* Array of Column Definitions.
*/
columnDefs: PropTypes.arrayOf(PropTypes.object),
/**
* A default column definition.
*/
defaultColDef: PropTypes.object,
/**
* Sets the Row Model type.
* Default Value: 'clientSide'
*/
rowModelType: PropTypes.oneOf([
'clientSide',
'infinite',
'viewport',
'serverSide',
]),
/**
* (Client-Side Row Model only) Set the data to be displayed as rows in the grid.
*/
rowData: PropTypes.arrayOf(PropTypes.object),
/**
* Used to enable Master Detail. See Enabling Master Detail.
* Default Value: false
*/
masterDetail: PropTypes.bool,
/**
* Specifies the params to be used by the default detail Cell Renderer. See Detail
* Grids.
*/
detailCellRendererParams: PropTypes.shape({
/**
* Grid options for detail grid in master-detail view.
*/
detailGridOptions: PropTypes.any,
/**
* Column name where detail grid data is located in main dataset, for master-detail view.
*/
detailColName: PropTypes.string,
/**
* Default: true. If true, suppresses the Dash callback in favor of using the data embedded in rowData at the given detailColName.
*/
suppressCallback: PropTypes.bool,
}),
/**
* The style to give a particular row. See Row Style.
*/
rowStyle: PropTypes.object,
/**
* The class to give a particular row. See Row Class.
*/
rowClass: PropTypes.string,
/**
* Rules which can be applied to include certain CSS classes. See Row Class Rules.
*/
rowClassRules: PropTypes.object,
/**
* If true, when you drag a column out of the grid (e.g. to the group zone) the column
* is not hidden.
*/
suppressDragLeaveHidesColumns: PropTypes.bool,
/********************************
* EVENT PROPS
*******************************/
/**
* Cell is clicked.
*/
cellClicked: PropTypes.shape({
/**
* value of the clicked cell
*/
value: PropTypes.any,
/**
* column where the cell was clicked
*/
colId: PropTypes.any,
/**
* rowIndex, typically a row number
*/
rowIndex: PropTypes.number,
/**
* Row Id from the grid, this could be a number automatically, or set via getRowId
*/
rowId: PropTypes.any,
/**
* timestamp of last action
*/
timestamp: PropTypes.any,
}),
/**
* Cell is double clicked.
*/
cellDoubleClicked: PropTypes.shape({
/**
* value of the double-clicked cell
*/
value: PropTypes.any,
/**
* column where the cell was double-clicked
*/
colId: PropTypes.any,
/**
* rowIndex, typically a row number
*/
rowIndex: PropTypes.number,
/**
* Row Id from the grid, this could be a number automatically, or set via getRowId
*/
rowId: PropTypes.any,
/**
* timestamp of last action
*/
timestamp: PropTypes.any,
}),
/**
* The actively selected rows from the grid (may include filtered rows)
* Can take one of three forms:
* (1) an array of row objects - if you have defined `getRowId`, you only need the fields it uses.
* (2) an object containing `function` with a function string
* - see: https://www.ag-grid.com/react-data-grid/row-selection/#example-using-foreachnode (selectAllAmerican function)
* (3) an object containing `ids` with a list of row IDs
*/
selectedRows: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.object),
PropTypes.exact({function: PropTypes.string.isRequired}),
PropTypes.exact({ids: PropTypes.arrayOf(PropTypes.string).isRequired}),
]),
/**
* Value has changed after editing.
*/
cellValueChanged: PropTypes.arrayOf(
PropTypes.shape({
/**
* rowIndex, typically a row number
*/
rowIndex: PropTypes.number,
/**
* Row Id from the grid, this could be a number automatically, or set via getRowId
*/
rowId: PropTypes.any,
/**
* data, data object from the row
*/
data: PropTypes.object,
/**
* old value of the cell
*/
oldValue: PropTypes.any,
/**
* new value of the cell
*/
newValue: PropTypes.any,
/**
* column where the cell was changed
*/
colId: PropTypes.any,
/**
* Timestamp of when the event was fired
*/
timestamp: PropTypes.any,
})
),
/**
* Other ag-grid options
*/
dashGridOptions: PropTypes.object,
};
export const propTypes = DashAgGrid.propTypes;
export const defaultProps = DashAgGrid.defaultProps;
export const apiGetters = {};
const _get = (flavor) => (id) => {
// optional chaining so before the fragment exists it'll just return undefined
// which does the right thing because clearly no grid is initialized yet!
const api = apiGetters[flavor]?.(id);
if (api) {
return api;
}
throw new Error(
`no grid found, or grid is not initialized yet, with id: ${id}`
);
};
const _getAsync = (flavor) => async (id) => {
// optional chaining so before the fragment exists it'll just return undefined
// which does the right thing because clearly no grid is initialized yet!
var api = apiGetters[flavor]?.(id);
const delay = (ms) => new Promise((res) => setTimeout(res, ms));
const startTime = Date.now();
const maxDelay = 120000;
const maxIncrement = 1000;
let pause = 1;
const increase = 1.5;
while (!api) {
await delay(pause);
pause *= increase;
pause = Math.min(pause, maxIncrement);
api = apiGetters[flavor]?.(id);
if (Date.now() > startTime + maxDelay) {
break;
}
}
if (api) {
return api;
}
throw new Error(
`no grid found, or grid is not initialized yet, with id: ${id}`
);
};
export const getApi = _get('getApi');
export const getApiAsync = _getAsync('getApi');