@azrico/reactgrid 1.0.18 → 1.0.20

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/dist/src/index.js CHANGED
@@ -17,15 +17,21 @@ Object.keys(_DataGrid).forEach(function (key) {
17
17
  }
18
18
  });
19
19
  });
20
+ var _example = _interopRequireDefault(require("./example"));
21
+ var _react = _interopRequireDefault(require("react"));
22
+ var _client = _interopRequireDefault(require("react-dom/client"));
23
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
24
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
21
25
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
22
- var _default = _DataGrid.default; // if (process.env!.NODE_ENV === 'production') {
23
- // // set production urls
24
- // // enable sentry
25
- // } else if (process.env!.NODE_ENV === 'development') {
26
- // const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
27
- // root.render(<ExampleGrid />);
28
- // } else {
29
- // // set defaults
30
- // }
31
- exports.default = _default;
26
+ /* -------------------------------------------------------------------------- */
27
+ var _default = _DataGrid.default;
28
+ exports.default = _default;
29
+ if (process.env.NODE_ENV === 'production') {
30
+ // set production urls
31
+ // enable sentry
32
+ } else if (process.env.NODE_ENV === 'development') {
33
+ const root = _client.default.createRoot(document.getElementById('root'));
34
+ root.render(_react.default.createElement(_example.default, null));
35
+ } else {
36
+ // set defaults
37
+ }
@@ -33,11 +33,10 @@ type customColumnData = {
33
33
  type GridOptions = Partial<TypeDataGridProps>;
34
34
  export type AzGridProps = {
35
35
  columns?: columnOrString[];
36
- rows?: any[];
37
- get_rows?: () => any[];
36
+ rows?: any[] | ((args: any) => any[] | Promise<any[]>);
38
37
  licenseKey?: string;
39
38
  gridOptions?: GridOptions;
40
- tableColumnExtensions?: Array<{
39
+ columnExtensions?: Array<{
41
40
  name: string;
42
41
  } & Partial<ColumnDefinition>>;
43
42
  editableColumns?: string[];
@@ -49,7 +48,6 @@ export type AzGridProps = {
49
48
  RowDetail?: RowDetailsFunc;
50
49
  selected?: RowSelectionValue;
51
50
  onSelectionChange?: OnSelectionChangeFunc;
52
- onRowsChange?: (rows: any[], options: any) => void;
53
51
  onRowClick?: (rowIndex: number, row: any) => void;
54
52
  onRowDoubleClick?: (rowIndex: number, row: any) => void;
55
53
  onLimitChange?: (v: number) => void;
@@ -30,141 +30,29 @@ const defaultColumnOptions = {
30
30
  draggable: false,
31
31
  lockable: false
32
32
  };
33
+ const dynamic_option_names = ['selected', 'loading', 'limit', 'skip'];
33
34
  /* -------------------------------------------------------------------------- */
34
35
  function AzGrid(props) {
35
- const [pendingChanges, set_pendingChanges] = _react.default.useState(1);
36
- const [cached_rows, set_cached_rows] = _react.default.useState([]);
37
36
  /* -------------------------------------------------------------------------- */
38
37
  /* Columns */
39
38
  /* -------------------------------------------------------------------------- */
40
39
  const cached_columns = _react.default.useMemo(() => {
41
- const final_columns = [];
42
- let mcols = props.columns || [];
43
- let mcolExts = props.tableColumnExtensions || [];
44
- let rightColumns = props.rightColumns || [];
45
- let leftColumns = props.leftColumns || [];
46
- let editableColumns = props.editableColumns || [];
47
- let customColumns = props.customColumns || [];
48
- if (!Array.isArray(mcols)) return [];
49
- /* ------------------------------- fix columns ------------------------------ */
50
- mcols.map(r => {
51
- if (r == null) return null;
52
- if (typeof r === 'string') return {
53
- field: r,
54
- header: r
55
- };
56
- if (typeof r === 'object') {
57
- let final_cl = {
58
- name: get_col_name(r),
59
- header: r.header || r.title || r.name || '-',
60
- width: r.width || undefined
61
- };
62
- delete r.field;
63
- delete r.key;
64
- delete r.columnName;
65
- final_cl = {
66
- ...defaultColumnOptions,
67
- ...r,
68
- ...final_cl
69
- };
70
- return final_cl;
71
- }
72
- return null;
73
- }).forEach(r => {
74
- if (r != null) final_columns.push(r);
75
- });
76
- /* ---------------------------- apply extensions ---------------------------- */
77
- if (mcolExts && Array.isArray(mcolExts)) for (let index = 0; index < mcolExts.length; index++) {
78
- const ext = mcolExts[index];
79
- const ext_cl = get_col_name(ext);
80
- const mcol = final_columns.find(s => s.name === ext_cl);
81
- if (mcol) {
82
- for (const key in ext) {
83
- if (!mcol[key] && key != 'name') mcol[key] = ext[key];
84
- }
85
- }
86
- // if (ext.widthGrow) mcol.widthGrow = ext.widthGrow;
87
- }
88
- /* ------------------------------- frozen cols ------------------------------ */
89
- if (Array.isArray(leftColumns)) {
90
- for (let index = 0; index < leftColumns.length; index++) {
91
- const r = get_col_name(leftColumns[index]);
92
- const mcol = final_columns.find(s => s.name === r);
93
- if (mcol) {
94
- mcol.locked = 'start';
95
- }
96
- }
97
- }
98
- if (Array.isArray(rightColumns)) {
99
- for (let index = 0; index < rightColumns.length; index++) {
100
- const r = get_col_name(rightColumns[index]);
101
- const mcol = final_columns.find(s => s.name === r);
102
- if (mcol) {
103
- mcol.locked = 'end';
104
- }
105
- }
106
- }
107
- /* -------------------------------- ediatable ------------------------------- */
108
- if (Array.isArray(editableColumns)) {
109
- for (let index = 0; index < editableColumns.length; index++) {
110
- const r = get_col_name(editableColumns[index]);
111
- const mcol = final_columns.find(s => s.name === r);
112
- if (mcol) {
113
- // mcol.editor = editorWrapper;
114
- // mcol.editorOptions = {
115
- // editOnClick: true,
116
- // };
117
- }
118
- }
119
- }
120
- if (Array.isArray(customColumns)) {
121
- for (let index = 0; index < customColumns.length; index++) {
122
- const r = customColumns[index];
123
- const rn = get_col_name(r);
124
- const mcol = final_columns.find(s => s.name === rn);
125
- if (mcol) {
126
- //@ts-ignore
127
- mcol.render = r.render;
128
- }
129
- }
130
- }
131
- return final_columns;
132
- }, [props.columns, props.leftColumns, props.editableColumns, props.tableColumnExtensions]);
133
- /* -------------------------------------------------------------------------- */
40
+ return ApplyColumnData(props);
41
+ }, [props.columns, props.leftColumns, props.rightColumns, props.editableColumns, props.columnExtensions, props.customColumns]);
134
42
  /* -------------------------------------------------------------------------- */
135
43
  /* Row changes */
136
44
  /* -------------------------------------------------------------------------- */
137
- _react.default.useEffect(() => {
138
- if (pendingChanges <= 0) return;
139
- //console.log('Updated Pending Changes: ', pendingChanges);
140
- let new_rows = [];
141
- if (typeof props.get_rows === 'function') {
142
- new_rows = props.get_rows();
143
- } else new_rows = props.rows || [];
144
- /* -------------------------------------------------------------------------- */
145
- if (!Array.isArray(new_rows)) new_rows = [];
146
- new_rows = new_rows.filter(s => s != null);
147
- /* -------------------------- fix child parent rel -------------------------- */
148
- if (props.allowChildRows) new_rows = (0, _helpers.fix_parent_child_relations)(new_rows);
149
- set_cached_rows(new_rows);
150
- set_pendingChanges(0);
151
- }, [pendingChanges]);
152
- _react.default.useEffect(() => {
153
- //console.log('Rows Changed !');
154
- set_pendingChanges(r => r + 1);
45
+ const dataSource = _react.default.useCallback(async dt => {
46
+ let result_rows = typeof props.rows === 'function' ? await props.rows(dt) : props.rows;
47
+ if (result_rows == null) return [];
48
+ if (!Array.isArray(result_rows)) result_rows = [result_rows];
49
+ if (props.allowChildRows) result_rows = (0, _helpers.fix_parent_child_relations)(result_rows);
50
+ return result_rows;
155
51
  }, [props.rows]);
156
- function onRowsChange(rows, options) {
157
- //console.log('onRowsChange');
158
- if (props.onRowsChange) props.onRowsChange(rows, options);
159
- set_pendingChanges(r => r + 1); //notify the cache to update rows after we made changes to them
160
- }
161
- /* -------------------------------------------------------------------------- */
162
- /* -------------------------------------------------------------------------- */
163
52
  return _react.default.createElement(DataGrid, {
164
53
  ...props,
165
54
  columns: cached_columns,
166
- rows: cached_rows,
167
- onRowsChange: onRowsChange,
55
+ dataSource: dataSource,
168
56
  highlightedRow: props.highlightedRow
169
57
  });
170
58
  }
@@ -172,14 +60,11 @@ function AzGrid(props) {
172
60
  function DataGrid(props) {
173
61
  const {
174
62
  columns,
175
- rows,
176
- highlightedRow,
63
+ dataSource,
177
64
  dark,
178
- contextMenuItems,
179
- onRowsChange
65
+ contextMenuItems
180
66
  } = props;
181
67
  /* -------------------------------------------------------------------------- */
182
- const gridRef = _react.default.useRef(null);
183
68
  const [menuAnchorEl, set_menuAnchorEl] = _react.default.useState(null);
184
69
  const [menuRow, set_menuRow] = _react.default.useState(null);
185
70
  /* -------------------------------------------------------------------------- */
@@ -207,7 +92,7 @@ function DataGrid(props) {
207
92
  ...events,
208
93
  ...extra_ops
209
94
  };
210
- }, [props.gridOptions]);
95
+ }, []);
211
96
  const static_gridOptions = _react.default.useMemo(() => {
212
97
  return {
213
98
  ...base_gridOptions,
@@ -216,13 +101,12 @@ function DataGrid(props) {
216
101
  };
217
102
  }, [props.gridOptions, base_gridOptions]);
218
103
  const dynamic_gridOptions = _react.default.useMemo(() => {
219
- return {
220
- selected: props.selected,
221
- loading: props.loading,
222
- limit: props.limit,
223
- skip: props.skip
224
- };
225
- }, [props.selected, props.loading, props.skip]);
104
+ const res = {};
105
+ dynamic_option_names.map(r => {
106
+ res[r] = props[r];
107
+ });
108
+ return res;
109
+ }, dynamic_option_names.map(r => props[r]));
226
110
  /* --------------------------------- events --------------------------------- */
227
111
  function onRowContextMenu(row, ev) {
228
112
  if (_helpers.func_selection.has_selection()) return;
@@ -236,10 +120,6 @@ function DataGrid(props) {
236
120
  function onRowDoubleClick(event, row) {
237
121
  props.onRowDoubleClick && props.onRowDoubleClick(row.rowIndex, row.data);
238
122
  }
239
- /* ------------------------- update the selected row ------------------------ */
240
- _react.default.useEffect(() => {
241
- if (highlightedRow) gridRef.current.scrollToRow(getRowIndex(highlightedRow, rows));
242
- }, [highlightedRow]);
243
123
  /* ------------------------------- ContextMenu ------------------------------ */
244
124
  function update_menuAnchorEl(el) {
245
125
  if (!el) {
@@ -260,7 +140,7 @@ function DataGrid(props) {
260
140
  ...dynamic_gridOptions,
261
141
  className: 'azgrid-filler',
262
142
  columns: columns,
263
- dataSource: static_gridOptions.dataSource ? static_gridOptions.dataSource : rows
143
+ dataSource: dataSource
264
144
  }), _react.default.createElement(_controls.ContextMenu, {
265
145
  menuAnchorEl: menuAnchorEl,
266
146
  set_menuAnchorEl: update_menuAnchorEl,
@@ -269,26 +149,122 @@ function DataGrid(props) {
269
149
  }));
270
150
  }
271
151
  /* -------------------------------------------------------------------------- */
152
+ /**
153
+ * Gets name of a column from its column object
154
+ * @param obj
155
+ * @returns
156
+ */
272
157
  function get_col_name(obj) {
273
158
  if (!obj) return 'default';
274
159
  if (typeof obj == 'string') return obj;
275
160
  if (typeof obj != 'object') return 'default';
276
161
  return obj.name || obj.field || obj.key || obj.columnName || 'default';
277
162
  }
163
+ /**
164
+ * Gets default activation key for grid plugin
165
+ * @returns
166
+ */
278
167
  function get_default_key() {
279
168
  return 'ExpiryDate=2999-01-01,Z=1040577552';
280
169
  }
281
- function getRowIndex(row, allrows) {
282
- if (!allrows) return -1;
283
- return allrows.findIndex(s => rowKeyGetter(s) === rowKeyGetter(row)) || -1;
284
- }
285
- function rowKeyGetter(row) {
286
- return row.__uid;
287
- }
288
- function isRowSelected(row, highlightedRow) {
289
- const rid = rowKeyGetter(row);
290
- const highLight = rowKeyGetter(highlightedRow);
291
- return rid != null && rid === highLight;
170
+ /**
171
+ * merges together all different props for columns
172
+ * to build a single final column data array
173
+ * @param props
174
+ * @returns
175
+ */
176
+ function ApplyColumnData(props) {
177
+ const final_columns = [];
178
+ let mcols = props.columns || [];
179
+ let mcolExts = props.columnExtensions || [];
180
+ let rightColumns = props.rightColumns || [];
181
+ let leftColumns = props.leftColumns || [];
182
+ let editableColumns = props.editableColumns || [];
183
+ let customColumns = props.customColumns || [];
184
+ if (!Array.isArray(mcols)) return [];
185
+ /* ------------------------------- fix columns ------------------------------ */
186
+ mcols.map(r => {
187
+ if (r == null) return null;
188
+ if (typeof r === 'string') return {
189
+ field: r,
190
+ header: r
191
+ };
192
+ if (typeof r === 'object') {
193
+ let final_cl = {
194
+ name: get_col_name(r),
195
+ header: r.header || r.title || r.name || '-',
196
+ width: r.width || undefined
197
+ };
198
+ delete r.field;
199
+ delete r.key;
200
+ delete r.columnName;
201
+ final_cl = {
202
+ ...defaultColumnOptions,
203
+ ...r,
204
+ ...final_cl
205
+ };
206
+ return final_cl;
207
+ }
208
+ return null;
209
+ }).forEach(r => {
210
+ if (r != null) final_columns.push(r);
211
+ });
212
+ /* ---------------------------- apply extensions ---------------------------- */
213
+ if (mcolExts && Array.isArray(mcolExts)) for (let index = 0; index < mcolExts.length; index++) {
214
+ const ext = mcolExts[index];
215
+ const ext_cl = get_col_name(ext);
216
+ const mcol = final_columns.find(s => s.name === ext_cl);
217
+ if (mcol) {
218
+ for (const key in ext) {
219
+ if (!mcol[key] && key != 'name') mcol[key] = ext[key];
220
+ }
221
+ }
222
+ // if (ext.widthGrow) mcol.widthGrow = ext.widthGrow;
223
+ }
224
+ /* ------------------------------- frozen cols ------------------------------ */
225
+ if (Array.isArray(leftColumns)) {
226
+ for (let index = 0; index < leftColumns.length; index++) {
227
+ const r = get_col_name(leftColumns[index]);
228
+ const mcol = final_columns.find(s => s.name === r);
229
+ if (mcol) {
230
+ mcol.locked = 'start';
231
+ }
232
+ }
233
+ }
234
+ if (Array.isArray(rightColumns)) {
235
+ for (let index = 0; index < rightColumns.length; index++) {
236
+ const r = get_col_name(rightColumns[index]);
237
+ const mcol = final_columns.find(s => s.name === r);
238
+ if (mcol) {
239
+ mcol.locked = 'end';
240
+ }
241
+ }
242
+ }
243
+ /* -------------------------------- ediatable ------------------------------- */
244
+ if (Array.isArray(editableColumns)) {
245
+ for (let index = 0; index < editableColumns.length; index++) {
246
+ const r = get_col_name(editableColumns[index]);
247
+ const mcol = final_columns.find(s => s.name === r);
248
+ if (mcol) {
249
+ // mcol.editor = editorWrapper;
250
+ // mcol.editorOptions = {
251
+ // editOnClick: true,
252
+ // };
253
+ }
254
+ }
255
+ }
256
+ if (Array.isArray(customColumns)) {
257
+ for (let index = 0; index < customColumns.length; index++) {
258
+ const r = customColumns[index];
259
+ const rn = get_col_name(r);
260
+ const mcol = final_columns.find(s => s.name === rn);
261
+ if (mcol) {
262
+ //@ts-ignore
263
+ mcol.render = r.render;
264
+ }
265
+ }
266
+ }
267
+ return final_columns;
292
268
  }
293
269
  /* -------------------------------------------------------------------------- */
294
270
  var _default = AzGrid;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azrico/reactgrid",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "main": "dist/index.js",
5
5
  "description": "React Datagrid",
6
6
  "author": "Azrideus <MehrdadTavangar@gmail.com>",
package/src/index.tsx CHANGED
@@ -7,12 +7,12 @@ import React from 'react';
7
7
  import ReactDOM from 'react-dom/client';
8
8
  export default AzGrid;
9
9
 
10
- // if (process.env!.NODE_ENV === 'production') {
11
- // // set production urls
12
- // // enable sentry
13
- // } else if (process.env!.NODE_ENV === 'development') {
14
- // const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
15
- // root.render(<ExampleGrid />);
16
- // } else {
17
- // // set defaults
18
- // }
10
+ if (process.env!.NODE_ENV === 'production') {
11
+ // set production urls
12
+ // enable sentry
13
+ } else if (process.env!.NODE_ENV === 'development') {
14
+ const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement);
15
+ root.render(<ExampleGrid />);
16
+ } else {
17
+ // set defaults
18
+ }
@@ -37,7 +37,12 @@ export type DataSourceDefinition = TypeDataSource;
37
37
  export type OnSelectionChangeFunc = (config: TypeOnSelectionChangeArg) => void;
38
38
  export type RowDetailsFunc = (rowDetailsInfo: TypeRowDetailsInfo) => React.ReactNode;
39
39
  /* -------------------------------------------------------------------------- */
40
- type loose_column_names = { key: string; field: string; columnName: string; name: string };
40
+ type loose_column_names = {
41
+ key: string;
42
+ field: string;
43
+ columnName: string;
44
+ name: string;
45
+ };
41
46
  type loose_column_fields = loose_column_names & { title: string };
42
47
  export type LooseColumnDefinition = Partial<loose_column_fields & ColumnDefinition>;
43
48
  //
@@ -50,14 +55,13 @@ type GridOptions = Partial<TypeDataGridProps>;
50
55
  /* -------------------------------------------------------------------------- */
51
56
  export type AzGridProps = {
52
57
  columns?: columnOrString[];
53
- rows?: any[];
54
- get_rows?: () => any[];
58
+ rows?: any[] | ((args: any) => any[] | Promise<any[]>);
55
59
 
56
60
  licenseKey?: string;
57
61
 
58
62
  gridOptions?: GridOptions;
59
63
 
60
- tableColumnExtensions?: Array<{ name: string } & Partial<ColumnDefinition>>;
64
+ columnExtensions?: Array<{ name: string } & Partial<ColumnDefinition>>;
61
65
  editableColumns?: string[];
62
66
  leftColumns?: string[];
63
67
  rightColumns?: string[];
@@ -71,7 +75,7 @@ export type AzGridProps = {
71
75
  selected?: RowSelectionValue;
72
76
  /* -------------------------------------------------------------------------- */
73
77
  onSelectionChange?: OnSelectionChangeFunc;
74
- onRowsChange?: (rows: any[], options: any) => void;
78
+
75
79
  onRowClick?: (rowIndex: number, row: any) => void;
76
80
  onRowDoubleClick?: (rowIndex: number, row: any) => void;
77
81
  onLimitChange?: (v: number) => void;
@@ -91,7 +95,7 @@ type DataGridProps = ModifyFields<
91
95
  AzGridProps,
92
96
  {
93
97
  columns: ColumnDefinition[];
94
- rows: DataRowDefinition[];
98
+ dataSource: any;
95
99
  }
96
100
  >;
97
101
  /* -------------------------------------------------------------------------- */
@@ -109,157 +113,53 @@ const defaultColumnOptions: ColumnDefinition = {
109
113
  draggable: false,
110
114
  lockable: false,
111
115
  };
112
-
116
+ const dynamic_option_names = ['selected', 'loading', 'limit', 'skip'];
113
117
  /* -------------------------------------------------------------------------- */
114
118
  export function AzGrid(props: AzGridProps) {
115
- const [pendingChanges, set_pendingChanges] = React.useState<number>(1);
116
- const [cached_rows, set_cached_rows] = React.useState<any[]>([]);
117
-
118
119
  /* -------------------------------------------------------------------------- */
119
120
  /* Columns */
120
121
  /* -------------------------------------------------------------------------- */
121
122
  const cached_columns = React.useMemo(() => {
122
- const final_columns: MakeWriteable<ColumnDefinition>[] = [];
123
- let mcols = props.columns || [];
124
- let mcolExts = props.tableColumnExtensions || [];
125
- let rightColumns = props.rightColumns || [];
126
- let leftColumns = props.leftColumns || [];
127
- let editableColumns = props.editableColumns || [];
128
- let customColumns = props.customColumns || [];
129
-
130
- if (!Array.isArray(mcols)) return [];
131
- /* ------------------------------- fix columns ------------------------------ */
132
-
133
- mcols
134
- .map((r) => {
135
- if (r == null) return null;
136
- if (typeof r === 'string') return { field: r, header: r };
137
- if (typeof r === 'object') {
138
- let final_cl: MakeWriteable<ColumnDefinition> = {
139
- name: get_col_name(r),
140
- header: r.header || r.title || r.name || '-',
141
- width: r.width || undefined,
142
- };
143
- delete r.field;
144
- delete r.key;
145
- delete r.columnName;
146
- final_cl = {
147
- ...defaultColumnOptions,
148
- ...r,
149
- ...final_cl,
150
- };
151
- return final_cl;
152
- }
153
- return null;
154
- })
155
- .forEach((r) => {
156
- if (r != null) final_columns.push(r);
157
- });
158
-
159
- /* ---------------------------- apply extensions ---------------------------- */
160
- if (mcolExts && Array.isArray(mcolExts))
161
- for (let index = 0; index < mcolExts.length; index++) {
162
- const ext = mcolExts[index];
163
- const ext_cl = get_col_name(ext);
164
- const mcol = final_columns.find((s) => s.name === ext_cl);
165
- if (mcol) {
166
- for (const key in ext) {
167
- if (!mcol[key] && key != 'name') mcol[key] = ext[key];
168
- }
169
- }
170
- // if (ext.widthGrow) mcol.widthGrow = ext.widthGrow;
171
- }
172
-
173
- /* ------------------------------- frozen cols ------------------------------ */
174
- if (Array.isArray(leftColumns)) {
175
- for (let index = 0; index < leftColumns.length; index++) {
176
- const r = get_col_name(leftColumns[index]);
177
- const mcol = final_columns.find((s) => s.name === r);
178
- if (mcol) {
179
- mcol.locked = 'start';
180
- }
181
- }
182
- }
183
- if (Array.isArray(rightColumns)) {
184
- for (let index = 0; index < rightColumns.length; index++) {
185
- const r = get_col_name(rightColumns[index]);
186
- const mcol = final_columns.find((s) => s.name === r);
187
- if (mcol) {
188
- mcol.locked = 'end';
189
- }
190
- }
191
- }
192
- /* -------------------------------- ediatable ------------------------------- */
193
- if (Array.isArray(editableColumns)) {
194
- for (let index = 0; index < editableColumns.length; index++) {
195
- const r = get_col_name(editableColumns[index]);
196
- const mcol = final_columns.find((s) => s.name === r);
197
- if (mcol) {
198
- // mcol.editor = editorWrapper;
199
- // mcol.editorOptions = {
200
- // editOnClick: true,
201
- // };
202
- }
203
- }
204
- }
205
- if (Array.isArray(customColumns)) {
206
- for (let index = 0; index < customColumns.length; index++) {
207
- const r = customColumns[index];
208
- const rn = get_col_name(r);
209
- const mcol = final_columns.find((s) => s.name === rn);
210
- if (mcol) {
211
- //@ts-ignore
212
- mcol.render = r.render;
213
- }
214
- }
215
- }
216
-
217
- return final_columns;
218
- }, [props.columns, props.leftColumns, props.editableColumns, props.tableColumnExtensions]);
219
- /* -------------------------------------------------------------------------- */
220
-
123
+ return ApplyColumnData(props);
124
+ }, [
125
+ props.columns,
126
+ props.leftColumns,
127
+ props.rightColumns,
128
+ props.editableColumns,
129
+ props.columnExtensions,
130
+ props.customColumns,
131
+ ]);
221
132
  /* -------------------------------------------------------------------------- */
222
133
  /* Row changes */
223
134
  /* -------------------------------------------------------------------------- */
224
- React.useEffect(() => {
225
- if (pendingChanges <= 0) return;
226
- //console.log('Updated Pending Changes: ', pendingChanges);
227
- let new_rows: any[] = [];
228
- if (typeof props.get_rows === 'function') {
229
- new_rows = props.get_rows();
230
- } else new_rows = props.rows || [];
231
- /* -------------------------------------------------------------------------- */
232
- if (!Array.isArray(new_rows)) new_rows = [];
233
- new_rows = new_rows.filter((s) => s != null);
234
- /* -------------------------- fix child parent rel -------------------------- */
235
- if (props.allowChildRows) new_rows = fix_parent_child_relations(new_rows);
236
-
237
- set_cached_rows(new_rows);
238
- set_pendingChanges(0);
239
- }, [pendingChanges]);
240
-
241
- React.useEffect(() => {
242
- //console.log('Rows Changed !');
243
- set_pendingChanges((r) => r + 1);
244
- }, [props.rows]);
245
-
246
- function onRowsChange(rows: any[], options: any) {
247
- //console.log('onRowsChange');
248
- if (props.onRowsChange) props.onRowsChange(rows, options);
249
- set_pendingChanges((r) => r + 1); //notify the cache to update rows after we made changes to them
250
- }
251
- /* -------------------------------------------------------------------------- */
252
- /* -------------------------------------------------------------------------- */
253
-
254
- return <DataGrid {...props} columns={cached_columns} rows={cached_rows} onRowsChange={onRowsChange} highlightedRow={props.highlightedRow} />;
135
+ const dataSource = React.useCallback(
136
+ async (dt) => {
137
+ let result_rows =
138
+ typeof props.rows === 'function' ? await props.rows(dt) : props.rows;
139
+
140
+ if (result_rows == null) return [];
141
+ if (!Array.isArray(result_rows)) result_rows = [result_rows];
142
+ if (props.allowChildRows) result_rows = fix_parent_child_relations(result_rows);
143
+ return result_rows;
144
+ },
145
+ [props.rows]
146
+ );
147
+ return (
148
+ <DataGrid
149
+ {...props}
150
+ columns={cached_columns}
151
+ dataSource={dataSource}
152
+ highlightedRow={props.highlightedRow}
153
+ />
154
+ );
255
155
  }
256
156
 
257
157
  /* -------------------------------------------------------------------------- */
258
158
  function DataGrid(props: DataGridProps) {
259
- const { columns, rows, highlightedRow, dark, contextMenuItems, onRowsChange } = props;
159
+ const { columns, dataSource, dark, contextMenuItems } = props;
260
160
 
261
161
  /* -------------------------------------------------------------------------- */
262
- const gridRef = React.useRef<any>(null);
162
+
263
163
  const [menuAnchorEl, set_menuAnchorEl] = React.useState<any>(null);
264
164
  const [menuRow, set_menuRow] = React.useState<any>(null);
265
165
 
@@ -285,13 +185,22 @@ function DataGrid(props: DataGridProps) {
285
185
  theme: dark ? 'amber-dark' : 'default-light',
286
186
  };
287
187
  return { ...defaultOptions, ...events, ...extra_ops };
288
- }, [props.gridOptions]);
188
+ }, []);
189
+
289
190
  const static_gridOptions = React.useMemo<GridOptions>(() => {
290
191
  return { ...base_gridOptions, ...props.gridOptions, ...forcedOptions };
291
192
  }, [props.gridOptions, base_gridOptions]);
