@azrico/reactgrid 1.1.2 → 1.1.3

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.
@@ -72,24 +72,24 @@ function getData() {
72
72
  }
73
73
  return fd;
74
74
  }
75
- //TODO REMOTE FILTER OFF ?
76
75
  function ExampleGrid() {
77
- const [selected, setSelected] = _react.default.useState(null);
78
- const onSelectionChange = _react.default.useCallback(({
79
- selected
80
- }) => {
81
- setSelected(selected);
82
- }, []);
76
+ const [ds_reset, set_ds_reset] = _react.default.useState(0);
83
77
  const dataSource = _react.default.useCallback(async dt => {
84
- console.log('Start loading');
78
+ //console.log('Start loading');
85
79
  return await new Promise(resolve => {
86
80
  setTimeout(() => {
87
- console.log('end loading');
81
+ //console.log('end loading');
88
82
  resolve(getData());
89
83
  }, 1000);
90
84
  });
91
- }, []);
92
- return _react.default.createElement(_DataGrid.default, {
85
+ }, [ds_reset]);
86
+ function rst_ds() {
87
+ //console.log('rst_ds');
88
+ set_ds_reset(s => s + 1);
89
+ }
90
+ return _react.default.createElement("div", null, _react.default.createElement("div", {
91
+ onClick: rst_ds
92
+ }, "RESET"), _react.default.createElement(_DataGrid.default, {
93
93
  dark: true,
94
94
  allowChildRows: true,
95
95
  // onSelectionChange={onSelectionChange}
@@ -110,7 +110,7 @@ function ExampleGrid() {
110
110
  checkboxColumn: true
111
111
  },
112
112
  dataSource: dataSource
113
- });
113
+ }));
114
114
  }
115
115
  var _default = ExampleGrid;
116
116
  exports.default = _default;
@@ -58,6 +58,7 @@ export type AzGridProps = {
58
58
  loading?: boolean;
59
59
  dark?: boolean;
60
60
  allowChildRows?: boolean;
61
+ looseCacheCheck?: boolean;
61
62
  className?: string;
62
63
  };
63
64
  export declare function AzGrid(props: AzGridProps): JSX.Element;
