@azrico/reactgrid 1.0.19 → 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;
@@ -33,139 +33,26 @@ const defaultColumnOptions = {
33
33
  const dynamic_option_names = ['selected', 'loading', 'limit', 'skip'];
34
34
  /* -------------------------------------------------------------------------- */
35
35
  function AzGrid(props) {
36
- const [pendingChanges, set_pendingChanges] = _react.default.useState(1);
37
- const [cached_rows, set_cached_rows] = _react.default.useState([]);
38
36
  /* -------------------------------------------------------------------------- */
39
37
  /* Columns */
40
38
  /* -------------------------------------------------------------------------- */
41
39
  const cached_columns = _react.default.useMemo(() => {
42
- const final_columns = [];
43
- let mcols = props.columns || [];
44
- let mcolExts = props.tableColumnExtensions || [];
45
- let rightColumns = props.rightColumns || [];
46
- let leftColumns = props.leftColumns || [];
47
- let editableColumns = props.editableColumns || [];
48
- let customColumns = props.customColumns || [];
49
- if (!Array.isArray(mcols)) return [];
50
- /* ------------------------------- fix columns ------------------------------ */
51
- mcols.map(r => {
52
- if (r == null) return null;
53
- if (typeof r === 'string') return {
54
- field: r,
55
- header: r
56
- };
57
- if (typeof r === 'object') {
58
- let final_cl = {
59
- name: get_col_name(r),
60
- header: r.header || r.title || r.name || '-',
61
- width: r.width || undefined
62
- };
63
- delete r.field;
64
- delete r.key;
65
- delete r.columnName;
66
- final_cl = {
67
- ...defaultColumnOptions,
68
- ...r,
69
- ...final_cl
70
- };
71
- return final_cl;
72
- }
73
- return null;
74
- }).forEach(r => {
75
- if (r != null) final_columns.push(r);
76
- });
77
- /* ---------------------------- apply extensions ---------------------------- */
78
- if (mcolExts && Array.isArray(mcolExts)) for (let index = 0; index < mcolExts.length; index++) {
79
- const ext = mcolExts[index];
80
- const ext_cl = get_col_name(ext);
81
- const mcol = final_columns.find(s => s.name === ext_cl);
82
- if (mcol) {
83
- for (const key in ext) {
84
- if (!mcol[key] && key != 'name') mcol[key] = ext[key];
85
- }
86
- }
87
- // if (ext.widthGrow) mcol.widthGrow = ext.widthGrow;
88
- }
89
- /* ------------------------------- frozen cols ------------------------------ */
90
- if (Array.isArray(leftColumns)) {
91
- for (let index = 0; index < leftColumns.length; index++) {
92
- const r = get_col_name(leftColumns[index]);
93
- const mcol = final_columns.find(s => s.name === r);
94
- if (mcol) {
95
- mcol.locked = 'start';
96
- }
97
- }
98
- }
99
- if (Array.isArray(rightColumns)) {
100
- for (let index = 0; index < rightColumns.length; index++) {
101
- const r = get_col_name(rightColumns[index]);
102
- const mcol = final_columns.find(s => s.name === r);
103
- if (mcol) {
104
- mcol.locked = 'end';
105
- }
106
- }
107
- }
108
- /* -------------------------------- ediatable ------------------------------- */
109
- if (Array.isArray(editableColumns)) {
110
- for (let index = 0; index < editableColumns.length; index++) {
111
- const r = get_col_name(editableColumns[index]);
112
- const mcol = final_columns.find(s => s.name === r);
113
- if (mcol) {
114
- // mcol.editor = editorWrapper;
115
- // mcol.editorOptions = {
116
- // editOnClick: true,
117
- // };
118
- }
119
- }
120
- }
121
- if (Array.isArray(customColumns)) {
122
- for (let index = 0; index < customColumns.length; index++) {
123
- const r = customColumns[index];
124
- const rn = get_col_name(r);
125
- const mcol = final_columns.find(s => s.name === rn);
126
- if (mcol) {
127
- //@ts-ignore
128
- mcol.render = r.render;
129
- }
130
- }
131
- }
132
- return final_columns;
133
- }, [props.columns, props.leftColumns, props.editableColumns, props.tableColumnExtensions]);
134
- /* -------------------------------------------------------------------------- */
40
+ return ApplyColumnData(props);
41
+ }, [props.columns, props.leftColumns, props.rightColumns, props.editableColumns, props.columnExtensions, props.customColumns]);
135
42
  /* -------------------------------------------------------------------------- */
136
43
  /* Row changes */
137
44
  /* -------------------------------------------------------------------------- */
