@addev-be/ui 0.1.18 → 0.1.19

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