@azrico/reactgrid 1.3.52 → 1.4.0

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.
@@ -1,96 +1,96 @@
1
- import React from 'react';
2
- import { DataGridWrapperProps, GridType } from '../types/types';
3
- import { find_realData, get_row_id } from '../func/func_grid';
4
- import { func_selection } from '../func/func_child';
5
-
6
- function useGridMenuRow() {
7
- const [menuAnchorEl, set_menuAnchorEl] = React.useState<any>(null);
8
- const [menuRow, set_menuRow] = React.useState<any>(null);
9
- const update_menuRow = React.useCallback((r: any = null, el: any = null) => {
10
- set_menuRow(r || null);
11
- set_menuAnchorEl(el || null);
12
- }, []);
13
- return { update_menuRow, menuAnchorEl, menuRow };
14
- }
15
- export function useGridEvents(grid: GridType, props: DataGridWrapperProps) {
16
- const click_data = React.useRef<any>(null);
17
- const { update_menuRow, menuAnchorEl, menuRow } = useGridMenuRow();
18
-
19
- const refreshColumn = React.useCallback(
20
- (col: string) => {
21
- const dt = grid.ungroupedData;
22
- for (let index = 0; index < dt.length; index++) {
23
- (grid as any).setItemPropertyAt(index, col, dt[index][col]);
24
- }
25
- },
26
- [grid]
27
- );
28
- const refresh = React.useCallback(() => {
29
- const dt = grid.ungroupedData;
30
- for (let index = 0; index < dt.length; index++) {
31
- (grid as any).setItemAt(index, {}, { replace: false });
32
- }
33
- }, [grid]);
34
-
35
- React.useEffect(() => {
36
- if (!grid) return;
37
- grid['refresh'] = refresh;
38
- grid['refreshColumn'] = refreshColumn;
39
- }, [grid, props.dataSource, refresh, refreshColumn]);
40
- /* -------------------------------------------------------------------------- */
41
- /* events */
42
- /* -------------------------------------------------------------------------- */
43
- const onRowClick = React.useCallback(
44
- (rowProps, event) => {
45
- const nowDate = Date.now();
46
- const lastclick: number = click_data.current[get_row_id(rowProps)];
47
- if (lastclick) {
48
- const diffTime = Math.abs(nowDate - lastclick);
49
- if (diffTime < 400) {
50
- click_data.current[get_row_id(rowProps)] = null;
51
- return (
52
- props.onRowDoubleClick &&
53
- props.onRowDoubleClick(rowProps.rowIndex, rowProps.data)
54
- );
55
- }
56
- }
57
-
58
- click_data.current[get_row_id(rowProps)] = nowDate;
59
- return props.onRowClick && props.onRowClick(rowProps.rowIndex, rowProps.data);
60
- },
61
- [props.onRowClick, props.onRowDoubleClick]
62
- );
63
-
64
- React.useEffect(() => {
65
- click_data.current = {};
66
- }, [props.dataSource]);
67
-
68
- /* ------------------------------- ContextMenu ------------------------------ */
69
- const onRowContextMenu = React.useCallback((rowProps, event) => {
70
- if (!rowProps || !rowProps.data || func_selection.has_selection()) {
71
- update_menuRow(null, null);
72
- return true;
73
- }
74
- event.preventDefault();
75
-
76
- const realRow = find_realData(rowProps.dataSourceArray, event.target, rowProps.data);
77
- const anEl = func_selection.getVirtualElement(event.clientX, event.clientY);
78
- update_menuRow(realRow, anEl);
79
- return false;
80
- }, []);
81
-
82
- /* -------------------------------------------------------------------------- */
83
- const onRenderRow = React.useCallback((rowProps) => {
84
- // save the original handlers to be called later
85
- rowProps['data-row-id'] = get_row_id(rowProps.data);
86
- }, []);
87
-
88
- return {
89
- onRowClick,
90
- onRowContextMenu,
91
- onRenderRow,
92
- update_menuRow,
93
- menuAnchorEl,
94
- menuRow,
95
- };
96
- }
1
+ import React from 'react';
2
+ import { DataGridWrapperProps, GridType } from '../types/types';
3
+ import { find_realData, get_row_id } from '../func/func_grid';
4
+ import { func_selection } from '../func/func_child';
5
+
6
+ function useGridMenuRow() {
7
+ const [menuAnchorEl, set_menuAnchorEl] = React.useState<any>(null);
8
+ const [menuRow, set_menuRow] = React.useState<any>(null);
9
+ const update_menuRow = React.useCallback((r: any = null, el: any = null) => {
10
+ set_menuRow(r || null);
11
+ set_menuAnchorEl(el || null);
12
+ }, []);
13
+ return { update_menuRow, menuAnchorEl, menuRow };
14
+ }
15
+ export function useGridEvents(grid: GridType, props: DataGridWrapperProps) {
16
+ const click_data = React.useRef<any>(null);
17
+ const { update_menuRow, menuAnchorEl, menuRow } = useGridMenuRow();
18
+
19
+ const refreshColumn = React.useCallback(
20
+ (col: string) => {
21
+ const dt = grid.ungroupedData;
22
+ for (let index = 0; index < dt.length; index++) {
23
+ (grid as any).setItemPropertyAt(index, col, dt[index][col]);
24
+ }
25
+ },
26
+ [grid]
27
+ );
28
+ const refresh = React.useCallback(() => {
29
+ const dt = grid.ungroupedData;
30
+ for (let index = 0; index < dt.length; index++) {
31
+ (grid as any).setItemAt(index, {}, { replace: false });
32
+ }
33
+ }, [grid]);
34
+
35
+ React.useEffect(() => {
36
+ if (!grid) return;
37
+ grid['refresh'] = refresh;
38
+ grid['refreshColumn'] = refreshColumn;
39
+ }, [grid, props.dataSource, refresh, refreshColumn]);
40
+ /* -------------------------------------------------------------------------- */
41
+ /* events */
42
+ /* -------------------------------------------------------------------------- */
43
+ const onRowClick = React.useCallback(
44
+ (rowProps, event) => {
45
+ const nowDate = Date.now();
46
+ const lastclick: number = click_data.current[get_row_id(rowProps)];
47
+ if (lastclick) {
48
+ const diffTime = Math.abs(nowDate - lastclick);
49
+ if (diffTime < 400) {
50
+ click_data.current[get_row_id(rowProps)] = null;
51
+ return (
52
+ props.onRowDoubleClick &&
53
+ props.onRowDoubleClick(rowProps.rowIndex, rowProps.data)
54
+ );
55
+ }
56
+ }
57
+
58
+ click_data.current[get_row_id(rowProps)] = nowDate;
59
+ return props.onRowClick && props.onRowClick(rowProps.rowIndex, rowProps.data);
60
+ },
61
+ [props.onRowClick, props.onRowDoubleClick]
62
+ );
63
+
64
+ React.useEffect(() => {
65
+ click_data.current = {};
66
+ }, [props.dataSource]);
67
+
68
+ /* ------------------------------- ContextMenu ------------------------------ */
69
+ const onRowContextMenu = React.useCallback((rowProps, event) => {
70
+ if (!rowProps || !rowProps.data || func_selection.has_selection()) {
71
+ update_menuRow(null, null);
72
+ return true;
73
+ }
74
+ event.preventDefault();
75
+
76
+ const realRow = find_realData(rowProps.dataSourceArray, event.target, rowProps.data);
77
+ const anEl = func_selection.getVirtualElement(event.clientX, event.clientY);
78
+ update_menuRow(realRow, anEl);
79
+ return false;
80
+ }, []);
81
+
82
+ /* -------------------------------------------------------------------------- */
83
+ const onRenderRow = React.useCallback((rowProps) => {
84
+ // save the original handlers to be called later
85
+ rowProps['data-row-id'] = get_row_id(rowProps.data);
86
+ }, []);
87
+
88
+ return {
89
+ onRowClick,
90
+ onRowContextMenu,
91
+ onRenderRow,
92
+ update_menuRow,
93
+ menuAnchorEl,
94
+ menuRow,
95
+ };
96
+ }
@@ -1,79 +1,79 @@
1
- import React from 'react';
2
- import { AzGridProps, DataGridWrapperProps, GridType } from '../types/types';
3
- import debounce from 'lodash.debounce';
4
- import { array_intersection, fn_eval } from '@azrico/object';
5
-
6
- export function useGridSearch(grid: GridType, props: DataGridWrapperProps) {
7
- const [search, set_search] = React.useState('');
8
- const [filteredData, set_filteredData] = React.useState<any>([]);
9
- const currentSearch = React.useRef('');
10
- const currentDS = React.useRef<any[]>([]);
11
-
12
- const try_search = React.useCallback(
13
- debounce(() => {
14
- if (!Array.isArray(currentDS.current)) {
15
- return;
16
- }
17
- let newData: any[] = [];
18
- if (currentSearch.current) {
19
- const visibleColumns = grid.visibleColumns;
20
- const columns_to_search: string[] = array_intersection(
21
- props.advancedFilterFields || [],
22
- visibleColumns.map((r) => r.id)
23
- );
24
- const lowerSearchText = currentSearch.current.toLowerCase().trim();
25
- if (!lowerSearchText || columns_to_search.length === 0)
26
- newData = currentDS.current;
27
- else {
28
- newData = currentDS.current.filter((p) => {
29
- for (let index = 0; index < columns_to_search.length; index++) {
30
- const col = columns_to_search[index];
31
- const v = (p[col] + '').toLowerCase(); //value of column
32
- if (v && v.indexOf(lowerSearchText) != -1) {
33
- //if one column is matching then the row is a match
34
- return true;
35
- }
36
- }
37
- return false;
38
- });
39
- }
40
- } else newData = currentDS.current;
41
- set_filteredData(newData);
42
- }, 1000),
43
- [grid]
44
- );
45
- React.useEffect(() => {
46
- fn_eval(props.dataSource).then((r) => {
47
- currentDS.current = typeof r === 'object' ? r.data || r : r;
48
- searchIfNeeded();
49
- });
50
- }, [props.dataSource]);
51
-
52
- React.useEffect(() => {
53
- currentSearch.current = search;
54
- searchIfNeeded();
55
- }, [search]);
56
-
57
- function searchIfNeeded() {
58
- if (!search) {
59
- set_filteredData(props.dataSource);
60
- } else try_search();
61
- }
62
-
63
- const element = (
64
- <>
65
- <form className={'azgrid-searcharea'}>
66
- <label>
67
- Search
68
- <input
69
- type="text"
70
- name="name"
71
- value={search}
72
- onChange={(e) => set_search(e.target.value)}
73
- />
74
- </label>
75
- </form>
76
- </>
77
- );
78
- return { filteredData, element };
79
- }
1
+ import React from 'react';
2
+ import { AzGridProps, DataGridWrapperProps, GridType } from '../types/types';
3
+ import debounce from 'lodash.debounce';
4
+ import { array_intersection, fn_eval } from '@azrico/object';
5
+
6
+ export function useGridSearch(grid: GridType, props: DataGridWrapperProps) {
7
+ const [search, set_search] = React.useState('');
8
+ const [filteredData, set_filteredData] = React.useState<any>([]);
9
+ const currentSearch = React.useRef('');
10
+ const currentDS = React.useRef<any[]>([]);
11
+
12
+ const try_search = React.useCallback(
13
+ debounce(() => {
14
+ if (!Array.isArray(currentDS.current)) {
15
+ return;
16
+ }
17
+ let newData: any[] = [];
18
+ if (currentSearch.current) {
19
+ const visibleColumns = grid.visibleColumns;
20
+ const columns_to_search: string[] = array_intersection(
21
+ props.advancedFilterFields || [],
22
+ visibleColumns.map((r) => r.id)
23
+ );
24
+ const lowerSearchText = currentSearch.current.toLowerCase().trim();
25
+ if (!lowerSearchText || columns_to_search.length === 0)
26
+ newData = currentDS.current;
27
+ else {
28
+ newData = currentDS.current.filter((p) => {
29
+ for (let index = 0; index < columns_to_search.length; index++) {
30
+ const col = columns_to_search[index];
31
+ const v = (p[col] + '').toLowerCase(); //value of column
32
+ if (v && v.indexOf(lowerSearchText) != -1) {
33
+ //if one column is matching then the row is a match
34
+ return true;
35
+ }
36
+ }
37
+ return false;
38
+ });
39
+ }
40
+ } else newData = currentDS.current;
41
+ set_filteredData(newData);
42
+ }, 1000),
43
+ [grid]
44
+ );
45
+ React.useEffect(() => {
46
+ fn_eval(props.dataSource).then((r) => {
47
+ currentDS.current = typeof r === 'object' ? r.data || r : r;
48
+ searchIfNeeded();
49
+ });
50
+ }, [props.dataSource]);
51
+
52
+ React.useEffect(() => {
53
+ currentSearch.current = search;
54
+ searchIfNeeded();
55
+ }, [search]);
56
+
57
+ function searchIfNeeded() {
58
+ if (!search) {
59
+ set_filteredData(props.dataSource);
60
+ } else try_search();
61
+ }
62
+
63
+ const element = (
64
+ <>
65
+ <form className={'azgrid-searcharea'}>
66
+ <label>
67
+ Search
68
+ <input
69
+ type="text"
70
+ name="name"
71
+ value={search}
72
+ onChange={(e) => set_search(e.target.value)}
73
+ />
74
+ </label>
75
+ </form>
76
+ </>
77
+ );
78
+ return { filteredData, element };
79
+ }
package/src/index.tsx CHANGED
@@ -4,7 +4,6 @@ export * from './types/types';
4
4
  export { get_row_id, set_row_id, get_col_name, get_col_header } from './func/func_grid';
5
5
 
6
6
  import AzGrid from './views/DataGrid';
7
- import ExampleGrid from './example';
8
7
  import React from 'react';
9
8
  import ReactDOM from 'react-dom/client';
10
9
 
@@ -12,8 +11,9 @@ import ReactDOM from 'react-dom/client';
12
11
  //
13
12
  export default AzGrid;
14
13
 
15
- const testroot = document.getElementById('testroot');
14
+ const testroot = typeof document === 'undefined' ? null : document.getElementById('testroot');
16
15
  if (testroot) {
17
- const root = ReactDOM.createRoot(document.getElementById('testroot') as HTMLElement);
18
- root.render(<ExampleGrid />);
16
+ const root = ReactDOM.createRoot(testroot);
17
+ // Example styles, including the optional token theme, load only for the demo.
18
+ import('./example').then(({ default: ExampleGrid }) => root.render(<ExampleGrid />));
19
19
  }