@addev-be/ui 0.2.2 → 0.2.6

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