-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathagGridReactUi.tsx
265 lines (217 loc) · 10 KB
/
agGridReactUi.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
import { BaseComponentWrapper, CtrlsService, ColumnApi, ComponentType, ComponentUtil, Context, FrameworkComponentWrapper, GridApi, GridCoreCreator, GridOptions, GridParams, WrappableInterface, _ } from '@ag-grid-community/core';
import React, { Component } from 'react';
import { AgGridColumn } from '../shared/agGridColumn';
import { ChangeDetectionService, ChangeDetectionStrategyType } from '../shared/changeDetectionService';
import { AgReactUiProps } from '../shared/interfaces';
import { NewReactComponent } from '../shared/newReactComponent';
import { PortalManager } from '../shared/portalManager';
import GridComp from './gridComp';
import { ReactFrameworkOverrides } from '../shared/reactFrameworkOverrides';
function debug(msg: string, obj?: any) {
// console.log(msg, obj);
}
export class AgGridReactUi<TData = any> extends Component<AgReactUiProps<TData>, { context: Context | undefined }> {
public api!: GridApi<TData>;
public columnApi!: ColumnApi;
private gridOptions!: GridOptions<TData>;
private destroyFuncs: (() => void)[] = [];
private changeDetectionService = new ChangeDetectionService();
private eGui = React.createRef<HTMLDivElement>();
private portalManager: PortalManager;
private whenReadyFuncs: (()=>void)[] = [];
private ready = false;
private renderedAfterMount = false;
private mounted = false;
constructor(public props: any) {
super(props);
debug('AgGridReactUi.constructor');
this.state = {context: undefined};
this.portalManager = new PortalManager(this, props.componentWrappingElement, props.maxComponentCreationTimeMs);
this.destroyFuncs.push(() => this.portalManager.destroy());
}
public render() {
debug('AgGridReactUi.render, context = ' + (this.state.context));
if (this.state.context) {
this.renderedAfterMount = true;
}
return (
<div style={ this.createStyleForDiv() } className={ this.props.className } ref={ this.eGui }>
{ this.state.context && <GridComp context={ this.state.context }/> }
{ this.portalManager.getPortals() }
</div>
);
}
private createStyleForDiv() {
return {
height: '100%',
...(this.props.containerStyle || {})
};
}
public componentDidMount() {
if (this.mounted) {
debug('AgGridReactUi.componentDidMount - skipping');
return;
}
debug('AgGridReactUi.componentDidMount');
this.mounted = true;
const modules = this.props.modules || [];
const gridParams: GridParams = {
providedBeanInstances: {
frameworkComponentWrapper: new ReactFrameworkComponentWrapper(this.portalManager)
},
modules,
frameworkOverrides: new ReactFrameworkOverrides(true)
};
this.gridOptions = this.props.gridOptions || {};
const {children} = this.props;
if (AgGridColumn.hasChildColumns(children)) {
this.gridOptions.columnDefs = AgGridColumn.mapChildColumnDefs(children);
}
this.gridOptions = ComponentUtil.copyAttributesToGridOptions(this.gridOptions, this.props);
const createUiCallback = (context: Context) => {
this.setState({context: context});
// because React is Async, we need to wait for the UI to be initialised before exposing the API's
const ctrlsService = context.getBean(CtrlsService.NAME) as CtrlsService;
ctrlsService.whenReady( ()=> {
debug('AgGridReactUi.createUiCallback');
this.api = this.gridOptions.api!;
this.columnApi = this.gridOptions.columnApi!;
this.props.setGridApi(this.api, this.columnApi);
this.destroyFuncs.push(() => this.api.destroy());
});
};
// this callback adds to ctrlsService.whenReady(), just like above, however because whenReady() executes
// funcs in the order they were received, we know adding items here will be AFTER the grid has set columns
// and data. this is because GridCoreCreator sets these between calling createUiCallback and acceptChangesCallback
const acceptChangesCallback = (context: Context)=> {
const ctrlsService = context.getBean(CtrlsService.NAME) as CtrlsService;
ctrlsService.whenReady( ()=> {
debug('AgGridReactUi.acceptChangesCallback');
this.whenReadyFuncs.forEach( f => f() );
this.whenReadyFuncs.length = 0;
this.ready = true;
});
}
// don't need the return value
const gridCoreCreator = new GridCoreCreator();
gridCoreCreator.create(this.eGui.current!, this.gridOptions, createUiCallback, acceptChangesCallback, gridParams);
}
public componentWillUnmount() {
if (this.renderedAfterMount) {
debug('AgGridReactUi.componentWillUnmount - executing');
this.destroyFuncs.forEach(f => f());
this.destroyFuncs.length = 0;
} else {
debug('AgGridReactUi.componentWillUnmount - skipping');
}
}
public componentDidUpdate(prevProps: any) {
this.processPropsChanges(prevProps, this.props);
}
public processPropsChanges(prevProps: any, nextProps: any) {
const changes = {};
this.extractGridPropertyChanges(prevProps, nextProps, changes);
this.extractDeclarativeColDefChanges(nextProps, changes);
this.processChanges(changes);
}
private extractDeclarativeColDefChanges(nextProps: any, changes: any) {
// if columnDefs are provided on gridOptions we use those - you can't combine both
// we also skip if columnDefs are provided as a prop directly on AgGridReact
if ((this.props.gridOptions && this.props.gridOptions.columnDefs) || this.props.columnDefs) {
return;
}
const debugLogging = !!nextProps.debug;
const propKey = 'columnDefs';
const currentColDefs = this.gridOptions.columnDefs;
if (AgGridColumn.hasChildColumns(nextProps.children)) {
const detectionStrategy = this.changeDetectionService.getStrategy(this.getStrategyTypeForProp(propKey));
const newColDefs = AgGridColumn.mapChildColumnDefs(nextProps.children);
if (!detectionStrategy.areEqual(currentColDefs, newColDefs)) {
if (debugLogging) {
console.log(`agGridReact: colDefs definitions changed`);
}
changes[propKey] =
{
previousValue: currentColDefs,
currentValue: newColDefs
};
}
} else if (currentColDefs && currentColDefs.length > 0) {
changes[propKey] =
{
previousValue: currentColDefs,
currentValue: []
};
}
}
private extractGridPropertyChanges(prevProps: any, nextProps: any, changes: any) {
const debugLogging = !!nextProps.debug;
Object.keys(nextProps).forEach(propKey => {
if (_.includes(ComponentUtil.ALL_PROPERTIES, propKey)) {
const changeDetectionStrategy = this.changeDetectionService.getStrategy(this.getStrategyTypeForProp(propKey));
if (!changeDetectionStrategy.areEqual(prevProps[propKey], nextProps[propKey])) {
if (debugLogging) {
console.log(`agGridReact: [${propKey}] property changed`);
}
changes[propKey] = {
previousValue: prevProps[propKey],
currentValue: nextProps[propKey]
};
}
}
});
ComponentUtil.getEventCallbacks().forEach(funcName => {
if (prevProps[funcName] !== nextProps[funcName]) {
if (debugLogging) {
console.log(`agGridReact: [${funcName}] event callback changed`);
}
changes[funcName] = {
previousValue: prevProps[funcName],
currentValue: nextProps[funcName]
};
}
});
}
private processChanges(changes: {}) {
this.processWhenReady( ()=>
ComponentUtil.processOnChange(changes, this.gridOptions, this.api, this.columnApi)
);
}
private processWhenReady(func: ()=>void): void {
if (this.ready) {
debug('AgGridReactUi.processWhenReady sync');
func();
} else {
debug('AgGridReactUi.processWhenReady async');
this.whenReadyFuncs.push(func);
}
}
private getStrategyTypeForProp(propKey: string) {
if (propKey === 'rowData') {
if (this.props.rowDataChangeDetectionStrategy) {
return this.props.rowDataChangeDetectionStrategy;
}
if (this.isImmutableDataActive()) {
return ChangeDetectionStrategyType.IdentityCheck;
}
}
// all other cases will default to DeepValueCheck
return ChangeDetectionStrategyType.DeepValueCheck;
}
private isImmutableDataActive() {
return (this.props.deltaRowDataMode || this.props.immutableData || this.props.getRowId != null) ||
(this.props.gridOptions && (this.props.gridOptions.deltaRowDataMode
|| this.props.gridOptions.immutableData
|| this.props.gridOptions.getRowId != null));
}
}
class ReactFrameworkComponentWrapper extends BaseComponentWrapper<WrappableInterface> implements FrameworkComponentWrapper {
private readonly parent: PortalManager;
constructor(parent: PortalManager) {
super();
this.parent = parent;
}
createWrapper(UserReactComponent: { new(): any; }, componentType: ComponentType): WrappableInterface {
return new NewReactComponent(UserReactComponent, this.parent, componentType);
}
}