@addev-be/ui 0.2.5 → 0.2.7

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.
Files changed (166) hide show
  1. package/assets/icons/arrow-down-1-9.svg +1 -0
  2. package/assets/icons/arrow-down-a-z.svg +1 -1
  3. package/assets/icons/arrow-down-big-small.svg +1 -0
  4. package/assets/icons/arrow-up-9-1.svg +1 -0
  5. package/assets/icons/arrow-up-big-small.svg +1 -0
  6. package/assets/icons/arrow-up-z-a.svg +1 -1
  7. package/assets/icons/check.svg +1 -1
  8. package/assets/icons/chevron-down.svg +1 -0
  9. package/assets/icons/down.svg +1 -1
  10. package/assets/icons/ellipsis.svg +1 -0
  11. package/assets/icons/filter-full.svg +1 -1
  12. package/assets/icons/filter.svg +1 -1
  13. package/assets/icons/hashtag.svg +1 -1
  14. package/assets/icons/image-slash.svg +1 -1
  15. package/assets/icons/left.svg +1 -1
  16. package/assets/icons/magnifier.svg +1 -1
  17. package/assets/icons/phone.svg +1 -1
  18. package/assets/icons/right.svg +1 -1
  19. package/assets/icons/sigma.svg +1 -0
  20. package/assets/icons/spinner-third.svg +1 -1
  21. package/assets/icons/table-columns.svg +1 -1
  22. package/assets/icons/table-footer-slash.svg +5 -0
  23. package/assets/icons/table-footer.svg +4 -0
  24. package/assets/icons/table.svg +1 -0
  25. package/assets/icons/tally.svg +1 -0
  26. package/assets/icons/up.svg +1 -1
  27. package/assets/icons/user-tie.svg +1 -1
  28. package/assets/icons/x-bar.svg +4 -0
  29. package/dist/Icons.d.ts +13 -1
  30. package/dist/Icons.js +25 -1
  31. package/dist/components/data/AdvancedRequestDataGrid/index.js +3 -3
  32. package/dist/components/data/DataGrid/AdvancedRequestDataGrid.d.ts +1 -2
  33. package/dist/components/data/DataGrid/AdvancedRequestDataGrid.js +3 -5
  34. package/dist/components/data/DataGrid/DataGridCell.d.ts +1 -1
  35. package/dist/components/data/DataGrid/DataGridCell.js +5 -5
  36. package/dist/components/data/DataGrid/DataGridColumnsModal/hooks.js +2 -1
  37. package/dist/components/data/DataGrid/DataGridEditableCell.js +2 -7
  38. package/dist/components/data/DataGrid/DataGridFilterMenu/index.js +90 -10
  39. package/dist/components/data/DataGrid/DataGridFilterMenu/styles.d.ts +3 -9
  40. package/dist/components/data/DataGrid/DataGridFilterMenu/styles.js +10 -37
  41. package/dist/components/data/DataGrid/DataGridFooter.d.ts +1 -1
  42. package/dist/components/data/DataGrid/DataGridFooter.js +35 -22
  43. package/dist/components/data/DataGrid/DataGridHeader.js +1 -3
  44. package/dist/components/data/DataGrid/DataGridHeaderCell.js +8 -23
  45. package/dist/components/data/DataGrid/FilterModalContent/index.d.ts +5 -5
  46. package/dist/components/data/DataGrid/FilterModalContent/index.js +11 -9
  47. package/dist/components/data/DataGrid/FilterValuesScroller.d.ts +8 -7
  48. package/dist/components/data/DataGrid/FilterValuesScroller.js +5 -6
  49. package/dist/components/data/DataGrid/helpers/columns.d.ts +1 -1
  50. package/dist/components/data/DataGrid/helpers/columns.js +72 -20
  51. package/dist/components/data/DataGrid/helpers/filters.d.ts +4 -3
  52. package/dist/components/data/DataGrid/helpers/filters.js +13 -5
  53. package/dist/components/data/DataGrid/hooks/useDataGrid.d.ts +1 -1
  54. package/dist/components/data/DataGrid/hooks/useDataGrid.js +60 -30
  55. package/dist/components/data/DataGrid/hooks/useDataGridCopy.d.ts +2 -2
  56. package/dist/components/data/DataGrid/hooks/useDataGridCopy.js +41 -40
  57. package/dist/components/data/DataGrid/hooks/useDataGridSettings.js +0 -1
  58. package/dist/components/data/DataGrid/index.d.ts +4 -2
  59. package/dist/components/data/DataGrid/index.js +38 -17
  60. package/dist/components/data/DataGrid/styles.d.ts +12 -5
  61. package/dist/components/data/DataGrid/styles.js +31 -18
  62. package/dist/components/data/DataGrid/types.d.ts +28 -16
  63. package/dist/components/data/SqlRequestDataGrid/helpers/columns.d.ts +1 -1
  64. package/dist/components/data/SqlRequestDataGrid/helpers/columns.js +30 -19
  65. package/dist/components/data/SqlRequestDataGrid/index.js +125 -37
  66. package/dist/components/data/SqlRequestDataGrid/types.d.ts +1 -0
  67. package/dist/components/data/index.d.ts +2 -0
  68. package/dist/components/data/index.js +2 -0
  69. package/dist/components/ui/ContextMenu/index.d.ts +11 -0
  70. package/dist/components/ui/ContextMenu/index.js +58 -0
  71. package/dist/components/ui/ContextMenu/styles.d.ts +18 -0
  72. package/dist/components/ui/ContextMenu/styles.js +56 -0
  73. package/dist/helpers/dates.d.ts +2 -0
  74. package/dist/helpers/dates.js +13 -0
  75. package/dist/services/advancedRequests.d.ts +1 -1
  76. package/dist/services/sqlRequests.d.ts +9 -4
  77. package/dist/services/sqlRequests.js +1 -0
  78. package/package.json +1 -1
  79. package/src/Icons.tsx +104 -80
  80. package/src/components/data/AdvancedRequestDataGrid/helpers/advancedRequests.ts +93 -93
  81. package/src/components/data/AdvancedRequestDataGrid/helpers/columns.tsx +262 -262
  82. package/src/components/data/AdvancedRequestDataGrid/helpers/index.ts +2 -2
  83. package/src/components/data/AdvancedRequestDataGrid/index.tsx +267 -269
  84. package/src/components/data/AdvancedRequestDataGrid/types.ts +47 -47
  85. package/src/components/data/DataGrid/DataGridCell.tsx +73 -73
  86. package/src/components/data/DataGrid/DataGridColumnsModal/helpers.ts +14 -14
  87. package/src/components/data/DataGrid/DataGridColumnsModal/hooks.tsx +59 -58
  88. package/src/components/data/DataGrid/DataGridColumnsModal/index.tsx +181 -181
  89. package/src/components/data/DataGrid/DataGridColumnsModal/styles.ts +104 -104
  90. package/src/components/data/DataGrid/DataGridEditableCell.tsx +43 -54
  91. package/src/components/data/DataGrid/DataGridFilterMenu/hooks.tsx +75 -75
  92. package/src/components/data/DataGrid/DataGridFilterMenu/index.tsx +364 -190
  93. package/src/components/data/DataGrid/DataGridFilterMenu/styles.ts +49 -100
  94. package/src/components/data/DataGrid/DataGridFooter.tsx +42 -64
  95. package/src/components/data/DataGrid/DataGridHeader.tsx +126 -126
  96. package/src/components/data/DataGrid/DataGridHeaderCell.tsx +132 -167
  97. package/src/components/data/DataGrid/FilterModalContent/index.tsx +124 -125
  98. package/src/components/data/DataGrid/FilterModalContent/styles.ts +22 -22
  99. package/src/components/data/DataGrid/FilterValuesScroller.tsx +133 -131
  100. package/src/components/data/DataGrid/VirtualScroller.tsx +46 -46
  101. package/src/components/data/DataGrid/helpers/columns.tsx +287 -196
  102. package/src/components/data/DataGrid/helpers/filters.ts +220 -207
  103. package/src/components/data/DataGrid/helpers/index.ts +2 -2
  104. package/src/components/data/DataGrid/hooks/index.ts +30 -30
  105. package/src/components/data/DataGrid/hooks/useDataGrid.tsx +305 -264
  106. package/src/components/data/DataGrid/hooks/useDataGridCopy.ts +170 -165
  107. package/src/components/data/DataGrid/hooks/useDataGridSettings.ts +48 -48
  108. package/src/components/data/DataGrid/index.tsx +139 -132
  109. package/src/components/data/DataGrid/styles.ts +347 -326
  110. package/src/components/data/DataGrid/types.ts +260 -240
  111. package/src/components/data/SqlRequestDataGrid/helpers/columns.tsx +259 -224
  112. package/src/components/data/SqlRequestDataGrid/helpers/index.ts +2 -2
  113. package/src/components/data/SqlRequestDataGrid/helpers/sqlRequests.ts +16 -16
  114. package/src/components/data/SqlRequestDataGrid/index.tsx +347 -252
  115. package/src/components/data/SqlRequestDataGrid/types.ts +47 -47
  116. package/src/components/data/index.ts +8 -8
  117. package/src/components/forms/Button.tsx +99 -99
  118. package/src/components/forms/IconButton.tsx +56 -56
  119. package/src/components/forms/IndeterminateCheckbox.tsx +46 -46
  120. package/src/components/forms/Select.tsx +40 -40
  121. package/src/components/forms/index.ts +5 -5
  122. package/src/components/forms/styles.ts +20 -20
  123. package/src/components/index.ts +3 -3
  124. package/src/components/layout/Dropdown/index.tsx +79 -79
  125. package/src/components/layout/Dropdown/styles.ts +44 -44
  126. package/src/components/layout/Loading/index.tsx +29 -29
  127. package/src/components/layout/Loading/styles.ts +29 -29
  128. package/src/components/layout/Modal/index.tsx +51 -51
  129. package/src/components/layout/Modal/styles.ts +110 -110
  130. package/src/components/layout/index.ts +3 -3
  131. package/src/components/ui/ContextMenu/index.tsx +73 -0
  132. package/src/components/ui/ContextMenu/styles.ts +115 -0
  133. package/src/config/index.ts +14 -14
  134. package/src/helpers/dates.ts +9 -9
  135. package/src/helpers/getScrollbarSize.ts +14 -14
  136. package/src/helpers/numbers.ts +20 -20
  137. package/src/hooks/index.ts +2 -2
  138. package/src/hooks/useElementSize.ts +24 -24
  139. package/src/hooks/useWindowSize.ts +20 -20
  140. package/src/index.ts +7 -7
  141. package/src/providers/PortalsProvider/index.tsx +54 -54
  142. package/src/providers/PortalsProvider/styles.ts +27 -27
  143. package/src/providers/SettingsProvider/index.tsx +70 -70
  144. package/src/providers/ThemeProvider/ThemeProvider.ts +55 -55
  145. package/src/providers/ThemeProvider/defaultTheme.ts +444 -444
  146. package/src/providers/ThemeProvider/index.ts +3 -3
  147. package/src/providers/ThemeProvider/types.ts +123 -123
  148. package/src/providers/UiProviders/index.tsx +65 -65
  149. package/src/providers/UiProviders/styles.ts +10 -10
  150. package/src/providers/hooks.ts +8 -8
  151. package/src/providers/index.ts +5 -5
  152. package/src/services/HttpService.ts +80 -80
  153. package/src/services/WebSocketService.ts +147 -147
  154. package/src/services/advancedRequests.ts +101 -101
  155. package/src/services/base.ts +31 -31
  156. package/src/services/hooks.ts +23 -23
  157. package/src/services/index.ts +2 -2
  158. package/src/services/sqlRequests.ts +110 -99
  159. package/src/styles/animations.scss +30 -30
  160. package/src/styles/index.scss +42 -42
  161. package/src/typings.d.ts +6 -6
  162. package/tsconfig.tsbuildinfo +1 -1
  163. package/dist/components/data/DataGrid/helpers.d.ts +0 -28
  164. package/dist/components/data/DataGrid/helpers.js +0 -436
  165. package/dist/config/types.d.ts +0 -11
  166. package/dist/config/types.js +0 -2
