@azrico/reactgrid 1.2.4 → 1.2.6
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/CHANGELOG.md +25 -0
- package/dist/src/example.js +27 -8
- package/dist/src/func/{helpers.js → func_child.js} +11 -7
- package/dist/src/func/func_grid.d.ts +38 -0
- package/dist/src/func/func_grid.js +276 -0
- package/dist/src/index.js +0 -3
- package/dist/src/views/DataGrid.d.ts +6 -13
- package/dist/src/views/DataGrid.js +71 -289
- package/package.json +1 -1
- package/src/example.tsx +27 -8
- package/src/func/{helpers.ts → func_child.ts} +18 -10
- package/src/func/func_grid.ts +287 -0
- package/src/index.tsx +3 -3
- package/src/views/DataGrid.tsx +98 -304
- /package/dist/src/func/{helpers.d.ts → func_child.d.ts} +0 -0
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { ColumnDefinition, LooseColumnDefinition } from '../views/DataGrid';
|
|
2
|
+
|
|
3
|
+
import BoolEditor from '@inovua/reactdatagrid-community/BoolEditor';
|
|
4
|
+
import NumericEditor from '@inovua/reactdatagrid-community/NumericEditor';
|
|
5
|
+
import SelectEditor from '@inovua/reactdatagrid-community/SelectEditor';
|
|
6
|
+
import { fix_parent_child_relations } from './func_child';
|
|
7
|
+
import { object_equals } from '@azrico/object';
|
|
8
|
+
|
|
9
|
+
export type MakeWriteable<Type> = {
|
|
10
|
+
-readonly [Key in keyof Type]: Type[Key];
|
|
11
|
+
};
|
|
12
|
+
export type ModifyFields<T, R> = Omit<T, keyof R> & R;
|
|
13
|
+
|
|
14
|
+
const defaultColumnOptions: ColumnDefinition = {
|
|
15
|
+
draggable: false,
|
|
16
|
+
lockable: false,
|
|
17
|
+
editable: false,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export async function ApplyDataSource(datasource, props) {
|
|
21
|
+
let result_data: any = undefined;
|
|
22
|
+
|
|
23
|
+
let isUsingCache = false;
|
|
24
|
+
|
|
25
|
+
const cacheInfo = { ds: datasource, skip: props.skip, limit: props.limit };
|
|
26
|
+
//check if we can used cached data
|
|
27
|
+
//if the datasource is changed since last render, we should invalidate cache
|
|
28
|
+
if (!validateCache(props.last_cache_info.current, cacheInfo, props.looseCacheCheck)) {
|
|
29
|
+
props.cached_data.current = undefined;
|
|
30
|
+
props.last_cache_info.current = cacheInfo;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (props.cached_data.current) {
|
|
34
|
+
//cache is valid, use the cache
|
|
35
|
+
isUsingCache = true;
|
|
36
|
+
result_data = props.cached_data.current;
|
|
37
|
+
} else {
|
|
38
|
+
//cache is invalid, use the datasource again
|
|
39
|
+
|
|
40
|
+
isUsingCache = false;
|
|
41
|
+
if (typeof datasource === 'function') {
|
|
42
|
+
result_data = await datasource(props);
|
|
43
|
+
} else result_data = datasource;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
//making sure the datasource is a valid object :
|
|
47
|
+
if (!result_data) result_data = [];
|
|
48
|
+
if (Array.isArray(result_data) || typeof result_data !== 'object')
|
|
49
|
+
result_data = { data: result_data };
|
|
50
|
+
|
|
51
|
+
if (!Array.isArray(result_data.data)) result_data.data = [result_data.data];
|
|
52
|
+
|
|
53
|
+
//no need to fix parent/child when using cache (it should be already fixed)
|
|
54
|
+
if (!isUsingCache && props.allowChildRows)
|
|
55
|
+
result_data.data = fix_parent_child_relations(result_data.data);
|
|
56
|
+
|
|
57
|
+
if (!result_data.count) result_data.count = (result_data.data as Array<any>).length;
|
|
58
|
+
|
|
59
|
+
//update cache
|
|
60
|
+
props.cached_data.current = result_data;
|
|
61
|
+
return result_data;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* merges together all different props for columns
|
|
65
|
+
* to build a single final column data array
|
|
66
|
+
* @param column_data
|
|
67
|
+
* @returns
|
|
68
|
+
*/
|
|
69
|
+
export function ApplyColumnData(column_data: any): ColumnDefinition[] {
|
|
70
|
+
const final_columns: MakeWriteable<LooseColumnDefinition>[] = [];
|
|
71
|
+
if (!column_data) column_data = {};
|
|
72
|
+
if (Array.isArray(column_data)) column_data = { columns: column_data };
|
|
73
|
+
let mcols = column_data.columns || [];
|
|
74
|
+
let mcolExts = column_data.columnExtensions || [];
|
|
75
|
+
let rightColumns = column_data.rightColumns || [];
|
|
76
|
+
let leftColumns = column_data.leftColumns || [];
|
|
77
|
+
let editableColumns = column_data.editableColumns || [];
|
|
78
|
+
let customColumns = column_data.customColumns || [];
|
|
79
|
+
|
|
80
|
+
if (!Array.isArray(mcols)) return [];
|
|
81
|
+
/* ------------------------------- fix columns ------------------------------ */
|
|
82
|
+
|
|
83
|
+
mcols
|
|
84
|
+
.map((r) => {
|
|
85
|
+
if (r == null) return null;
|
|
86
|
+
if (typeof r === 'string') return { field: r, header: r };
|
|
87
|
+
if (typeof r === 'object') {
|
|
88
|
+
let final_cl: MakeWriteable<ColumnDefinition> = {
|
|
89
|
+
name: get_col_name(r),
|
|
90
|
+
header: get_col_header(r),
|
|
91
|
+
width: r.width || undefined,
|
|
92
|
+
};
|
|
93
|
+
final_cl = {
|
|
94
|
+
...defaultColumnOptions,
|
|
95
|
+
...r,
|
|
96
|
+
...final_cl,
|
|
97
|
+
};
|
|
98
|
+
return final_cl;
|
|
99
|
+
}
|
|
100
|
+
return null;
|
|
101
|
+
})
|
|
102
|
+
.forEach((r) => {
|
|
103
|
+
if (r != null) final_columns.push(r);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
/* ---------------------------- apply extensions ---------------------------- */
|
|
107
|
+
if (mcolExts && Array.isArray(mcolExts))
|
|
108
|
+
for (let index = 0; index < mcolExts.length; index++) {
|
|
109
|
+
const ext = mcolExts[index];
|
|
110
|
+
const ext_cl = get_col_name(ext);
|
|
111
|
+
if (!ext_cl) {
|
|
112
|
+
console.warn('ColumnExtension must have a name');
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
const mcol = final_columns.find((s) => get_col_name(s) === ext_cl);
|
|
116
|
+
if (mcol) {
|
|
117
|
+
for (const key in ext) {
|
|
118
|
+
if (!mcol[key] && key != 'name') mcol[key] = ext[key];
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
// if (ext.widthGrow) mcol.widthGrow = ext.widthGrow;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/* ------------------------------- frozen cols ------------------------------ */
|
|
125
|
+
if (Array.isArray(leftColumns)) {
|
|
126
|
+
for (let index = 0; index < leftColumns.length; index++) {
|
|
127
|
+
const r = get_col_name(leftColumns[index]);
|
|
128
|
+
const mcol = final_columns.find((s) => get_col_name(s) === r);
|
|
129
|
+
if (mcol) {
|
|
130
|
+
mcol.locked = 'start';
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (Array.isArray(rightColumns)) {
|
|
135
|
+
for (let index = 0; index < rightColumns.length; index++) {
|
|
136
|
+
const r = get_col_name(rightColumns[index]);
|
|
137
|
+
const mcol = final_columns.find((s) => get_col_name(s) === r);
|
|
138
|
+
if (mcol) {
|
|
139
|
+
mcol.locked = 'end';
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
/* -------------------------------- ediatable ------------------------------- */
|
|
144
|
+
if (Array.isArray(editableColumns)) {
|
|
145
|
+
for (let index = 0; index < editableColumns.length; index++) {
|
|
146
|
+
const r = get_col_name(editableColumns[index]);
|
|
147
|
+
const mcol = final_columns.find((s) => get_col_name(s) === r);
|
|
148
|
+
if (mcol) {
|
|
149
|
+
mcol.editable = true;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
for (let index = 0; index < final_columns.length; index++) {
|
|
154
|
+
const col = final_columns[index];
|
|
155
|
+
if (col.type) {
|
|
156
|
+
switch (col.type) {
|
|
157
|
+
case 'combo':
|
|
158
|
+
case 'select':
|
|
159
|
+
//@ts-ignore
|
|
160
|
+
col.editor = SelectEditor;
|
|
161
|
+
break;
|
|
162
|
+
case 'number':
|
|
163
|
+
case 'numberic':
|
|
164
|
+
//@ts-ignore
|
|
165
|
+
col.editor = NumericEditor;
|
|
166
|
+
break;
|
|
167
|
+
case 'truefalse':
|
|
168
|
+
case 'bool':
|
|
169
|
+
//@ts-ignore
|
|
170
|
+
col.editor = BoolEditor;
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
if (col.editable != false) col.editable = true;
|
|
174
|
+
delete col.type;
|
|
175
|
+
}
|
|
176
|
+
//@ts-ignore
|
|
177
|
+
if (!col.editorProps) col.editorProps = {};
|
|
178
|
+
if (col.items) {
|
|
179
|
+
//@ts-ignore
|
|
180
|
+
if (!col.editorProps.idProperty) col.editorProps.idProperty = 'id';
|
|
181
|
+
//@ts-ignore
|
|
182
|
+
if (!col.editorProps.collapseOnSelect) col.editorProps.collapseOnSelect = true;
|
|
183
|
+
//@ts-ignore
|
|
184
|
+
col.editorProps.dataSource = to_datasource(col.items, col.editorProps.idProperty);
|
|
185
|
+
|
|
186
|
+
delete col.items;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/* -------------------------------------------------------------------------- */
|
|
191
|
+
if (Array.isArray(customColumns)) {
|
|
192
|
+
for (let index = 0; index < customColumns.length; index++) {
|
|
193
|
+
const r = customColumns[index];
|
|
194
|
+
const rn = get_col_name(r);
|
|
195
|
+
const mcol = final_columns.find((s) => get_col_name(s) === rn);
|
|
196
|
+
if (mcol) {
|
|
197
|
+
//@ts-ignore
|
|
198
|
+
mcol.render = r.render;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/* ------------------------------ apply renames ----------------------------- */
|
|
204
|
+
for (let index = 0; index < final_columns.length; index++) {
|
|
205
|
+
const col = final_columns[index];
|
|
206
|
+
if (!col.name) {
|
|
207
|
+
col.name = get_col_name(col);
|
|
208
|
+
delete col.field;
|
|
209
|
+
delete col.key;
|
|
210
|
+
delete col.columnName;
|
|
211
|
+
|
|
212
|
+
if (col.flex && !col.defaultFlex) col.defaultFlex = col.flex;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/* -------------------------------------------------------------------------- */
|
|
216
|
+
//console.log(final_columns);
|
|
217
|
+
return final_columns;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function to_datasource(arr: any[], idProperty: string, valueAsID = true) {
|
|
221
|
+
const res: any[] = [];
|
|
222
|
+
let index = 0;
|
|
223
|
+
if (Array.isArray(arr)) {
|
|
224
|
+
arr.map((s: any) => {
|
|
225
|
+
let elem = {};
|
|
226
|
+
if (valueAsID) elem[idProperty] = String(s).toLowerCase();
|
|
227
|
+
else elem[idProperty] = '_cid_' + index;
|
|
228
|
+
if (typeof s === 'object') elem = { ...elem, ...s };
|
|
229
|
+
else elem['label'] = s;
|
|
230
|
+
res.push(elem);
|
|
231
|
+
index++;
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return res;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Gets name of a column from its column object
|
|
239
|
+
* @param obj
|
|
240
|
+
* @returns
|
|
241
|
+
*/
|
|
242
|
+
export function get_col_name(obj: any): string {
|
|
243
|
+
if (!obj) return '';
|
|
244
|
+
if (typeof obj == 'string') return obj;
|
|
245
|
+
if (typeof obj != 'object') return '';
|
|
246
|
+
return obj.name || obj.field || obj.key || obj.columnName || '';
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Gets header of a column
|
|
250
|
+
* @param obj
|
|
251
|
+
* @returns
|
|
252
|
+
*/
|
|
253
|
+
export function get_col_header(obj: any): string {
|
|
254
|
+
if (!obj) return '';
|
|
255
|
+
if (typeof obj == 'string') return obj;
|
|
256
|
+
if (typeof obj != 'object') return '';
|
|
257
|
+
return obj.header || obj.title || get_col_name(obj);
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Gets name of a column from its column object
|
|
261
|
+
* @param obj
|
|
262
|
+
* @returns
|
|
263
|
+
*/
|
|
264
|
+
export function get_row_id(obj: any): any {
|
|
265
|
+
if (!obj) return '-1';
|
|
266
|
+
if (typeof obj == 'string') return obj;
|
|
267
|
+
if (typeof obj != 'object') return '-1';
|
|
268
|
+
return obj.__table_id || obj.id || '-1';
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Gets default activation key for grid plugin
|
|
273
|
+
* @returns
|
|
274
|
+
*/
|
|
275
|
+
export function get_default_key() {
|
|
276
|
+
return 'ExpiryDate=2999-01-01,Z=1040577552';
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
//check if two cache objects are the same
|
|
280
|
+
export function validateCache(current: any, old: any, looseCheck = false) {
|
|
281
|
+
if (!old || !object_equals(current, old)) return false;
|
|
282
|
+
|
|
283
|
+
if (looseCheck) return true;
|
|
284
|
+
|
|
285
|
+
if (current.ds != old.ds) return false;
|
|
286
|
+
return true;
|
|
287
|
+
}
|
package/src/index.tsx
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export * from './views/DataGrid';
|
|
2
2
|
|
|
3
3
|
import AzGrid from './views/DataGrid';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
import ExampleGrid from './example';
|
|
5
|
+
import React from 'react';
|
|
6
|
+
import ReactDOM from 'react-dom/client';
|
|
7
7
|
|
|
8
8
|
/* -------------------------------------------------------------------------- */
|
|
9
9
|
|