@@ -66,7 +66,6 @@ function AzGrid(props) {
66
66
  const dataSource = _react.default.useCallback(async dt => {
67
67
  let result_data = undefined;
68
68
  let isUsingCache = false;
69
- //console.log(dt);
70
69
  const cacheInfo = {
71
70
  ds: props.dataSource,
72
71
  skip: dt.skip,
@@ -74,18 +73,15 @@ function AzGrid(props) {
74
73
  };
75
74
  //check if we can used cached data
76
75
  //if the datasource is changed since last render, we should invalidate cache
77
- if (!(0, _object.object_equals)(last_cache_info.current, cacheInfo)) {
78
- //console.log('invalid cache', cacheInfo);
76
+ if (!validateCache(last_cache_info.current, cacheInfo, props.looseCacheCheck)) {
79
77
  cached_data.current = undefined;
80
78
  last_cache_info.current = cacheInfo;
81
79
  }
82
80
  if (cached_data.current) {
83
81
  //cache is valid, use the cache
84
- //console.log('use cache', cached_data.current);
85
82
  isUsingCache = true;
86
83
  result_data = cached_data.current;
87
84
  } else {
88
- //console.log('use ds');
89
85
  //cache is invalid, use the datasource again
90
86
  isUsingCache = false;
91
87
  if (typeof props.dataSource === 'function') {
@@ -225,6 +221,13 @@ function get_col_name(obj) {
225
221
  function get_default_key() {
226
222
  return 'ExpiryDate=2999-01-01,Z=1040577552';
227
223
  }
224
+ //check if two cache objects are the same
225
+ function validateCache(current, old, looseCheck = false) {
226
+ if (!old || !(0, _object.object_equals)(current, old)) return false;
227
+ if (looseCheck) return true;
228
+ if (current.ds != old.ds) return false;
229
+ return true;
230
+ }
228
231
  /**
229
232
  * merges together all different props for columns
230
233
  * to build a single final column data array
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@azrico/reactgrid",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "main": "dist/index.js",
5
5
  "description": "React Datagrid",
6
6
  "author": "Azrideus <MehrdadTavangar@gmail.com>",
package/src/example.tsx CHANGED
@@ -56,47 +56,54 @@ function getData() {
56
56
  }
57
57
  return fd;
58
58
  }
59
- //TODO REMOTE FILTER OFF ?
59
+
60
60
  export function ExampleGrid() {
61
- const [selected, setSelected] = React.useState(null);
61
+ const [ds_reset, set_ds_reset] = React.useState(0);
62
62
 
63
- const onSelectionChange = React.useCallback(({ selected }) => {
64
- setSelected(selected);
65
- }, []);
63
+ const dataSource = React.useCallback(
64
+ async (dt) => {
65
+ //console.log('Start loading');
66
+ return await new Promise((resolve) => {
67
+ setTimeout(() => {
68
+ //console.log('end loading');
69
+ resolve(getData());
70
+ }, 1000);
71
+ });
72
+ },
73
+ [ds_reset]
74
+ );
66
75
 
67
- const dataSource = React.useCallback(async (dt) => {
68
- console.log('Start loading');
69
- return await new Promise((resolve) => {
70
- setTimeout(() => {
71
- console.log('end loading');
72
- resolve(getData());
73
- }, 1000);
74
- });
75
- }, []);
76
+ function rst_ds() {
77
+ //console.log('rst_ds');
78
+ set_ds_reset((s) => s + 1);
79
+ }
76
80
 
77
81
  return (
78
- <AzGrid
79
- dark
80
- allowChildRows
81
- // onSelectionChange={onSelectionChange}
82
- // selected={selected}
83
- contextMenuItems={contextMenuItems}
84
- customColumns={customColumns}
85
- columns={columns}
86
- leftColumns={leftColumns}
87
- gridOptions={{
88
- defaultFilterValue: filterValue,
89
- remoteFilter: false,
90
- multiSelect: true,
91
- pagination: 'local',
92
- defaultLimit: 100,
93
- limit: 100,
94
- pageSizes: [100],
95
- rowIndexColumn: true,
96
- checkboxColumn: true,
97
- }}
98
- dataSource={dataSource}
99
- />
82
+ <div>
83
+ <div onClick={rst_ds}>RESET</div>
84
+ <AzGrid
85
+ dark
86
+ allowChildRows
87
+ // onSelectionChange={onSelectionChange}
88
+ // selected={selected}
89
+ contextMenuItems={contextMenuItems}
90
+ customColumns={customColumns}
91
+ columns={columns}
92
+ leftColumns={leftColumns}
93
+ gridOptions={{
94
+ defaultFilterValue: filterValue,
95
+ remoteFilter: false,
96
+ multiSelect: true,
97
+ pagination: 'local',
98
+ defaultLimit: 100,
99
+ limit: 100,
100
+ pageSizes: [100],
101
+ rowIndexColumn: true,
102
+ checkboxColumn: true,
103
+ }}
104
+ dataSource={dataSource}
105
+ />
106
+ </div>
100
107
  );
101
108
  }
102
109
  export default ExampleGrid;
@@ -90,7 +90,7 @@ export type AzGridProps = {
90
90
  loading?: boolean;
91
91
  dark?: boolean;
92
92
  allowChildRows?: boolean;
93
-
93
+ looseCacheCheck?: boolean;
94
94
  className?: string;
95
95
  };
96
96
 
@@ -162,28 +162,23 @@ export function AzGrid(props: AzGridProps) {
162
162
  let result_data: any = undefined;
163
163
 
164
164
  let isUsingCache = false;
165
- //console.log(dt);
165
+
166
166
  const cacheInfo = { ds: props.dataSource, skip: dt.skip, limit: dt.limit };
167
167
  //check if we can used cached data
168
168
  //if the datasource is changed since last render, we should invalidate cache
169
- if (!object_equals(last_cache_info.current, cacheInfo)) {
170
- //console.log('invalid cache', cacheInfo);
169
+ if (!validateCache(last_cache_info.current, cacheInfo, props.looseCacheCheck)) {
171
170
  cached_data.current = undefined;
172
171
  last_cache_info.current = cacheInfo;
173
172
  }
174
173
 
175
174
  if (cached_data.current) {
176
175
  //cache is valid, use the cache
177
- //console.log('use cache', cached_data.current);
178
-
179
176
  isUsingCache = true;
180
177
  result_data = cached_data.current;
181
178
  } else {
182
- //console.log('use ds');
183
-
184
179
  //cache is invalid, use the datasource again
185
- isUsingCache = false;
186
180
 
181
+ isUsingCache = false;
187
182
  if (typeof props.dataSource === 'function') {
188
183
  result_data = await props.dataSource(dt);
189
184
  } else result_data = props.dataSource;
@@ -339,6 +334,15 @@ function get_default_key() {
339
334
  return 'ExpiryDate=2999-01-01,Z=1040577552';
340
335
  }
341
336
 
337
+ //check if two cache objects are the same
338
+ function validateCache(current: any, old: any, looseCheck = false) {
339
+ if (!old || !object_equals(current, old)) return false;
340
+
341
+ if (looseCheck) return true;
342
+
343
+ if (current.ds != old.ds) return false;
344
+ return true;
345
+ }
342
346
  /**
343
347
  * merges together all different props for columns
344
348
  * to build a single final column data array