@@ -1,264 +1,305 @@
1
- /* eslint-disable @typescript-eslint/no-unnecessary-type-constraint */
2
- /* eslint-disable @typescript-eslint/no-explicit-any */
3
-
4
- import { DEFAULT_ROW_HEIGHT, VIRTUAL_SCROLL_TOLERANCE } from '../styles';
5
- import {
6
- DataGridColumns,
7
- DataGridContext,
8
- DataGridContextProps,
9
- DataGridFilters,
10
- DataGridProps,
11
- DataGridSort,
12
- } from '../types';
13
- import {
14
- createContext,
15
- useCallback,
16
- useEffect,
17
- useMemo,
18
- useRef,
19
- useState,
20
- } from 'react';
21
- import { useDataGridCopy, useSortedColumns } from '.';
22
-
23
- import { applyFilter } from '../helpers';
24
- import { merge } from 'lodash';
25
- import { useDataGridSettings } from './useDataGridSettings';
26
- import { useElementSize } from '../../../../hooks';
27
-
28
- export const useDataGrid = <R,>(
29
- props: DataGridProps<R>
30
- ): [DataGridContextProps<R>, DataGridContext<R>] => {
31
- const {
32
- rows,
33
- onFiltersChanged,
34
- onSortsChanged,
35
- loadCopyRows,
36
- rowHeight = DEFAULT_ROW_HEIGHT,
37
- onVisibleRowsChange,
38
- onSelectionChange,
39
- selectable,
40
- initialSorts,
41
- } = props;
42
- const [columns, setColumns] = useState<DataGridColumns<R>>(props.columns);
43
-
44
- const visibleColumns = useSortedColumns(columns);
45
-
46
- /** SETTINGS */
47
-
48
- const { settings, setSettings, saveSettings } = useDataGridSettings(
49
- props.name
50
- );
51
-
52
- const setColumnWidth = useCallback(
53
- (key: string, width: number) => {
54
- setSettings((prev) => ({ ...prev, [key]: { ...prev[key], width } }));
55
- },
56
- [setSettings]
57
- );
58
-
59
- const gridTemplateColumns = useMemo(
60
- () =>
61
- [
62
- ...(selectable ? ['var(--space-8)'] : []),
63
- ...visibleColumns.map(([, col]) => `${col.width ?? 150}px`),
64
- ].join(' '),
65
- [selectable, visibleColumns]
66
- );
67
-
68
- // Update columns when settings change
69
- useEffect(() => {
70
- setColumns((prev) => merge({}, prev, settings));
71
- }, [settings]);
72
-
73
- /** ROWS SELECTION **/
74
-
75
- const rowKeyGetter = useMemo(
76
- () =>
77
- typeof props.rowKey === 'function'
78
- ? props.rowKey
79
- : (row: R) => String(row?.[props.rowKey as keyof R]),
80
- [props.rowKey]
81
- );
82
- const [selectedKeys, setSelectedKeys] = useState<string[]>([]);
83
-
84
- const selectedRows = useMemo(
85
- () => props.rows.filter((row) => selectedKeys.includes(rowKeyGetter(row))),
86
- [props.rows, rowKeyGetter, selectedKeys]
87
- );
88
-
89
- useEffect(() => {
90
- onSelectionChange?.(selectedKeys);
91
- }, [onSelectionChange, selectedKeys]);
92
-
93
- /** ROWS FILTERING **/
94
-
95
- const [filters, setFilters] = useState<DataGridFilters>({});
96
- const [filteredRows, setFilteredRows] = useState<R[]>(rows ?? []);
97
-
98
- useEffect(() => {
99
- onFiltersChanged?.(filters);
100
- }, [filters, onFiltersChanged]);
101
-
102
- useEffect(() => {
103
- const filteredRows =
104
- props.filter === false
105
- ? rows
106
- : Object.entries(filters).reduce(
107
- (tempRows, [, filter]) =>
108
- applyFilter(tempRows, filter as any /* TODO : fix typings */),
109
- rows
110
- );
111
- setFilteredRows(filteredRows ?? []);
112
- }, [filters, props.filter, rows]);
113
-
114
- /** CELL EDITING */
115
-
116
- const [editingCell, setEditingCell] = useState<[number, number]>([-1, -1]);
117
-
118
- /** ROWS SORTING **/
119
-
120
- const [sorts, setSorts] = useState<Record<string, DataGridSort>>(
121
- initialSorts ?? {}
122
- );
123
- const [sortedRows, setSortedRows] = useState<R[]>(rows);
124
-
125
- useEffect(() => {
126
- const sortedRows = [...filteredRows];
127
- if (props.sort !== false) {
128
- Object.entries(sorts).forEach(([sortKey, sort]) => {
129
- // Get the sort column and make sure it exists and is sortable
130
- const sortColumn = columns[sortKey];
131
- if (!sortColumn || typeof sortColumn.sortGetter !== 'function') return;
132
-
133
- // Sort the rows using the sortGetter function of the column
134
- const sortDirection = sort === 'asc' ? 1 : -1;
135
- sortedRows.sort((a: R, b: R) => {
136
- const aValue = sortColumn.sortGetter!(a);
137
- const bValue = sortColumn.sortGetter!(b);
138
- if (aValue === bValue) return 0;
139
- return aValue > bValue ? sortDirection : -sortDirection;
140
- });
141
- });
142
- }
143
- setSortedRows(sortedRows);
144
- }, [columns, filteredRows, sorts, onSortsChanged, props.sort]);
145
-
146
- useEffect(() => {
147
- onSortsChanged?.(sorts);
148
- }, [sorts, onSortsChanged]);
149
-
150
- /** VIRTUAL SCROLLING */
151
-
152
- const scrollableRef = useRef<HTMLTableSectionElement | null>(null);
153
- const { height } = useElementSize(scrollableRef.current);
154
-
155
- const [scrollTop, setScrollTop] = useState(0);
156
- const [index, length] =
157
- // props.showAllRows ? [0, sortedRows.length] :
158
- [Math.floor(scrollTop / rowHeight), Math.ceil(height / rowHeight)];
159
-
160
- const onScroll = useCallback((e: React.UIEvent<HTMLDivElement>) => {
161
- const target = e.target as HTMLDivElement;
162
- setScrollTop(target.scrollTop);
163
- }, []);
164
-
165
- const indexWithTolerance = Math.max(0, index - VIRTUAL_SCROLL_TOLERANCE);
166
- const lengthWithTolerance = length + 2 * VIRTUAL_SCROLL_TOLERANCE;
167
- const visibleRows = sortedRows.slice(
168
- indexWithTolerance,
169
- indexWithTolerance + lengthWithTolerance
170
- );
171
-
172
- useEffect(() => {
173
- onVisibleRowsChange?.(indexWithTolerance, lengthWithTolerance);
174
- }, [indexWithTolerance, lengthWithTolerance, onVisibleRowsChange]);
175
-
176
- /** COPYING */
177
-
178
- const { copyTable } = useDataGridCopy(sortedRows, columns, loadCopyRows);
179
-
180
- const contextValue = useMemo<DataGridContextProps<R>>(
181
- () => ({
182
- ...props,
183
- columns,
184
- visibleColumns,
185
- sortedRows,
186
- selectedRows,
187
- selectedKeys,
188
- setSelectedKeys,
189
- sorts,
190
- setSorts,
191
- filters,
192
- setFilters,
193
- editingCell,
194
- setEditingCell,
195
- copyTable,
196
- setColumnWidth,
197
- settings,
198
- setSettings,
199
- saveSettings,
200
- visibleRows,
201
- scrollableRef,
202
- onScroll,
203
- index,
204
- length,
205
- rowKeyGetter,
206
- gridTemplateColumns,
207
- }),
208
- [
209
- columns,
210
- copyTable,
211
- editingCell,
212
- filters,
213
- index,
214
- length,
215
- onScroll,
216
- props,
217
- rowKeyGetter,
218
- saveSettings,
219
- selectedKeys,
220
- selectedRows,
221
- setColumnWidth,
222
- setSettings,
223
- settings,
224
- sortedRows,
225
- sorts,
226
- visibleColumns,
227
- visibleRows,
228
- gridTemplateColumns,
229
- ]
230
- );
231
-
232
- const context = useMemo(
233
- () =>
234
- createContext<DataGridContextProps<R>>({
235
- rows: [],
236
- columns: {},
237
- visibleColumns: [],
238
- rowKey: '' as keyof R,
239
- rowKeyGetter: () => '',
240
- sortedRows: [],
241
- selectedRows: [],
242
- selectedKeys: [],
243
- setSelectedKeys: () => {},
244
- setSorts: () => {},
245
- setFilters: () => {},
246
- editingCell: [-1, -1],
247
- setEditingCell: () => {},
248
- copyTable: async () => {},
249
- setColumnWidth: () => {},
250
- saveSettings: () => {},
251
- settings: {},
252
- setSettings: () => {},
253
- visibleRows: [],
254
- scrollableRef: { current: null },
255
- onScroll: () => {},
256
- index: 0,
257
- length: 0,
258
- gridTemplateColumns: '',
259
- }),
260
- []
261
- );
262
-
263
- return useMemo(() => [contextValue, context], [context, contextValue]);
264
- };
1
+ /* eslint-disable @typescript-eslint/no-unnecessary-type-constraint */
2
+ /* eslint-disable @typescript-eslint/no-explicit-any */
3
+
4
+ import { DEFAULT_ROW_HEIGHT, VIRTUAL_SCROLL_TOLERANCE } from '../styles';
5
+ import {
6
+ DataGridColumns,
7
+ DataGridContext,
8
+ DataGridContextProps,
9
+ DataGridFilters,
10
+ DataGridFooterFunction,
11
+ DataGridProps,
12
+ DataGridSort,
13
+ } from '../types';
14
+ import {
15
+ createContext,
16
+ useCallback,
17
+ useEffect,
18
+ useMemo,
19
+ useRef,
20
+ useState,
21
+ } from 'react';
22
+ import { useDataGridCopy, useSortedColumns } from '.';
23
+
24
+ import { applyFilter } from '../helpers';
25
+ import { merge } from 'lodash';
26
+ import { useDataGridSettings } from './useDataGridSettings';
27
+ import { useElementSize } from '../../../../hooks';
28
+
29
+ export const useDataGrid = <R,>(
30
+ props: DataGridProps<R>,
31
+ override?: Partial<DataGridContextProps<R>>
32
+ ): [DataGridContextProps<R>, DataGridContext<R>] => {
33
+ const {
34
+ rows,
35
+ onFiltersChanged,
36
+ onSortsChanged,
37
+ loadCopyRows,
38
+ rowHeight = DEFAULT_ROW_HEIGHT,
39
+ onVisibleRowsChange,
40
+ onSelectionChange,
41
+ selectable,
42
+ initialSorts,
43
+ } = props;
44
+ const [columns, setColumns] = useState<DataGridColumns<R>>(props.columns);
45
+
46
+ const visibleColumns = useSortedColumns(columns);
47
+
48
+ /** SETTINGS */
49
+
50
+ const { settings, setSettings, saveSettings } = useDataGridSettings(
51
+ props.name
52
+ );
53
+
54
+ const setColumnWidth = useCallback(
55
+ (key: string, width: number) => {
56
+ setSettings((prev) => ({ ...prev, [key]: { ...prev[key], width } }));
57
+ },
58
+ [setSettings]
59
+ );
60
+
61
+ const gridTemplateColumns = useMemo(
62
+ () =>
63
+ [
64
+ ...(selectable ? ['var(--space-10)'] : []),
65
+ ...visibleColumns.map(([, col]) => `${col.width ?? 150}px`),
66
+ ].join(' '),
67
+ [selectable, visibleColumns]
68
+ );
69
+
70
+ // Update columns when settings change
71
+ useEffect(() => {
72
+ setColumns((prev) => merge({}, prev, settings));
73
+ }, [settings]);
74
+
75
+ /** ROWS SELECTION **/
76
+
77
+ const rowKeyGetter = useMemo(
78
+ () =>
79
+ typeof props.rowKey === 'function'
80
+ ? props.rowKey
81
+ : (row: R) => String(row?.[props.rowKey as keyof R]),
82
+ [props.rowKey]
83
+ );
84
+ const [selectedKeys, setSelectedKeys] = useState<string[]>([]);
85
+
86
+ const selectedRows = useMemo(
87
+ () => props.rows.filter((row) => selectedKeys.includes(rowKeyGetter(row))),
88
+ [props.rows, rowKeyGetter, selectedKeys]
89
+ );
90
+
91
+ useEffect(() => {
92
+ onSelectionChange?.(selectedKeys);
93
+ }, [onSelectionChange, selectedKeys]);
94
+
95
+ /** ROWS FILTERING **/
96
+
97
+ const [filters, setFilters] = useState<DataGridFilters>({});
98
+ const [filteredRows, setFilteredRows] = useState<R[]>(rows ?? []);
99
+
100
+ useEffect(() => {
101
+ onFiltersChanged?.(filters);
102
+ }, [filters, onFiltersChanged]);
103
+
104
+ useEffect(() => {
105
+ const filteredRows =
106
+ props.filter === false
107
+ ? rows
108
+ : Object.entries(filters).reduce(
109
+ (tempRows, [, filter]) =>
110
+ applyFilter(tempRows, filter as any /* TODO : fix typings */),
111
+ rows
112
+ );
113
+ setFilteredRows(filteredRows ?? []);
114
+ }, [filters, props.filter, rows]);
115
+
116
+ /** CELL EDITING */
117
+
118
+ const [editingCell, setEditingCell] = useState<[number, number]>([-1, -1]);
119
+
120
+ /** ROWS SORTING **/
121
+
122
+ const [sorts, setSorts] = useState<Record<string, DataGridSort>>(
123
+ initialSorts ?? {}
124
+ );
125
+ const [sortedRows, setSortedRows] = useState<R[]>(rows);
126
+
127
+ useEffect(() => {
128
+ const sortedRows = [...filteredRows];
129
+ if (props.sort !== false) {
130
+ Object.entries(sorts).forEach(([sortKey, sort]) => {
131
+ // Get the sort column and make sure it exists and is sortable
132
+ const sortColumn = columns[sortKey];
133
+ if (!sortColumn || typeof sortColumn.sortGetter !== 'function') return;
134
+
135
+ // Sort the rows using the sortGetter function of the column
136
+ const sortDirection = sort === 'asc' ? 1 : -1;
137
+ sortedRows.sort((a: R, b: R) => {
138
+ const aValue = sortColumn.sortGetter!(a);
139
+ const bValue = sortColumn.sortGetter!(b);
140
+ if (aValue === bValue) return 0;
141
+ return aValue > bValue ? sortDirection : -sortDirection;
142
+ });
143
+ });
144
+ }
145
+ setSortedRows(sortedRows);
146
+ }, [columns, filteredRows, sorts, onSortsChanged, props.sort]);
147
+
148
+ useEffect(() => {
149
+ onSortsChanged?.(sorts);
150
+ }, [sorts, onSortsChanged]);
151
+
152
+ /** VIRTUAL SCROLLING */
153
+
154
+ const scrollableRef = useRef<HTMLTableSectionElement | null>(null);
155
+ const { height } = useElementSize(scrollableRef.current);
156
+
157
+ const [scrollTop, setScrollTop] = useState(0);
158
+ const [index, length] =
159
+ // props.showAllRows ? [0, sortedRows.length] :
160
+ [Math.floor(scrollTop / rowHeight), Math.ceil(height / rowHeight)];
161
+
162
+ const onScroll = useCallback((e: React.UIEvent<HTMLDivElement>) => {
163
+ const target = e.target as HTMLDivElement;
164
+ setScrollTop(target.scrollTop);
165
+ }, []);
166
+
167
+ const indexWithTolerance = Math.max(0, index - VIRTUAL_SCROLL_TOLERANCE);
168
+ const lengthWithTolerance = length + 2 * VIRTUAL_SCROLL_TOLERANCE;
169
+ const visibleRows = sortedRows.slice(
170
+ indexWithTolerance,
171
+ indexWithTolerance + lengthWithTolerance
172
+ );
173
+
174
+ useEffect(() => {
175
+ onVisibleRowsChange?.(indexWithTolerance, lengthWithTolerance);
176
+ }, [indexWithTolerance, lengthWithTolerance, onVisibleRowsChange]);
177
+
178
+ /** FOOTERS */
179
+ const [footers, setFooters] = useState<Record<string, string>>(
180
+ props.initialFooters ?? {}
181
+ );
182
+
183
+ const footerFunctions = useMemo(
184
+ () =>
185
+ Object.entries(footers).reduce((acc, [columnKey, footerKey]) => {
186
+ const column = columns[columnKey];
187
+ if (!column) {
188
+ return acc;
189
+ }
190
+ if (typeof column.footer === 'function') {
191
+ acc[columnKey] = column.footer;
192
+ } else if (
193
+ typeof column.footer === 'object' &&
194
+ typeof column.footer[footerKey] === 'function'
195
+ ) {
196
+ acc[columnKey] = column.footer[footerKey];
197
+ }
198
+ return acc;
199
+ }, {} as Record<string, DataGridFooterFunction<R>>),
200
+ [columns, footers]
201
+ );
202
+
203
+ /** COPYING */
204
+
205
+ const { copyTable } = useDataGridCopy({
206
+ rows,
207
+ visibleColumns,
208
+ footerFunctions,
209
+ loadCopyRows,
210
+ });
211
+
212
+ const contextValue = useMemo<DataGridContextProps<R>>(
213
+ () => ({
214
+ ...props,
215
+ columns,
216
+ visibleColumns,
217
+ sortedRows,
218
+ selectedRows,
219
+ selectedKeys,
220
+ setSelectedKeys,
221
+ sorts,
222
+ setSorts,
223
+ filters,
224
+ setFilters,
225
+ editingCell,
226
+ setEditingCell,
227
+ copyTable,
228
+ setColumnWidth,
229
+ settings,
230
+ setSettings,
231
+ saveSettings,
232
+ visibleRows,
233
+ scrollableRef,
234
+ onScroll,
235
+ index,
236
+ length,
237
+ rowKeyGetter,
238
+ gridTemplateColumns,
239
+ footers,
240
+ setFooters,
241
+ footerFunctions,
242
+ ...override,
243
+ }),
244
+ [
245
+ props,
246
+ columns,
247
+ visibleColumns,
248
+ sortedRows,
249
+ selectedRows,
250
+ selectedKeys,
251
+ sorts,
252
+ filters,
253
+ editingCell,
254
+ copyTable,
255
+ setColumnWidth,
256
+ settings,
257
+ setSettings,
258
+ saveSettings,
259
+ visibleRows,
260
+ onScroll,
261
+ index,
262
+ length,
263
+ rowKeyGetter,
264
+ gridTemplateColumns,
265
+ footers,
266
+ footerFunctions,
267
+ override,
268
+ ]
269
+ );
270
+
271
+ const context = useMemo(
272
+ () =>
273
+ createContext<DataGridContextProps<R>>({
274
+ rows: [],
275
+ columns: {},
276
+ visibleColumns: [],
277
+ rowKey: '' as keyof R,
278
+ rowKeyGetter: () => '',
279
+ sortedRows: [],
280
+ selectedRows: [],
281
+ selectedKeys: [],
282
+ setSelectedKeys: () => {},
283
+ setSorts: () => {},
284
+ setFilters: () => {},
285
+ editingCell: [-1, -1],
286
+ setEditingCell: () => {},
287
+ copyTable: async () => {},
288
+ setColumnWidth: () => {},
289
+ saveSettings: () => {},
290
+ settings: {},
291
+ setSettings: () => {},
292
+ visibleRows: [],
293
+ scrollableRef: { current: null },
294
+ onScroll: () => {},
295
+ index: 0,
296
+ length: 0,
297
+ gridTemplateColumns: '',
298
+ footers: {},
299
+ setFooters: () => {},
300
+ }),
301
+ []
302
+ );
303
+
304
+ return useMemo(() => [contextValue, context], [context, contextValue]);
305
+ };