@addev-be/ui 0.2.16 → 0.2.18

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 (88) hide show
  1. package/eslint.config.js +28 -0
  2. package/package.json +13 -20
  3. package/src/Icons.tsx +108 -0
  4. package/src/components/data/AdvancedRequestDataGrid/helpers/advancedRequests.ts +93 -0
  5. package/src/components/data/AdvancedRequestDataGrid/helpers/columns.tsx +262 -0
  6. package/src/components/data/AdvancedRequestDataGrid/helpers/index.ts +2 -0
  7. package/src/components/data/AdvancedRequestDataGrid/index.tsx +267 -0
  8. package/src/components/data/AdvancedRequestDataGrid/types.ts +47 -0
  9. package/src/components/data/DataGrid/DataGridCell.tsx +73 -0
  10. package/src/components/data/DataGrid/DataGridColumnsModal/helpers.ts +14 -0
  11. package/src/components/data/DataGrid/DataGridColumnsModal/hooks.tsx +59 -0
  12. package/src/components/data/DataGrid/DataGridColumnsModal/index.tsx +181 -0
  13. package/src/components/data/DataGrid/DataGridColumnsModal/styles.ts +104 -0
  14. package/src/components/data/DataGrid/DataGridEditableCell.tsx +43 -0
  15. package/src/components/data/DataGrid/DataGridFilterMenu/FilterValuesScroller.tsx +120 -0
  16. package/src/components/data/DataGrid/DataGridFilterMenu/hooks.tsx +75 -0
  17. package/src/components/data/DataGrid/DataGridFilterMenu/index.tsx +360 -0
  18. package/src/components/data/DataGrid/DataGridFilterMenu/styles.ts +96 -0
  19. package/src/components/data/DataGrid/DataGridFooter.tsx +42 -0
  20. package/src/components/data/DataGrid/DataGridHeader.tsx +126 -0
  21. package/src/components/data/DataGrid/DataGridHeaderCell.tsx +132 -0
  22. package/src/components/data/DataGrid/FilterModalContent/index.tsx +136 -0
  23. package/src/components/data/DataGrid/FilterModalContent/styles.ts +22 -0
  24. package/src/components/data/DataGrid/VirtualScroller.tsx +46 -0
  25. package/src/components/data/DataGrid/helpers/columns.tsx +295 -0
  26. package/src/components/data/DataGrid/helpers/filters.ts +287 -0
  27. package/src/components/data/DataGrid/helpers/index.ts +2 -0
  28. package/src/components/data/DataGrid/hooks/index.ts +30 -0
  29. package/src/components/data/DataGrid/hooks/useDataGrid.tsx +306 -0
  30. package/src/components/data/DataGrid/hooks/useDataGridCopy.ts +175 -0
  31. package/src/components/data/DataGrid/hooks/useDataGridSettings.ts +48 -0
  32. package/src/components/data/DataGrid/index.tsx +140 -0
  33. package/src/components/data/DataGrid/styles.ts +323 -0
  34. package/src/components/data/DataGrid/types.ts +267 -0
  35. package/src/components/data/SqlRequestDataGrid/helpers/columns.tsx +277 -0
  36. package/src/components/data/SqlRequestDataGrid/helpers/index.ts +2 -0
  37. package/src/components/data/SqlRequestDataGrid/helpers/sqlRequests.ts +16 -0
  38. package/src/components/data/SqlRequestDataGrid/index.tsx +347 -0
  39. package/src/components/data/SqlRequestDataGrid/types.ts +47 -0
  40. package/src/components/data/index.ts +8 -0
  41. package/src/components/forms/Button.tsx +99 -0
  42. package/src/components/forms/IconButton.tsx +56 -0
  43. package/src/components/forms/IndeterminateCheckbox.tsx +46 -0
  44. package/src/components/forms/Select.tsx +40 -0
  45. package/src/components/forms/index.ts +5 -0
  46. package/src/components/forms/styles.ts +20 -0
  47. package/src/components/index.ts +3 -0
  48. package/src/components/layout/Dropdown/index.tsx +79 -0
  49. package/src/components/layout/Dropdown/styles.ts +44 -0
  50. package/src/components/layout/Loading/index.tsx +29 -0
  51. package/src/components/layout/Loading/styles.ts +29 -0
  52. package/src/components/layout/Modal/index.tsx +51 -0
  53. package/src/components/layout/Modal/styles.ts +110 -0
  54. package/src/components/layout/index.ts +3 -0
  55. package/src/components/ui/ContextMenu/index.tsx +79 -0
  56. package/src/components/ui/ContextMenu/styles.ts +119 -0
  57. package/src/config/index.ts +14 -0
  58. package/src/helpers/dates.ts +9 -0
  59. package/src/helpers/getScrollbarSize.ts +14 -0
  60. package/src/helpers/numbers.ts +26 -0
  61. package/src/hooks/index.ts +2 -0
  62. package/src/hooks/useElementSize.ts +24 -0
  63. package/src/hooks/useWindowSize.ts +20 -0
  64. package/src/index.ts +7 -0
  65. package/src/providers/PortalsProvider/index.tsx +54 -0
  66. package/src/providers/PortalsProvider/styles.ts +27 -0
  67. package/src/providers/SettingsProvider/index.tsx +70 -0
  68. package/src/providers/ThemeProvider/ThemeProvider.ts +55 -0
  69. package/src/providers/ThemeProvider/defaultTheme.ts +444 -0
  70. package/src/providers/ThemeProvider/index.ts +3 -0
  71. package/src/providers/ThemeProvider/types.ts +123 -0
  72. package/src/providers/UiProviders/index.tsx +65 -0
  73. package/src/providers/UiProviders/styles.ts +10 -0
  74. package/src/providers/hooks.ts +8 -0
  75. package/src/providers/index.ts +5 -0
  76. package/src/services/HttpService.ts +80 -0
  77. package/src/services/WebSocketService.ts +147 -0
  78. package/src/services/advancedRequests.ts +101 -0
  79. package/src/services/base.ts +31 -0
  80. package/src/services/globalSearch.ts +27 -0
  81. package/src/services/hooks.ts +23 -0
  82. package/src/services/index.ts +2 -0
  83. package/src/services/sqlRequests.ts +110 -0
  84. package/src/styles/animations.scss +30 -0
  85. package/src/styles/index.scss +42 -0
  86. package/src/typings.d.ts +6 -0
  87. package/tsconfig.json +18 -0
  88. package/tsconfig.tsbuildinfo +1 -0