292
- const dynamic_gridOptions = React.useMemo<GridOptions>(() => {
293
- return { selected: props.selected, loading: props.loading, limit: props.limit, skip: props.skip };
294
- }, [props.selected, props.loading, props.skip]);
193
+
194
+ const dynamic_gridOptions = React.useMemo<GridOptions>(
195
+ () => {
196
+ const res = {};
197
+ dynamic_option_names.map((r) => {
198
+ res[r] = props[r];
199
+ });
200
+ return res;
201
+ },
202
+ dynamic_option_names.map((r) => props[r])
203
+ );
295
204
 
296
205
  /* --------------------------------- events --------------------------------- */
297
206
  function onRowContextMenu(row: TypeRowProps, ev: any) {
@@ -307,11 +216,6 @@ function DataGrid(props: DataGridProps) {
307
216
  props.onRowDoubleClick && props.onRowDoubleClick(row.rowIndex, row.data);
308
217
  }
309
218
 
310
- /* ------------------------- update the selected row ------------------------ */
311
- React.useEffect(() => {
312
- if (highlightedRow) gridRef.current!.scrollToRow(getRowIndex(highlightedRow, rows));
313
- }, [highlightedRow]);
314
-
315
219
  /* ------------------------------- ContextMenu ------------------------------ */
316
220
 
317
221
  function update_menuAnchorEl(el) {
@@ -335,34 +239,143 @@ function DataGrid(props: DataGridProps) {
335
239
  {...dynamic_gridOptions}
336
240
  className={'azgrid-filler'}
337
241
  columns={columns}
338
- dataSource={static_gridOptions.dataSource ? static_gridOptions.dataSource : rows}
242
+ dataSource={dataSource}
243
+ />
244
+ <ContextMenu
245
+ menuAnchorEl={menuAnchorEl}
246
+ set_menuAnchorEl={update_menuAnchorEl}
247
+ menuRow={menuRow}
248
+ items={contextMenuItems}
339
249
  />
340
- <ContextMenu menuAnchorEl={menuAnchorEl} set_menuAnchorEl={update_menuAnchorEl} menuRow={menuRow} items={contextMenuItems} />
341
250
  </div>
342
251
  );
343
252
  }
253
+
344
254
  /* -------------------------------------------------------------------------- */
345
255
 
256
+ /**
257
+ * Gets name of a column from its column object
258
+ * @param obj
259
+ * @returns
260
+ */
346
261
  function get_col_name(obj: any): string {
347
262
  if (!obj) return 'default';
348
263
  if (typeof obj == 'string') return obj;
349
264
  if (typeof obj != 'object') return 'default';
350
265
  return obj.name || obj.field || obj.key || obj.columnName || 'default';
351
266
  }
267
+
268
+ /**
269
+ * Gets default activation key for grid plugin
270
+ * @returns
271
+ */
352
272
  function get_default_key() {
353
273
  return 'ExpiryDate=2999-01-01,Z=1040577552';
354
274
  }
355
- function getRowIndex(row: any, allrows?: any[]): number {
356
- if (!allrows) return -1;
357
- return allrows.findIndex((s) => rowKeyGetter(s) === rowKeyGetter(row)) || -1;
358
- }
359
- function rowKeyGetter(row: any) {
360
- return row.__uid;
361
- }
362
- function isRowSelected(row: any, highlightedRow: any) {
363
- const rid = rowKeyGetter(row);
364
- const highLight = rowKeyGetter(highlightedRow);
365
- return rid != null && rid === highLight;
275
+
276
+ /**
277
+ * merges together all different props for columns
278
+ * to build a single final column data array
279
+ * @param props
280
+ * @returns
281
+ */
282
+ function ApplyColumnData(props: any) {
283
+ const final_columns: MakeWriteable<ColumnDefinition>[] = [];
284
+ let mcols = props.columns || [];
285
+ let mcolExts = props.columnExtensions || [];
286
+ let rightColumns = props.rightColumns || [];
287
+ let leftColumns = props.leftColumns || [];
288
+ let editableColumns = props.editableColumns || [];
289
+ let customColumns = props.customColumns || [];
290
+
291
+ if (!Array.isArray(mcols)) return [];
292
+ /* ------------------------------- fix columns ------------------------------ */
293
+
294
+ mcols
295
+ .map((r) => {
296
+ if (r == null) return null;
297
+ if (typeof r === 'string') return { field: r, header: r };
298
+ if (typeof r === 'object') {
299
+ let final_cl: MakeWriteable<ColumnDefinition> = {
300
+ name: get_col_name(r),
301
+ header: r.header || r.title || r.name || '-',
302
+ width: r.width || undefined,
303
+ };
304
+ delete r.field;
305
+ delete r.key;
306
+ delete r.columnName;
307
+ final_cl = {
308
+ ...defaultColumnOptions,
309
+ ...r,
310
+ ...final_cl,
311
+ };
312
+ return final_cl;
313
+ }
314
+ return null;
315
+ })
316
+ .forEach((r) => {
317
+ if (r != null) final_columns.push(r);
318
+ });
319
+
320
+ /* ---------------------------- apply extensions ---------------------------- */
321
+ if (mcolExts && Array.isArray(mcolExts))
322
+ for (let index = 0; index < mcolExts.length; index++) {
323
+ const ext = mcolExts[index];
324
+ const ext_cl = get_col_name(ext);
325
+ const mcol = final_columns.find((s) => s.name === ext_cl);
326
+ if (mcol) {
327
+ for (const key in ext) {
328
+ if (!mcol[key] && key != 'name') mcol[key] = ext[key];
329
+ }
330
+ }
331
+ // if (ext.widthGrow) mcol.widthGrow = ext.widthGrow;
332
+ }
333
+
334
+ /* ------------------------------- frozen cols ------------------------------ */
335
+ if (Array.isArray(leftColumns)) {
336
+ for (let index = 0; index < leftColumns.length; index++) {
337
+ const r = get_col_name(leftColumns[index]);
338
+ const mcol = final_columns.find((s) => s.name === r);
339
+ if (mcol) {
340
+ mcol.locked = 'start';
341
+ }
342
+ }
343
+ }
344
+ if (Array.isArray(rightColumns)) {
345
+ for (let index = 0; index < rightColumns.length; index++) {
346
+ const r = get_col_name(rightColumns[index]);
347
+ const mcol = final_columns.find((s) => s.name === r);
348
+ if (mcol) {
349
+ mcol.locked = 'end';
350
+ }
351
+ }
352
+ }
353
+ /* -------------------------------- ediatable ------------------------------- */
354
+ if (Array.isArray(editableColumns)) {
355
+ for (let index = 0; index < editableColumns.length; index++) {
356
+ const r = get_col_name(editableColumns[index]);
357
+ const mcol = final_columns.find((s) => s.name === r);
358
+ if (mcol) {
359
+ // mcol.editor = editorWrapper;
360
+ // mcol.editorOptions = {
361
+ // editOnClick: true,
362
+ // };
363
+ }
364
+ }
365
+ }
366
+ if (Array.isArray(customColumns)) {
367
+ for (let index = 0; index < customColumns.length; index++) {
368
+ const r = customColumns[index];
369
+ const rn = get_col_name(r);
370
+ const mcol = final_columns.find((s) => s.name === rn);
371
+ if (mcol) {
372
+ //@ts-ignore
373
+ mcol.render = r.render;
374
+ }
375
+ }
376
+ }
377
+
378
+ return final_columns;
366
379
  }
367
380
 
368
381
  /* -------------------------------------------------------------------------- */