@etsoo/react 1.5.78 → 1.5.81

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 (255) hide show
  1. package/README.md +7 -2
  2. package/__tests__/ReactUtils.ts +6 -0
  3. package/lib/{mu → components}/DnDList.d.ts +1 -14
  4. package/lib/{mu → components}/DnDList.js +1 -24
  5. package/lib/components/GridMethodRef.d.ts +11 -0
  6. package/lib/{mu → components}/GridMethodRef.js +0 -0
  7. package/lib/components/ScrollerGrid.d.ts +6 -6
  8. package/lib/components/ScrollerList.d.ts +3 -3
  9. package/lib/index.d.ts +2 -74
  10. package/lib/index.js +2 -75
  11. package/lib/notifier/Notifier.d.ts +2 -3
  12. package/lib/uses/useWindowScroll.d.ts +10 -0
  13. package/lib/uses/useWindowScroll.js +46 -0
  14. package/lib/uses/useWindowSize.js +11 -5
  15. package/package.json +9 -21
  16. package/src/{mu → components}/DnDList.tsx +11 -34
  17. package/src/components/GridMethodRef.ts +12 -0
  18. package/src/components/ScrollerGrid.tsx +6 -6
  19. package/src/components/ScrollerList.tsx +5 -3
  20. package/src/index.ts +2 -78
  21. package/src/notifier/Notifier.ts +2 -3
  22. package/src/uses/useWindowScroll.ts +60 -0
  23. package/src/uses/useWindowSize.ts +14 -5
  24. package/__tests__/mu/MUGlobalTests.tsx +0 -58
  25. package/__tests__/mu/NotifierMUTests.tsx +0 -213
  26. package/lib/app/CommonApp.d.ts +0 -39
  27. package/lib/app/CommonApp.js +0 -149
  28. package/lib/app/IServiceAppSettings.d.ts +0 -11
  29. package/lib/app/IServiceAppSettings.js +0 -1
  30. package/lib/app/IServicePage.d.ts +0 -6
  31. package/lib/app/IServicePage.js +0 -1
  32. package/lib/app/IServiceUser.d.ts +0 -14
  33. package/lib/app/IServiceUser.js +0 -1
  34. package/lib/app/ISmartERPUser.d.ts +0 -14
  35. package/lib/app/ISmartERPUser.js +0 -1
  36. package/lib/app/Labels.d.ts +0 -65
  37. package/lib/app/Labels.js +0 -62
  38. package/lib/app/ReactApp.d.ts +0 -194
  39. package/lib/app/ReactApp.js +0 -298
  40. package/lib/app/ServiceApp.d.ts +0 -78
  41. package/lib/app/ServiceApp.js +0 -244
  42. package/lib/components/ShowDataComparison.d.ts +0 -20
  43. package/lib/components/ShowDataComparison.js +0 -60
  44. package/lib/mu/AuditDisplay.d.ts +0 -33
  45. package/lib/mu/AuditDisplay.js +0 -52
  46. package/lib/mu/AutocompleteExtendedProps.d.ts +0 -66
  47. package/lib/mu/AutocompleteExtendedProps.js +0 -1
  48. package/lib/mu/BackButton.d.ts +0 -13
  49. package/lib/mu/BackButton.js +0 -33
  50. package/lib/mu/BridgeCloseButton.d.ts +0 -23
  51. package/lib/mu/BridgeCloseButton.js +0 -32
  52. package/lib/mu/ButtonLink.d.ts +0 -17
  53. package/lib/mu/ButtonLink.js +0 -19
  54. package/lib/mu/ComboBox.d.ts +0 -40
  55. package/lib/mu/ComboBox.js +0 -108
  56. package/lib/mu/CountdownButton.d.ts +0 -23
  57. package/lib/mu/CountdownButton.js +0 -81
  58. package/lib/mu/CustomFabProps.d.ts +0 -27
  59. package/lib/mu/CustomFabProps.js +0 -1
  60. package/lib/mu/DataGridEx.d.ts +0 -96
  61. package/lib/mu/DataGridEx.js +0 -331
  62. package/lib/mu/DataGridRenderers.d.ts +0 -22
  63. package/lib/mu/DataGridRenderers.js +0 -99
  64. package/lib/mu/DialogButton.d.ts +0 -54
  65. package/lib/mu/DialogButton.js +0 -45
  66. package/lib/mu/DraggablePaperComponent.d.ts +0 -8
  67. package/lib/mu/DraggablePaperComponent.js +0 -12
  68. package/lib/mu/EmailInput.d.ts +0 -11
  69. package/lib/mu/EmailInput.js +0 -15
  70. package/lib/mu/FabBox.d.ts +0 -21
  71. package/lib/mu/FabBox.js +0 -31
  72. package/lib/mu/FlexBox.d.ts +0 -14
  73. package/lib/mu/FlexBox.js +0 -18
  74. package/lib/mu/GridDataFormat.d.ts +0 -10
  75. package/lib/mu/GridDataFormat.js +0 -43
  76. package/lib/mu/GridMethodRef.d.ts +0 -11
  77. package/lib/mu/IconButtonLink.d.ts +0 -17
  78. package/lib/mu/IconButtonLink.js +0 -16
  79. package/lib/mu/InputField.d.ts +0 -21
  80. package/lib/mu/InputField.js +0 -39
  81. package/lib/mu/ItemList.d.ts +0 -55
  82. package/lib/mu/ItemList.js +0 -77
  83. package/lib/mu/ListItemRightIcon.d.ts +0 -4
  84. package/lib/mu/ListItemRightIcon.js +0 -8
  85. package/lib/mu/ListMoreDisplay.d.ts +0 -35
  86. package/lib/mu/ListMoreDisplay.js +0 -99
  87. package/lib/mu/LoadingButton.d.ts +0 -16
  88. package/lib/mu/LoadingButton.js +0 -41
  89. package/lib/mu/MUGlobal.d.ts +0 -102
  90. package/lib/mu/MUGlobal.js +0 -184
  91. package/lib/mu/MaskInput.d.ts +0 -34
  92. package/lib/mu/MaskInput.js +0 -43
  93. package/lib/mu/MobileListItemRenderer.d.ts +0 -17
  94. package/lib/mu/MobileListItemRenderer.js +0 -35
  95. package/lib/mu/MoreFab.d.ts +0 -45
  96. package/lib/mu/MoreFab.js +0 -95
  97. package/lib/mu/NotifierMU.d.ts +0 -47
  98. package/lib/mu/NotifierMU.js +0 -387
  99. package/lib/mu/NotifierPromptProps.d.ts +0 -22
  100. package/lib/mu/NotifierPromptProps.js +0 -1
  101. package/lib/mu/OptionGroup.d.ts +0 -62
  102. package/lib/mu/OptionGroup.js +0 -81
  103. package/lib/mu/PList.d.ts +0 -15
  104. package/lib/mu/PList.js +0 -12
  105. package/lib/mu/ProgressCount.d.ts +0 -44
  106. package/lib/mu/ProgressCount.js +0 -79
  107. package/lib/mu/PullToRefreshUI.d.ts +0 -9
  108. package/lib/mu/PullToRefreshUI.js +0 -18
  109. package/lib/mu/RLink.d.ts +0 -14
  110. package/lib/mu/RLink.js +0 -37
  111. package/lib/mu/ResponsibleContainer.d.ts +0 -89
  112. package/lib/mu/ResponsibleContainer.js +0 -159
  113. package/lib/mu/ScrollTopFab.d.ts +0 -7
  114. package/lib/mu/ScrollTopFab.js +0 -25
  115. package/lib/mu/ScrollerListEx.d.ts +0 -81
  116. package/lib/mu/ScrollerListEx.js +0 -167
  117. package/lib/mu/SearchBar.d.ts +0 -29
  118. package/lib/mu/SearchBar.js +0 -262
  119. package/lib/mu/SearchField.d.ts +0 -21
  120. package/lib/mu/SearchField.js +0 -39
  121. package/lib/mu/SearchOptionGroup.d.ts +0 -9
  122. package/lib/mu/SearchOptionGroup.js +0 -14
  123. package/lib/mu/SelectBool.d.ts +0 -14
  124. package/lib/mu/SelectBool.js +0 -22
  125. package/lib/mu/SelectEx.d.ts +0 -54
  126. package/lib/mu/SelectEx.js +0 -156
  127. package/lib/mu/Switch.d.ts +0 -29
  128. package/lib/mu/Switch.js +0 -34
  129. package/lib/mu/SwitchAnt.d.ts +0 -25
  130. package/lib/mu/SwitchAnt.js +0 -40
  131. package/lib/mu/TabBox.d.ts +0 -54
  132. package/lib/mu/TabBox.js +0 -31
  133. package/lib/mu/TableEx.d.ts +0 -68
  134. package/lib/mu/TableEx.js +0 -271
  135. package/lib/mu/TextFieldEx.d.ts +0 -101
  136. package/lib/mu/TextFieldEx.js +0 -127
  137. package/lib/mu/Tiplist.d.ts +0 -18
  138. package/lib/mu/Tiplist.js +0 -158
  139. package/lib/mu/TooltipClick.d.ts +0 -15
  140. package/lib/mu/TooltipClick.js +0 -40
  141. package/lib/mu/UserAvatar.d.ts +0 -24
  142. package/lib/mu/UserAvatar.js +0 -25
  143. package/lib/mu/UserAvatarEditor.d.ts +0 -53
  144. package/lib/mu/UserAvatarEditor.js +0 -129
  145. package/lib/mu/pages/CommonPage.d.ts +0 -11
  146. package/lib/mu/pages/CommonPage.js +0 -60
  147. package/lib/mu/pages/CommonPageProps.d.ts +0 -60
  148. package/lib/mu/pages/CommonPageProps.js +0 -1
  149. package/lib/mu/pages/DataGridPage.d.ts +0 -9
  150. package/lib/mu/pages/DataGridPage.js +0 -81
  151. package/lib/mu/pages/DataGridPageProps.d.ts +0 -17
  152. package/lib/mu/pages/DataGridPageProps.js +0 -1
  153. package/lib/mu/pages/EditPage.d.ts +0 -33
  154. package/lib/mu/pages/EditPage.js +0 -29
  155. package/lib/mu/pages/FixedListPage.d.ts +0 -15
  156. package/lib/mu/pages/FixedListPage.js +0 -72
  157. package/lib/mu/pages/ListPage.d.ts +0 -9
  158. package/lib/mu/pages/ListPage.js +0 -51
  159. package/lib/mu/pages/ListPageProps.d.ts +0 -7
  160. package/lib/mu/pages/ListPageProps.js +0 -1
  161. package/lib/mu/pages/ResponsivePage.d.ts +0 -9
  162. package/lib/mu/pages/ResponsivePage.js +0 -45
  163. package/lib/mu/pages/ResponsivePageProps.d.ts +0 -39
  164. package/lib/mu/pages/ResponsivePageProps.js +0 -1
  165. package/lib/mu/pages/SearchPageProps.d.ts +0 -30
  166. package/lib/mu/pages/SearchPageProps.js +0 -1
  167. package/lib/mu/pages/TablePage.d.ts +0 -9
  168. package/lib/mu/pages/TablePage.js +0 -71
  169. package/lib/mu/pages/TablePageProps.d.ts +0 -7
  170. package/lib/mu/pages/TablePageProps.js +0 -1
  171. package/lib/mu/pages/ViewPage.d.ts +0 -66
  172. package/lib/mu/pages/ViewPage.js +0 -105
  173. package/lib/mu/texts/DateText.d.ts +0 -34
  174. package/lib/mu/texts/DateText.js +0 -25
  175. package/lib/mu/texts/MoneyText.d.ts +0 -21
  176. package/lib/mu/texts/MoneyText.js +0 -14
  177. package/lib/mu/texts/NumberText.d.ts +0 -25
  178. package/lib/mu/texts/NumberText.js +0 -14
  179. package/src/app/CommonApp.ts +0 -225
  180. package/src/app/IServiceAppSettings.ts +0 -13
  181. package/src/app/IServicePage.ts +0 -6
  182. package/src/app/IServiceUser.ts +0 -17
  183. package/src/app/ISmartERPUser.ts +0 -16
  184. package/src/app/Labels.ts +0 -77
  185. package/src/app/ReactApp.ts +0 -500
  186. package/src/app/ServiceApp.ts +0 -353
  187. package/src/components/ShowDataComparison.tsx +0 -108
  188. package/src/mu/AuditDisplay.tsx +0 -117
  189. package/src/mu/AutocompleteExtendedProps.ts +0 -84
  190. package/src/mu/BackButton.tsx +0 -55
  191. package/src/mu/BridgeCloseButton.tsx +0 -69
  192. package/src/mu/ButtonLink.tsx +0 -32
  193. package/src/mu/ComboBox.tsx +0 -244
  194. package/src/mu/CountdownButton.tsx +0 -119
  195. package/src/mu/CustomFabProps.ts +0 -32
  196. package/src/mu/DataGridEx.tsx +0 -712
  197. package/src/mu/DataGridRenderers.tsx +0 -140
  198. package/src/mu/DialogButton.tsx +0 -163
  199. package/src/mu/DraggablePaperComponent.tsx +0 -19
  200. package/src/mu/EmailInput.tsx +0 -24
  201. package/src/mu/FabBox.tsx +0 -51
  202. package/src/mu/FlexBox.tsx +0 -20
  203. package/src/mu/GridDataFormat.tsx +0 -77
  204. package/src/mu/GridMethodRef.ts +0 -12
  205. package/src/mu/IconButtonLink.tsx +0 -29
  206. package/src/mu/InputField.tsx +0 -82
  207. package/src/mu/ItemList.tsx +0 -201
  208. package/src/mu/ListItemRightIcon.tsx +0 -9
  209. package/src/mu/ListMoreDisplay.tsx +0 -205
  210. package/src/mu/LoadingButton.tsx +0 -75
  211. package/src/mu/MUGlobal.ts +0 -220
  212. package/src/mu/MaskInput.tsx +0 -107
  213. package/src/mu/MobileListItemRenderer.tsx +0 -79
  214. package/src/mu/MoreFab.tsx +0 -211
  215. package/src/mu/NotifierMU.tsx +0 -654
  216. package/src/mu/NotifierPromptProps.ts +0 -26
  217. package/src/mu/OptionGroup.tsx +0 -218
  218. package/src/mu/PList.tsx +0 -27
  219. package/src/mu/ProgressCount.tsx +0 -166
  220. package/src/mu/PullToRefreshUI.tsx +0 -21
  221. package/src/mu/RLink.tsx +0 -64
  222. package/src/mu/ResponsibleContainer.tsx +0 -394
  223. package/src/mu/ScrollTopFab.tsx +0 -34
  224. package/src/mu/ScrollerListEx.tsx +0 -387
  225. package/src/mu/SearchBar.tsx +0 -398
  226. package/src/mu/SearchField.tsx +0 -82
  227. package/src/mu/SearchOptionGroup.tsx +0 -25
  228. package/src/mu/SelectBool.tsx +0 -40
  229. package/src/mu/SelectEx.tsx +0 -285
  230. package/src/mu/Switch.tsx +0 -94
  231. package/src/mu/SwitchAnt.tsx +0 -95
  232. package/src/mu/TabBox.tsx +0 -118
  233. package/src/mu/TableEx.tsx +0 -561
  234. package/src/mu/TextFieldEx.tsx +0 -250
  235. package/src/mu/Tiplist.tsx +0 -304
  236. package/src/mu/TooltipClick.tsx +0 -84
  237. package/src/mu/UserAvatar.tsx +0 -64
  238. package/src/mu/UserAvatarEditor.tsx +0 -287
  239. package/src/mu/pages/CommonPage.tsx +0 -128
  240. package/src/mu/pages/CommonPageProps.ts +0 -71
  241. package/src/mu/pages/DataGridPage.tsx +0 -137
  242. package/src/mu/pages/DataGridPageProps.ts +0 -24
  243. package/src/mu/pages/EditPage.tsx +0 -114
  244. package/src/mu/pages/FixedListPage.tsx +0 -134
  245. package/src/mu/pages/ListPage.tsx +0 -86
  246. package/src/mu/pages/ListPageProps.ts +0 -11
  247. package/src/mu/pages/ResponsivePage.tsx +0 -68
  248. package/src/mu/pages/ResponsivePageProps.ts +0 -57
  249. package/src/mu/pages/SearchPageProps.ts +0 -39
  250. package/src/mu/pages/TablePage.tsx +0 -119
  251. package/src/mu/pages/TablePageProps.ts +0 -11
  252. package/src/mu/pages/ViewPage.tsx +0 -285
  253. package/src/mu/texts/DateText.tsx +0 -74
  254. package/src/mu/texts/MoneyText.tsx +0 -49
  255. package/src/mu/texts/NumberText.tsx +0 -40