@@ -0,0 +1,295 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+
3
+ import { DataGridColumn, DataGridColumns, DataGridSettings } from '../types';
4
+ import { dateFilter, numberFilter, textFilter } from './filters';
5
+ import {
6
+ formatMoney,
7
+ formatNumber,
8
+ formatNumberInvariant,
9
+ formatPercentage,
10
+ } from '../../../../helpers/numbers';
11
+
12
+ import moment from 'moment';
13
+ import { repeat } from 'lodash';
14
+
15
+ export const isColumnVisible = <R,>(
16
+ obj: DataGridColumn<R> | DataGridSettings
17
+ ): boolean => obj?.order !== -1;
18
+
19
+ export const buildExcelFormat = (decimals = 2, suffix = '') =>
20
+ `#0${decimals > 0 ? `.${repeat('0', decimals)}` : ''}${suffix}`;
21
+
22
+ export const textColumn = <R extends Record<string, any>>(
23
+ key: string,
24
+ title: string,
25
+ options?: Partial<DataGridColumn<R>>
26
+ ): DataGridColumns<R> => ({
27
+ [key]: {
28
+ name: title,
29
+ render: (row) => row[key] ?? '',
30
+ getter: (row) => row[key] ?? '',
31
+ sortGetter: (row) => row[key] ?? '',
32
+ footer: (_, filteredRows) => `${filteredRows.length} éléments`,
33
+ filter: textFilter(key),
34
+ ...options,
35
+ },
36
+ });
37
+
38
+ export const mailColumn = <R extends Record<string, any>>(
39
+ key: string,
40
+ title: string,
41
+ options?: Partial<DataGridColumn<R>>
42
+ ): DataGridColumns<R> => ({
43
+ [key]: {
44
+ name: title,
45
+ render: (row) => (
46
+ <a
47
+ // className="text-blue-500 hover:text-blue-700"
48
+ href={`mailto:${row[key]}`}
49
+ >
50
+ {row[key] ?? ''}
51
+ </a>
52
+ ),
53
+ getter: (row) => row[key] ?? '',
54
+ sortGetter: (row) => row[key] ?? '',
55
+ footer: (_, filteredRows) => `${filteredRows.length} éléments`,
56
+ filter: textFilter(key),
57
+ ...options,
58
+ },
59
+ });
60
+
61
+ export const phoneColumn = <R extends Record<string, any>>(
62
+ key: string,
63
+ title: string,
64
+ options?: Partial<DataGridColumn<R>>
65
+ ): DataGridColumns<R> => ({
66
+ [key]: {
67
+ name: title,
68
+ render: (row) => (
69
+ <a
70
+ // className="text-blue-500 hover:text-blue-700"
71
+ href={`tel:${row[key]}`}
72
+ >
73
+ {row[key] ?? ''}
74
+ </a>
75
+ ),
76
+ getter: (row) => row[key] ?? '',
77
+ sortGetter: (row) => row[key] ?? '',
78
+ footer: (_, filteredRows) => `${filteredRows.length} éléments`,
79
+ filter: textFilter(key),
80
+ ...options,
81
+ },
82
+ });
83
+
84
+ export const dateColumn = <R extends Record<string, any>>(
85
+ key: string,
86
+ title: string,
87
+ options?: Partial<DataGridColumn<R>>
88
+ ): DataGridColumns<R> => ({
89
+ [key]: {
90
+ name: title,
91
+ type: 'date',
92
+ render: (row) => moment(row[key]).format('DD/MM/YYYY') ?? '',
93
+ getter: (row) => row[key] ?? '',
94
+ sortGetter: (row) => row[key] ?? '',
95
+ footer: (_, filteredRows) => `${filteredRows.length} éléments`,
96
+ filter: {
97
+ ...dateFilter(key),
98
+ renderer: (value) => moment(value).format('DD/MM/YYYY') ?? '',
99
+ },
100
+ ...options,
101
+ },
102
+ });
103
+
104
+ export const monthColumn = <R extends Record<string, any>>(
105
+ key: string,
106
+ title: string,
107
+ options?: Partial<DataGridColumn<R>>
108
+ ): DataGridColumns<R> => ({
109
+ [key]: {
110
+ name: title,
111
+ render: (row) => (row[key] ? `${row[key]} mois ` : ''),
112
+ getter: (row) => row[key] ?? '',
113
+ sortGetter: (row) => row[key] ?? '',
114
+ footer: (_, filteredRows) => `${filteredRows.length} éléments`,
115
+ filter: textFilter(key),
116
+ ...options,
117
+ },
118
+ });
119
+
120
+ export const numberColumn = <R extends Record<string, any>>(
121
+ key: string,
122
+ title: string,
123
+ decimals = 2,
124
+ options?: Partial<DataGridColumn<R>>
125
+ ): DataGridColumns<R> => ({
126
+ [key]: {
127
+ name: title,
128
+ render: (row) => formatNumber(row[key], decimals) ?? '',
129
+ excelFormatter: () => buildExcelFormat(decimals),
130
+ excelValue: (value) => formatNumberInvariant(value, decimals),
131
+ getter: (row) => row[key] ?? '',
132
+ sortGetter: (row) => row[key] ?? '',
133
+ footer: {
134
+ sum: (_, filteredRows) =>
135
+ formatNumber(
136
+ filteredRows.reduce((acc, row) => acc + (row[key] ?? 0), 0),
137
+ decimals
138
+ ),
139
+ average: (_, filteredRows) =>
140
+ formatNumber(
141
+ filteredRows.reduce((acc, row) => acc + (row[key] ?? 0), 0) /
142
+ (filteredRows.length || 1),
143
+ decimals
144
+ ),
145
+ count: (_, filteredRows) => `${filteredRows.length} éléments`,
146
+ max: (_, filteredRows) =>
147
+ formatNumber(
148
+ Math.max(...filteredRows.map((row) => row[key] ?? 0)),
149
+ decimals
150
+ ),
151
+ min: (_, filteredRows) =>
152
+ formatNumber(
153
+ Math.min(...filteredRows.map((row) => row[key] ?? 0)),
154
+ decimals
155
+ ),
156
+ },
157
+ filter: {
158
+ ...numberFilter(key),
159
+ renderer: (value) => formatNumber(value, decimals) ?? '',
160
+ },
161
+ ...options,
162
+ },
163
+ });
164
+
165
+ export const moneyColumn = <R extends Record<string, any>>(
166
+ key: string,
167
+ title: string,
168
+ decimals = 2,
169
+ options?: Partial<DataGridColumn<R>>
170
+ ): DataGridColumns<R> => ({
171
+ [key]: {
172
+ name: title,
173
+ render: (row) => formatMoney(row[key], decimals) ?? '',
174
+ excelFormatter: () => buildExcelFormat(decimals, ' €'),
175
+ excelValue: (value) => formatNumberInvariant(value, decimals),
176
+ getter: (row) => row[key] ?? '',
177
+ sortGetter: (row) => row[key] ?? '',
178
+ filter: {
179
+ ...numberFilter(key),
180
+ renderer: (value) => formatMoney(value, decimals) ?? '',
181
+ },
182
+ footer: {
183
+ sum: (_, filteredRows) =>
184
+ formatMoney(
185
+ filteredRows.reduce((acc, row) => acc + (row[key] ?? 0), 0),
186
+ decimals
187
+ ),
188
+ average: (_, filteredRows) =>
189
+ formatMoney(
190
+ filteredRows.reduce((acc, row) => acc + (row[key] ?? 0), 0) /
191
+ (filteredRows.length || 1),
192
+ decimals
193
+ ),
194
+ count: (_, filteredRows) => `${filteredRows.length} éléments`,
195
+ max: (_, filteredRows) =>
196
+ formatMoney(
197
+ Math.max(...filteredRows.map((row) => row[key] ?? 0)),
198
+ decimals
199
+ ),
200
+ min: (_, filteredRows) =>
201
+ formatMoney(
202
+ Math.min(...filteredRows.map((row) => row[key] ?? 0)),
203
+ decimals
204
+ ),
205
+ },
206
+ ...options,
207
+ },
208
+ });
209
+
210
+ export const percentageColumn = <R extends Record<string, any>>(
211
+ key: string,
212
+ title: string,
213
+ decimals = 2,
214
+ options?: Partial<DataGridColumn<R>>
215
+ ): DataGridColumns<R> => ({
216
+ [key]: {
217
+ name: title,
218
+ render: (row) => formatPercentage(row[key]) ?? '',
219
+ excelFormatter: () => buildExcelFormat(decimals, '%'),
220
+ excelValue: (value) => formatNumberInvariant(value, decimals),
221
+ getter: (row) => row[key] ?? '',
222
+ sortGetter: (row) => row[key] ?? '',
223
+ filter: numberFilter(key),
224
+ footer: {
225
+ average: (_, filteredRows) =>
226
+ formatNumber(
227
+ filteredRows.reduce((acc, row) => acc + (row[key] ?? 0), 0) /
228
+ (filteredRows.length || 1),
229
+ decimals
230
+ ),
231
+ count: (_, filteredRows) => `${filteredRows.length} éléments`,
232
+ max: (_, filteredRows) =>
233
+ formatNumber(
234
+ Math.max(...filteredRows.map((row) => row[key] ?? 0)),
235
+ decimals
236
+ ),
237
+ min: (_, filteredRows) =>
238
+ formatNumber(
239
+ Math.min(...filteredRows.map((row) => row[key] ?? 0)),
240
+ decimals
241
+ ),
242
+ sum: (_, filteredRows) =>
243
+ formatNumber(
244
+ filteredRows.reduce((acc, row) => acc + (row[key] ?? 0), 0),
245
+ decimals
246
+ ),
247
+ },
248
+ ...options,
249
+ },
250
+ });
251
+
252
+ export const checkboxColumn = <R extends Record<string, any>>(
253
+ key: string,
254
+ title: string,
255
+ options?: Partial<DataGridColumn<R>>
256
+ ): DataGridColumns<R> => ({
257
+ [key]: {
258
+ name: title,
259
+ render: (row) => (
260
+ <>
261
+ <input type="checkbox" checked={row[key]} />
262
+ <span>{row[key] ? ' Oui' : ' Non'}</span>
263
+ </>
264
+ ),
265
+ getter: (row) => row[key] ?? '',
266
+ sortGetter: (row) => row[key] ?? '',
267
+ filter: numberFilter(key),
268
+ footer: {
269
+ count: (_, filteredRows) => `${filteredRows.length} éléments`,
270
+ checked: (_, filteredRows) =>
271
+ `${filteredRows.filter((row) => !!row[key]).length} cochés`,
272
+ unchecked: (_, filteredRows) =>
273
+ `${filteredRows.filter((row) => !row[key]).length} décochés`,
274
+ },
275
+ ...options,
276
+ },
277
+ });
278
+
279
+ export const colorColumn = <R extends Record<string, any>>(
280
+ key: string,
281
+ title: string,
282
+ options?: Partial<DataGridColumn<R>>
283
+ ): DataGridColumns<R> => ({
284
+ [key]: {
285
+ name: title,
286
+ render: (row) => (
287
+ <div style={{ backgroundColor: row[key] }}>{row[key] ?? ''}</div>
288
+ ),
289
+ getter: (row) => row[key] ?? '',
290
+ sortGetter: (row) => row[key] ?? '',
291
+ filter: textFilter(key),
292
+ footer: (rows) => `${rows.length} éléments`,
293
+ ...options,
294
+ },
295
+ });
@@ -0,0 +1,287 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+
3
+ import {
4
+ DataGridFilter,
5
+ DataGridFilterCheckbox,
6
+ DataGridFilterDataType,
7
+ DataGridFilterFormatter,
8
+ DataGridFilterGroup,
9
+ DataGridFilterPredicateBuilder,
10
+ DataGridFilterPredicates,
11
+ DataGridFilterRenderer,
12
+ DataGridFilterType,
13
+ DataGridFilterValue,
14
+ } from '../types';
15
+
16
+ import moment from 'moment';
17
+
18
+ const escapeRegExp = (s: string) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
19
+
20
+ const textContainsPredicate: DataGridFilterPredicateBuilder<string> = (
21
+ ...filterValues
22
+ ) => {
23
+ const regex = new RegExp(`${escapeRegExp(filterValues[0])}`, 'i');
24
+ return (valueToVerify: string) => regex.test(valueToVerify);
25
+ };
26
+ const textNotContainsPredicate: DataGridFilterPredicateBuilder<string> = (
27
+ ...filterValues
28
+ ) => {
29
+ const regex = new RegExp(`${escapeRegExp(filterValues[0])}`, 'i');
30
+ return (valueToVerify: string) => !regex.test(valueToVerify);
31
+ };
32
+ const textEqualsPredicate: DataGridFilterPredicateBuilder<string> = (
33
+ ...filterValues
34
+ ) => {
35
+ const regex = new RegExp(`^${escapeRegExp(filterValues[0])}$`, 'i');
36
+ return (valueToVerify: string) => regex.test(valueToVerify);
37
+ };
38
+ const textNotEqualsPredicate: DataGridFilterPredicateBuilder<string> = (
39
+ ...filterValues
40
+ ) => {
41
+ const regex = new RegExp(`^${escapeRegExp(filterValues[0])}$`, 'i');
42
+ return (valueToVerify: string) => !regex.test(valueToVerify);
43
+ };
44
+ const textStartsWithPredicate: DataGridFilterPredicateBuilder<string> = (
45
+ ...filterValues
46
+ ) => {
47
+ const regex = new RegExp(`^${escapeRegExp(filterValues[0])}`, 'i');
48
+ return (valueToVerify: string) => regex.test(valueToVerify);
49
+ };
50
+ const textEndsWithPredicate: DataGridFilterPredicateBuilder<string> = (
51
+ ...filterValues
52
+ ) => {
53
+ const regex = new RegExp(`${escapeRegExp(filterValues[0])}$`, 'i');
54
+ return (valueToVerify: string) => regex.test(valueToVerify);
55
+ };
56
+ const textInArrayPredicate: DataGridFilterPredicateBuilder<string> = (
57
+ ...filterValues
58
+ ) => {
59
+ return (valueToVerify: string) => filterValues.includes(valueToVerify);
60
+ };
61
+
62
+ const numberEqualsPredicate: DataGridFilterPredicateBuilder<number> =
63
+ (...filterValues) =>
64
+ (valueToVerify: number) =>
65
+ valueToVerify === filterValues[0];
66
+ const numberNotEqualsPredicate: DataGridFilterPredicateBuilder<number> =
67
+ (...filterValues) =>
68
+ (valueToVerify: number) =>
69
+ valueToVerify !== filterValues[0];
70
+ const numberLessThanPredicate: DataGridFilterPredicateBuilder<number> =
71
+ (...filterValues) =>
72
+ (valueToVerify: number) =>
73
+ valueToVerify < filterValues[0];
74
+ const numberLessThanOrEqualPredicate: DataGridFilterPredicateBuilder<number> =
75
+ (...filterValues) =>
76
+ (valueToVerify: number) =>
77
+ valueToVerify <= filterValues[0];
78
+ const numberGreaterThanPredicate: DataGridFilterPredicateBuilder<number> =
79
+ (...filterValues) =>
80
+ (valueToVerify: number) =>
81
+ valueToVerify > filterValues[0];
82
+ const numberGreaterThanOrEqualPredicate: DataGridFilterPredicateBuilder<
83
+ number
84
+ > =
85
+ (...filterValues) =>
86
+ (valueToVerify: number) =>
87
+ valueToVerify >= filterValues[0];
88
+ const numberInRangePredicate: DataGridFilterPredicateBuilder<number> =
89
+ (...filterValues) =>
90
+ (valueToVerify: number) =>
91
+ valueToVerify >= filterValues[0] && valueToVerify <= filterValues[1];
92
+ const numberInArrayPredicate: DataGridFilterPredicateBuilder<number> = (
93
+ ...filterValues
94
+ ) => {
95
+ return (valueToVerify: number) => filterValues.includes(valueToVerify);
96
+ };
97
+
98
+ const dateBeforePredicate: DataGridFilterPredicateBuilder<string> = (
99
+ ...filterValues
100
+ ) => {
101
+ const limitDate = moment(filterValues[0]);
102
+ return (valueToVerify) => moment(valueToVerify).isBefore(limitDate);
103
+ };
104
+ const dateAfterPredicate: DataGridFilterPredicateBuilder<string> = (
105
+ ...filterValues
106
+ ) => {
107
+ const limitDate = moment(filterValues[0]);
108
+ return (valueToVerify) => moment(valueToVerify).isAfter(limitDate);
109
+ };
110
+ const dateEqualsPredicate: DataGridFilterPredicateBuilder<string> = (
111
+ ...filterValues
112
+ ) => {
113
+ const date = moment(filterValues[0]);
114
+ return (valueToVerify) => moment(valueToVerify).isSame(date);
115
+ };
116
+ const dateNotEqualsPredicate: DataGridFilterPredicateBuilder<string> = (
117
+ ...filterValues
118
+ ) => {
119
+ const date = moment(filterValues[0]);
120
+ return (valueToVerify) => !moment(valueToVerify).isSame(date);
121
+ };
122
+ const dateInRangePredicate: DataGridFilterPredicateBuilder<string> = (
123
+ ...filterValues
124
+ ) => {
125
+ const date1 = moment(filterValues[0]);
126
+ const date2 = moment(filterValues[1]);
127
+ const startDate = date1.isBefore(date2) ? date1 : date2;
128
+ const endDate = date1.isBefore(date2) ? date2 : date1;
129
+ return (valueToVerify) =>
130
+ moment(valueToVerify).isBetween(startDate, endDate, 'days', '[]');
131
+ };
132
+ const dateInArrayPredicate: DataGridFilterPredicateBuilder<string> = (
133
+ ...filterValues
134
+ ) => {
135
+ return (valueToVerify) => filterValues.includes(valueToVerify);
136
+ };
137
+
138
+ export const filtersPredicates: DataGridFilterPredicates = {
139
+ date: {
140
+ before: dateBeforePredicate,
141
+ after: dateAfterPredicate,
142
+ equals: dateEqualsPredicate,
143
+ notEquals: dateNotEqualsPredicate,
144
+ inRange: dateInRangePredicate,
145
+ inArray: dateInArrayPredicate,
146
+ },
147
+ text: {
148
+ contains: textContainsPredicate,
149
+ notContains: textNotContainsPredicate,
150
+ equals: textEqualsPredicate,
151
+ notEquals: textNotEqualsPredicate,
152
+ startsWith: textStartsWithPredicate,
153
+ endsWith: textEndsWithPredicate,
154
+ inArray: textInArrayPredicate,
155
+ },
156
+ number: {
157
+ equals: numberEqualsPredicate,
158
+ notEquals: numberNotEqualsPredicate,
159
+ lessThan: numberLessThanPredicate,
160
+ lessThanOrEqual: numberLessThanOrEqualPredicate,
161
+ greaterThan: numberGreaterThanPredicate,
162
+ greaterThanOrEqual: numberGreaterThanOrEqualPredicate,
163
+ inRange: numberInRangePredicate,
164
+ inArray: numberInArrayPredicate,
165
+ },
166
+ };
167
+
168
+ export const applyFilters = <R>(rows: R[], filters: DataGridFilter[]): R[] => {
169
+ return filters.reduce(
170
+ (tempRows, filter) => applyFilter(tempRows, filter),
171
+ rows
172
+ );
173
+ };
174
+
175
+ export const applyFilter = <R>(rows: R[], filter: DataGridFilter): R[] => {
176
+ const predicateBuilder = (filtersPredicates as any)[filter.type][
177
+ filter.operator
178
+ ];
179
+ if (typeof predicateBuilder !== 'function') return rows;
180
+ const predicate = predicateBuilder(...filter.values);
181
+ return rows.filter((row) => predicate(filter.getter(row)));
182
+ };
183
+
184
+ export const defaultFilterGetter = (row: any, columnKey: string) =>
185
+ row[columnKey];
186
+
187
+ export const defaultFilterValues: {
188
+ [K in DataGridFilterType]: DataGridFilterDataType<K>;
189
+ } = {
190
+ date: '',
191
+ text: '',
192
+ number: 0,
193
+ };
194
+
195
+ export const defaultValueParsers: {
196
+ [K in DataGridFilterType]: (value: string) => DataGridFilterDataType<K>;
197
+ } = {
198
+ date: (value) => moment(value).format('YYYY-MM-DD'),
199
+ text: (value) => value,
200
+ number: (value) => parseFloat(value),
201
+ };
202
+
203
+ export const groupDatesByYearAndMonth = (dates: any[]) =>
204
+ dates.reduce((acc: Record<string, Record<string, any[]>>, date) => {
205
+ const year = moment(date).format('YYYY');
206
+ if (!acc[year]) {
207
+ acc[year] = {};
208
+ }
209
+ const yearAndMonth = moment(date).format(`MM/YYYY`);
210
+ if (!acc[year][yearAndMonth]) {
211
+ acc[year][yearAndMonth] = [];
212
+ }
213
+ acc[year][yearAndMonth].push(date);
214
+ return acc;
215
+ }, {} as Record<string, Record<string, any[]>>);
216
+
217
+ export const getDateGroups = (dates: any[]): DataGridFilterGroup[] => {
218
+ const grouperDates = groupDatesByYearAndMonth(dates);
219
+ return Object.entries(grouperDates).map(([year, months]) => ({
220
+ name: year,
221
+ displayValue: year,
222
+ values: Object.values(months).flat(),
223
+ groups: Object.entries(months).map(([month, dates]) => ({
224
+ displayValue: month,
225
+ name: month,
226
+ values: dates,
227
+ })),
228
+ }));
229
+ };
230
+
231
+ export const defaultRendererAndFormatter = (value: any) =>
232
+ String(value ?? '').trim() || '(Vides)';
233
+
234
+ export const getCheckboxes = (
235
+ values: DataGridFilterValue[],
236
+ displayValue: DataGridFilterValue,
237
+ renderer: DataGridFilterRenderer,
238
+ formatter: DataGridFilterFormatter,
239
+ groups?: DataGridFilterGroup[],
240
+ level = 0
241
+ ): DataGridFilterCheckbox[] =>
242
+ groups
243
+ ? groups
244
+ .map<DataGridFilterCheckbox[]>((group) => [
245
+ {
246
+ displayValue: group.displayValue,
247
+ title: formatter(group.displayValue),
248
+ values: group.values ?? [],
249
+ level,
250
+ },
251
+ ...getCheckboxes(
252
+ group.values ?? [],
253
+ group.displayValue,
254
+ renderer,
255
+ formatter,
256
+ group.groups,
257
+ level + 1
258
+ ),
259
+ ])
260
+ .flat()
261
+ : values.map<DataGridFilterCheckbox>((value) => ({
262
+ displayValue: renderer(value),
263
+ title: formatter(value),
264
+ values: [value ?? ''],
265
+ level,
266
+ }));
267
+
268
+ export const dateFilter = (key: string): DataGridFilter<'date'> => ({
269
+ type: 'date',
270
+ operator: 'before',
271
+ values: [''],
272
+ getter: (row) => row[key] ?? '',
273
+ });
274
+
275
+ export const textFilter = (key: string): DataGridFilter<'text'> => ({
276
+ type: 'text',
277
+ operator: 'contains',
278
+ values: [''],
279
+ getter: (row) => row[key] ?? '',
280
+ });
281
+
282
+ export const numberFilter = (key: string): DataGridFilter<'number'> => ({
283
+ type: 'number',
284
+ operator: 'equals',
285
+ values: [0],
286
+ getter: (row) => row[key] ?? '',
287
+ });
@@ -0,0 +1,2 @@
1
+ export * from './filters';
2
+ export * from './columns';
@@ -0,0 +1,30 @@
1
+ import { DataGridColumns, DataGridContext } from '../types';
2
+ import { useContext, useMemo } from 'react';
3
+
4
+ import { isColumnVisible } from '../helpers/columns';
5
+ import { pickBy } from 'lodash';
6
+
7
+ export { useDataGridCopy } from './useDataGridCopy';
8
+ export { useDataGridSettings } from './useDataGridSettings';
9
+ export { useDataGrid } from './useDataGrid';
10
+
11
+ export const useDataGridContext = <R>(context: DataGridContext<R>) =>
12
+ useContext(context);
13
+
14
+ export const useVisibleAndHiddenColumns = <R>(columns: DataGridColumns<R>) =>
15
+ useMemo(
16
+ () => [
17
+ pickBy(columns, (col) => isColumnVisible(col)),
18
+ pickBy(columns, (col) => !isColumnVisible(col)),
19
+ ],
20
+ [columns]
21
+ );
22
+
23
+ export const useSortedColumns = <R>(columns: DataGridColumns<R>) =>
24
+ useMemo(
25
+ () =>
26
+ Object.entries(columns)
27
+ .filter(([, col]) => isColumnVisible(col))
28
+ .sort((a, b) => (a[1].order ?? 0) - (b[1].order ?? 0)),
29
+ [columns]
30
+ );