138
- _react.default.useEffect(() => {
139
- if (pendingChanges <= 0) return;
140
- //console.log('Updated Pending Changes: ', pendingChanges);
141
- let new_rows = [];
142
- if (typeof props.get_rows === 'function') {
143
- new_rows = props.get_rows();
144
- } else new_rows = props.rows || [];
145
- /* -------------------------------------------------------------------------- */
146
- if (!Array.isArray(new_rows)) new_rows = [];
147
- new_rows = new_rows.filter(s => s != null);
148
- /* -------------------------- fix child parent rel -------------------------- */
149
- if (props.allowChildRows) new_rows = (0, _helpers.fix_parent_child_relations)(new_rows);
150
- set_cached_rows(new_rows);
151
- set_pendingChanges(0);
152
- }, [pendingChanges]);
153
- _react.default.useEffect(() => {
154
- //console.log('Rows Changed !');
155
- 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;
156
51
  }, [props.rows]);
157
- function onRowsChange(rows, options) {
158
- //console.log('onRowsChange');
159
- if (props.onRowsChange) props.onRowsChange(rows, options);
160
- set_pendingChanges(r => r + 1); //notify the cache to update rows after we made changes to them
161
- }
162
- /* -------------------------------------------------------------------------- */
163
- /* -------------------------------------------------------------------------- */
164
52
  return _react.default.createElement(DataGrid, {
165
53
  ...props,
166
54
  columns: cached_columns,
167
- rows: cached_rows,
168
- onRowsChange: onRowsChange,
55
+ dataSource: dataSource,
169
56
  highlightedRow: props.highlightedRow
170
57
  });
171
58
  }
@@ -173,14 +60,11 @@ function AzGrid(props) {
173
60
  function DataGrid(props) {
174
61
  const {
175
62
  columns,
176
- rows,
177
- highlightedRow,
63
+ dataSource,
178
64
  dark,
179
- contextMenuItems,
180
- onRowsChange
65
+ contextMenuItems
181
66
  } = props;
182
67
  /* -------------------------------------------------------------------------- */
183
- const gridRef = _react.default.useRef(null);
184
68
  const [menuAnchorEl, set_menuAnchorEl] = _react.default.useState(null);
185
69
  const [menuRow, set_menuRow] = _react.default.useState(null);
186
70
  /* -------------------------------------------------------------------------- */
@@ -208,7 +92,7 @@ function DataGrid(props) {
208
92
  ...events,
209
93
  ...extra_ops
210
94
  };
211
- }, [props.gridOptions]);
95
+ }, []);
212
96
  const static_gridOptions = _react.default.useMemo(() => {
213
97
  return {
214
98
  ...base_gridOptions,
@@ -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.19",
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
  /* -------------------------------------------------------------------------- */
@@ -112,154 +116,50 @@ const defaultColumnOptions: ColumnDefinition = {
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,7 +185,8 @@ 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]);
@@ -315,11 +216,6 @@ function DataGrid(props: DataGridProps) {
315
216
  props.onRowDoubleClick && props.onRowDoubleClick(row.rowIndex, row.data);
316
217
  }
317
218
 
318
- /* ------------------------- update the selected row ------------------------ */
319
- React.useEffect(() => {
320
- if (highlightedRow) gridRef.current!.scrollToRow(getRowIndex(highlightedRow, rows));
321
- }, [highlightedRow]);
322
-
323
219
  /* ------------------------------- ContextMenu ------------------------------ */
324
220
 
325
221
  function update_menuAnchorEl(el) {
@@ -343,34 +239,143 @@ function DataGrid(props: DataGridProps) {
343
239
  {...dynamic_gridOptions}
344
240
  className={'azgrid-filler'}
345
241
  columns={columns}
346
- 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}
347
249
  />
348
- <ContextMenu menuAnchorEl={menuAnchorEl} set_menuAnchorEl={update_menuAnchorEl} menuRow={menuRow} items={contextMenuItems} />
349
250
  </div>
350
251
  );
351
252
  }
253
+
352
254
  /* -------------------------------------------------------------------------- */
353
255
 
256
+ /**
257
+ * Gets name of a column from its column object
258
+ * @param obj
259
+ * @returns
260
+ */
354
261
  function get_col_name(obj: any): string {
355
262
  if (!obj) return 'default';
356
263
  if (typeof obj == 'string') return obj;
357
264
  if (typeof obj != 'object') return 'default';
358
265
  return obj.name || obj.field || obj.key || obj.columnName || 'default';
359
266
  }
267
+
268
+ /**
269
+ * Gets default activation key for grid plugin
270
+ * @returns
271
+ */
360
272
  function get_default_key() {
361
273
  return 'ExpiryDate=2999-01-01,Z=1040577552';
362
274
  }
363
- function getRowIndex(row: any, allrows?: any[]): number {
364
- if (!allrows) return -1;
365
- return allrows.findIndex((s) => rowKeyGetter(s) === rowKeyGetter(row)) || -1;
366
- }
367
- function rowKeyGetter(row: any) {
368
- return row.__uid;
369
- }
370
- function isRowSelected(row: any, highlightedRow: any) {
371
- const rid = rowKeyGetter(row);
372
- const highLight = rowKeyGetter(highlightedRow);
373
- 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;
374
379
  }
375
380
 
376
381
  /* -------------------------------------------------------------------------- */