@@ -1,561 +0,0 @@
1
- import { DataTypes } from '@etsoo/shared';
2
- import {
3
- Checkbox,
4
- Paper,
5
- Table,
6
- TableBody,
7
- TableCell,
8
- TableCellProps,
9
- TableContainer,
10
- TableHead,
11
- TablePagination,
12
- TableProps,
13
- TableRow,
14
- TableSortLabel,
15
- useTheme
16
- } from '@mui/material';
17
- import React from 'react';
18
- import {
19
- GridAlignGet,
20
- GridCellFormatterProps,
21
- GridColumn
22
- } from '../components/GridColumn';
23
- import {
24
- GridLoadDataProps,
25
- GridLoader,
26
- GridLoaderStates,
27
- GridSizeGet
28
- } from '../components/GridLoader';
29
- import { DataGridRenderers } from './DataGridRenderers';
30
- import { GridMethodRef } from './GridMethodRef';
31
-
32
- /**
33
- * Extended table min width for width-unset column
34
- */
35
- export const TableExMinWidth: number = 180;
36
-
37
- /**
38
- * Extended table methods ref
39
- */
40
- export interface TableExMethodRef extends GridMethodRef {
41
- /**
42
- * Refresh data
43
- */
44
- refresh(): void;
45
- }
46
-
47
- /**
48
- * Extended table props
49
- */
50
- export type TableExProps<
51
- T extends object,
52
- D extends DataTypes.Keys<T> = DataTypes.Keys<T>
53
- > = TableProps &
54
- GridLoader<T> & {
55
- /**
56
- * Alternating colors for odd/even rows
57
- */
58
- alternatingColors?: [string?, string?];
59
-
60
- /**
61
- * Columns
62
- */
63
- columns: GridColumn<T>[];
64
-
65
- /**
66
- * Header cells background color and font color
67
- */
68
- headerColors?: [string?, string?];
69
-
70
- /**
71
- * Max height
72
- */
73
- maxHeight?: number;
74
-
75
- /**
76
- * Methods
77
- */
78
- mRef?: React.Ref<TableExMethodRef>;
79
-
80
- /**
81
- * On items select change
82
- */
83
- onSelectChange?: (selectedItems: T[]) => void;
84
-
85
- /**
86
- * Row height
87
- */
88
- rowHeight?: number;
89
-
90
- /**
91
- * Header and bottom height
92
- */
93
- otherHeight?: number;
94
- } & (T extends { id: DataTypes.IdType }
95
- ? {
96
- idField?: D;
97
- }
98
- : {
99
- idField: D;
100
- });
101
-
102
- /**
103
- * Extended Table
104
- * @param props Props
105
- * @returns Component
106
- */
107
- export function TableEx<
108
- T extends object,
109
- D extends DataTypes.Keys<T> = DataTypes.Keys<T>
110
- >(props: TableExProps<T, D>) {
111
- // Theme
112
- const theme = useTheme();
113
-
114
- // Destruct
115
- const {
116
- alternatingColors = [theme.palette.action.hover, undefined],
117
- autoLoad = true,
118
- columns,
119
- defaultOrderBy,
120
- headerColors = [undefined, undefined],
121
- idField = 'id' as D,
122
- loadBatchSize,
123
- loadData,
124
- maxHeight,
125
- mRef,
126
- onSelectChange,
127
- rowHeight = 53,
128
- otherHeight = 110,
129
- threshold,
130
- ...rest
131
- } = props;
132
-
133
- const selectable: boolean = onSelectChange != null;
134
-
135
- // Rows per page
136
- let rowsPerPageLocal: number;
137
- if (maxHeight != null) {
138
- if (loadBatchSize != null)
139
- rowsPerPageLocal = GridSizeGet(loadBatchSize, maxHeight);
140
- else
141
- rowsPerPageLocal = Math.floor(
142
- (maxHeight - otherHeight) / rowHeight
143
- );
144
- } else if (typeof loadBatchSize === 'number') {
145
- rowsPerPageLocal = loadBatchSize;
146
- } else {
147
- rowsPerPageLocal = 10;
148
- }
149
-
150
- // Rows
151
- const [rows, updateRows] = React.useState<T[]>([]);
152
- const setRows = (rows: T[]) => {
153
- state.loadedItems = rows.length;
154
- updateRows(rows);
155
- };
156
-
157
- // States
158
- const stateRefs = React.useRef<GridLoaderStates<T>>({
159
- autoLoad,
160
- currentPage: 0,
161
- loadedItems: 0,
162
- hasNextPage: true,
163
- isNextPageLoading: false,
164
- orderBy: defaultOrderBy,
165
- orderByAsc: defaultOrderBy
166
- ? columns.find((column) => column.field === defaultOrderBy)?.sortAsc
167
- : undefined,
168
- batchSize: rowsPerPageLocal,
169
- selectedItems: []
170
- });
171
- const state = stateRefs.current;
172
-
173
- // Reset the state and load again
174
- const reset = (add?: Partial<GridLoaderStates<T>>) => {
175
- const resetState: Partial<GridLoaderStates<T>> = {
176
- autoLoad: true,
177
- currentPage: 0,
178
- loadedItems: 0,
179
- hasNextPage: true,
180
- isNextPageLoading: false,
181
- lastLoadedItems: undefined,
182
- ...add
183
- };
184
- Object.assign(state, resetState);
185
- };
186
-
187
- React.useImperativeHandle(
188
- mRef,
189
- () => ({
190
- /**
191
- * Refresh data
192
- */
193
- refresh(): void {
194
- loadDataLocal();
195
- },
196
-
197
- /**
198
- * Reset
199
- */
200
- reset
201
- }),
202
- []
203
- );
204
-
205
- // Load data
206
- const loadDataLocal = () => {
207
- // Prevent multiple loadings
208
- if (!state.hasNextPage || state.isNextPageLoading) return;
209
-
210
- // Update state
211
- state.isNextPageLoading = true;
212
-
213
- // Parameters
214
- const { currentPage, batchSize, orderBy, orderByAsc, data, isMounted } =
215
- state;
216
-
217
- const loadProps: GridLoadDataProps = {
218
- currentPage,
219
- batchSize,
220
- orderBy,
221
- orderByAsc,
222
- data
223
- };
224
-
225
- loadData(loadProps).then((result) => {
226
- state.isMounted = true;
227
- if (result == null || isMounted === false) {
228
- return;
229
- }
230
-
231
- const newItems = result.length;
232
- state.lastLoadedItems = newItems;
233
- state.hasNextPage = newItems >= batchSize;
234
- state.isNextPageLoading = false;
235
-
236
- // Update rows
237
- setRows(result);
238
- });
239
- };
240
-
241
- const handleChangePage = (_event: unknown, newPage: number) => {
242
- state.hasNextPage = true;
243
- state.currentPage = newPage;
244
- loadDataLocal();
245
- };
246
-
247
- const handleChangeRowsPerPage = (
248
- event: React.ChangeEvent<HTMLInputElement>
249
- ) => {
250
- const batchSize = parseInt(event.target.value);
251
- reset({ batchSize });
252
- };
253
-
254
- const handleSelect = (item: T, checked: Boolean) => {
255
- const selectedItems = state.selectedItems;
256
-
257
- const index = selectedItems.findIndex(
258
- (selectedItem) => selectedItem[idField] === item[idField]
259
- );
260
- if (checked) {
261
- if (index === -1) selectedItems.push(item);
262
- } else {
263
- if (index !== -1) selectedItems.splice(index, 1);
264
- }
265
-
266
- if (onSelectChange != null) {
267
- onSelectChange(selectedItems);
268
- }
269
- };
270
-
271
- const handleSelectAll = (checked: boolean) => {
272
- const selectedItems = state.selectedItems;
273
-
274
- rows.forEach((row) => {
275
- const index = selectedItems.findIndex(
276
- (selectedItem) => selectedItem[idField] === row[idField]
277
- );
278
-
279
- if (checked) {
280
- if (index === -1) selectedItems.push(row);
281
- } else if (index !== -1) {
282
- selectedItems.splice(index, 1);
283
- }
284
- });
285
-
286
- if (onSelectChange != null) {
287
- onSelectChange(selectedItems);
288
- }
289
- };
290
-
291
- // New sort
292
- const handleSort = (field: string, asc?: boolean) => {
293
- reset({ orderBy: field, orderByAsc: asc });
294
- };
295
-
296
- // Destruct states
297
- const {
298
- autoLoad: stateAutoLoad,
299
- currentPage,
300
- hasNextPage,
301
- lastLoadedItems,
302
- orderBy,
303
- batchSize,
304
- selectedItems
305
- } = state;
306
-
307
- // Current page selected items
308
- const pageSelectedItems = selectable
309
- ? rows.reduce((previousValue, currentItem) => {
310
- if (
311
- selectedItems.some(
312
- (item) => item[idField] === currentItem[idField]
313
- )
314
- )
315
- return previousValue + 1;
316
-
317
- return previousValue;
318
- }, 0)
319
- : 0;
320
-
321
- // Total rows
322
- const totalRows = hasNextPage
323
- ? -1
324
- : currentPage * batchSize + (lastLoadedItems ?? 0);
325
-
326
- // Auto load data when current page is 0
327
- if (currentPage === 0 && stateAutoLoad && lastLoadedItems == null)
328
- loadDataLocal();
329
-
330
- React.useEffect(() => {
331
- return () => {
332
- state.isMounted = false;
333
- };
334
- }, []);
335
-
336
- // Layout
337
- return (
338
- <Paper>
339
- <TableContainer sx={{ maxHeight }}>
340
- <Table {...rest}>
341
- <TableHead>
342
- <TableRow
343
- sx={{
344
- '& th': {
345
- backgroundColor: headerColors[0],
346
- color: headerColors[1]
347
- }
348
- }}
349
- >
350
- {selectable && (
351
- <TableCell padding="checkbox">
352
- <Checkbox
353
- color="primary"
354
- indeterminate={
355
- pageSelectedItems > 0 &&
356
- pageSelectedItems < rows.length
357
- }
358
- checked={pageSelectedItems > 0}
359
- onChange={(_event, checked) =>
360
- handleSelectAll(checked)
361
- }
362
- />
363
- </TableCell>
364
- )}
365
- {columns.map((column, index) => {
366
- // Destruct
367
- const {
368
- align,
369
- field,
370
- header,
371
- minWidth,
372
- sortable,
373
- sortAsc = true,
374
- type,
375
- width
376
- } = column;
377
-
378
- // Header text
379
- const headerText = header ?? field;
380
-
381
- // Sortable
382
- let sortLabel: React.ReactNode;
383
- if (sortable && field != null) {
384
- const active = orderBy === field;
385
-
386
- sortLabel = (
387
- <TableSortLabel
388
- active={active}
389
- direction={sortAsc ? 'asc' : 'desc'}
390
- onClick={(_event) => {
391
- if (active)
392
- column.sortAsc = !sortAsc;
393
-
394
- handleSort(
395
- field,
396
- column.sortAsc
397
- );
398
- }}
399
- >
400
- {headerText}
401
- </TableSortLabel>
402
- );
403
- } else {
404
- sortLabel = headerText;
405
- }
406
-
407
- return (
408
- <TableCell
409
- align={GridAlignGet(align, type)}
410
- key={field ?? index.toString()}
411
- width={width}
412
- sx={{
413
- minWidth:
414
- minWidth == null
415
- ? width == null
416
- ? TableExMinWidth
417
- : undefined
418
- : minWidth
419
- }}
420
- >
421
- {sortLabel}
422
- </TableCell>
423
- );
424
- })}
425
- </TableRow>
426
- </TableHead>
427
- <TableBody
428
- sx={{
429
- '& tr:nth-of-type(odd):not(.Mui-selected)': {
430
- backgroundColor: alternatingColors[0]
431
- },
432
- '& tr:nth-of-type(even):not(.Mui-selected)': {
433
- backgroundColor: alternatingColors[1]
434
- }
435
- }}
436
- >
437
- {[...Array(batchSize)].map((_item, rowIndex) => {
438
- // Row
439
- const row =
440
- rowIndex < rows.length
441
- ? rows[rowIndex]
442
- : undefined;
443
-
444
- // Row id field value
445
- const rowId =
446
- DataTypes.getValue(row, idField) ?? rowIndex;
447
-
448
- // Selected or not
449
- const isItemSelected = selectable
450
- ? selectedItems.some(
451
- (item) => item[idField] === rowId
452
- )
453
- : false;
454
-
455
- return (
456
- <TableRow
457
- key={rowId as unknown as React.Key}
458
- selected={isItemSelected}
459
- >
460
- {selectable && (
461
- <TableCell padding="checkbox">
462
- {row && (
463
- <Checkbox
464
- color="primary"
465
- checked={isItemSelected}
466
- onChange={(
467
- _event,
468
- checked
469
- ) =>
470
- handleSelect(
471
- row,
472
- checked
473
- )
474
- }
475
- />
476
- )}
477
- </TableCell>
478
- )}
479
- {columns.map(
480
- (
481
- {
482
- align,
483
- cellRenderer = DataGridRenderers.defaultCellRenderer,
484
- field,
485
- type,
486
- valueFormatter
487
- },
488
- columnIndex
489
- ) => {
490
- const formatProps: GridCellFormatterProps<T> =
491
- {
492
- data: row,
493
- field,
494
- rowIndex,
495
- columnIndex
496
- };
497
-
498
- const cellProps: TableCellProps = {
499
- align: GridAlignGet(
500
- align,
501
- type
502
- ),
503
- valign: 'middle'
504
- };
505
-
506
- const child = row ? (
507
- cellRenderer({
508
- data: row,
509
- field,
510
- formattedValue:
511
- valueFormatter
512
- ? valueFormatter(
513
- formatProps
514
- )
515
- : undefined,
516
- selected: isItemSelected,
517
- type,
518
- rowIndex,
519
- columnIndex,
520
- cellProps
521
- })
522
- ) : (
523
- <React.Fragment>
524
- &nbsp;
525
- </React.Fragment>
526
- );
527
-
528
- return (
529
- <TableCell
530
- key={`${rowId}${columnIndex}`}
531
- {...cellProps}
532
- >
533
- {child}
534
- </TableCell>
535
- );
536
- }
537
- )}
538
- </TableRow>
539
- );
540
- })}
541
- </TableBody>
542
- </Table>
543
- </TableContainer>
544
- <TablePagination
545
- component="div"
546
- showFirstButton
547
- count={totalRows}
548
- rowsPerPage={batchSize}
549
- page={currentPage}
550
- onPageChange={handleChangePage}
551
- onRowsPerPageChange={handleChangeRowsPerPage}
552
- rowsPerPageOptions={[
553
- batchSize,
554
- 2 * batchSize,
555
- 5 * batchSize,
556
- 10 * batchSize
557
- ]}
558
- />
559
- </Paper>
560
- );
561
- }