@patternfly/react-data-view 5.4.1 → 5.5.0

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 (87) hide show
  1. package/dist/cjs/DataViewFilters/DataViewFilters.d.ts +19 -0
  2. package/dist/cjs/DataViewFilters/DataViewFilters.js +70 -0
  3. package/dist/cjs/DataViewFilters/DataViewFilters.test.d.ts +1 -0
  4. package/dist/cjs/DataViewFilters/DataViewFilters.test.js +19 -0
  5. package/dist/cjs/DataViewFilters/index.d.ts +2 -0
  6. package/dist/cjs/DataViewFilters/index.js +23 -0
  7. package/dist/cjs/DataViewTable/DataViewTable.d.ts +8 -0
  8. package/dist/cjs/DataViewTableBasic/DataViewTableBasic.d.ts +1 -0
  9. package/dist/cjs/DataViewTableHead/DataViewTableHead.d.ts +1 -0
  10. package/dist/cjs/DataViewTableTree/DataViewTableTree.d.ts +1 -0
  11. package/dist/cjs/DataViewTextFilter/DataViewTextFilter.d.ts +21 -0
  12. package/dist/cjs/DataViewTextFilter/DataViewTextFilter.js +26 -0
  13. package/dist/cjs/DataViewTextFilter/DataViewTextFilter.test.d.ts +1 -0
  14. package/dist/cjs/DataViewTextFilter/DataViewTextFilter.test.js +22 -0
  15. package/dist/cjs/DataViewTextFilter/index.d.ts +2 -0
  16. package/dist/cjs/DataViewTextFilter/index.js +23 -0
  17. package/dist/cjs/DataViewToolbar/DataViewToolbar.d.ts +10 -4
  18. package/dist/cjs/DataViewToolbar/DataViewToolbar.js +29 -6
  19. package/dist/cjs/Hooks/filters.d.ts +14 -0
  20. package/dist/cjs/Hooks/filters.js +70 -0
  21. package/dist/cjs/Hooks/filters.test.d.ts +1 -0
  22. package/dist/cjs/Hooks/filters.test.js +50 -0
  23. package/dist/cjs/Hooks/index.d.ts +1 -0
  24. package/dist/cjs/Hooks/index.js +1 -0
  25. package/dist/cjs/Hooks/pagination.d.ts +1 -0
  26. package/dist/cjs/Hooks/selection.d.ts +1 -1
  27. package/dist/cjs/Hooks/selection.js +4 -2
  28. package/dist/cjs/InternalContext/InternalContext.d.ts +1 -0
  29. package/dist/cjs/index.d.ts +2 -0
  30. package/dist/cjs/index.js +4 -1
  31. package/dist/dynamic/DataViewFilters/package.json +1 -0
  32. package/dist/dynamic/DataViewTextFilter/package.json +1 -0
  33. package/dist/esm/DataViewFilters/DataViewFilters.d.ts +19 -0
  34. package/dist/esm/DataViewFilters/DataViewFilters.js +43 -0
  35. package/dist/esm/DataViewFilters/DataViewFilters.test.d.ts +1 -0
  36. package/dist/esm/DataViewFilters/DataViewFilters.test.js +14 -0
  37. package/dist/esm/DataViewFilters/index.d.ts +2 -0
  38. package/dist/esm/DataViewFilters/index.js +2 -0
  39. package/dist/esm/DataViewTable/DataViewTable.d.ts +8 -0
  40. package/dist/esm/DataViewTableBasic/DataViewTableBasic.d.ts +1 -0
  41. package/dist/esm/DataViewTableHead/DataViewTableHead.d.ts +1 -0
  42. package/dist/esm/DataViewTableTree/DataViewTableTree.d.ts +1 -0
  43. package/dist/esm/DataViewTextFilter/DataViewTextFilter.d.ts +21 -0
  44. package/dist/esm/DataViewTextFilter/DataViewTextFilter.js +19 -0
  45. package/dist/esm/DataViewTextFilter/DataViewTextFilter.test.d.ts +1 -0
  46. package/dist/esm/DataViewTextFilter/DataViewTextFilter.test.js +17 -0
  47. package/dist/esm/DataViewTextFilter/index.d.ts +2 -0
  48. package/dist/esm/DataViewTextFilter/index.js +2 -0
  49. package/dist/esm/DataViewToolbar/DataViewToolbar.d.ts +10 -4
  50. package/dist/esm/DataViewToolbar/DataViewToolbar.js +7 -4
  51. package/dist/esm/Hooks/filters.d.ts +14 -0
  52. package/dist/esm/Hooks/filters.js +66 -0
  53. package/dist/esm/Hooks/filters.test.d.ts +1 -0
  54. package/dist/esm/Hooks/filters.test.js +48 -0
  55. package/dist/esm/Hooks/index.d.ts +1 -0
  56. package/dist/esm/Hooks/index.js +1 -0
  57. package/dist/esm/Hooks/pagination.d.ts +1 -0
  58. package/dist/esm/Hooks/selection.d.ts +1 -1
  59. package/dist/esm/Hooks/selection.js +4 -2
  60. package/dist/esm/InternalContext/InternalContext.d.ts +1 -0
  61. package/dist/esm/index.d.ts +2 -0
  62. package/dist/esm/index.js +2 -0
  63. package/package.json +1 -1
  64. package/patternfly-docs/content/extensions/data-view/examples/Components/Components.md +2 -2
  65. package/patternfly-docs/content/extensions/data-view/examples/Functionality/FiltersExample.tsx +92 -0
  66. package/patternfly-docs/content/extensions/data-view/examples/Functionality/Functionality.md +36 -2
  67. package/src/DataViewFilters/DataViewFilters.test.tsx +21 -0
  68. package/src/DataViewFilters/DataViewFilters.tsx +125 -0
  69. package/src/DataViewFilters/__snapshots__/DataViewFilters.test.tsx.snap +182 -0
  70. package/src/DataViewFilters/index.tsx +2 -0
  71. package/src/DataViewTable/DataViewTable.tsx +23 -3
  72. package/src/DataViewTableBasic/DataViewTableBasic.tsx +1 -0
  73. package/src/DataViewTableHead/DataViewTableHead.tsx +1 -0
  74. package/src/DataViewTableTree/DataViewTableTree.tsx +1 -0
  75. package/src/DataViewTextFilter/DataViewTextFilter.test.tsx +24 -0
  76. package/src/DataViewTextFilter/DataViewTextFilter.tsx +53 -0
  77. package/src/DataViewTextFilter/__snapshots__/DataViewTextFilter.test.tsx.snap +196 -0
  78. package/src/DataViewTextFilter/index.ts +2 -0
  79. package/src/DataViewToolbar/DataViewToolbar.tsx +47 -28
  80. package/src/DataViewToolbar/__snapshots__/DataViewToolbar.test.tsx.snap +28 -0
  81. package/src/Hooks/filters.test.tsx +62 -0
  82. package/src/Hooks/filters.ts +96 -0
  83. package/src/Hooks/index.ts +1 -0
  84. package/src/Hooks/pagination.ts +1 -0
  85. package/src/Hooks/selection.ts +3 -2
  86. package/src/InternalContext/InternalContext.tsx +1 -0
  87. package/src/index.ts +3 -0
@@ -0,0 +1,196 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`DataViewTextFilter component should render correctly 1`] = `
4
+ <div>
5
+ <div
6
+ class="pf-v5-c-toolbar"
7
+ data-ouia-component-id="DataViewToolbar"
8
+ data-ouia-component-type="PF5/Toolbar"
9
+ data-ouia-safe="true"
10
+ id="pf-random-id-0"
11
+ >
12
+ <div
13
+ class="pf-v5-c-toolbar__content"
14
+ >
15
+ <div
16
+ class="pf-v5-c-toolbar__content-section"
17
+ >
18
+ <div
19
+ class="pf-v5-c-toolbar__item pf-m-search-filter"
20
+ >
21
+ <div
22
+ class="pf-v5-c-toolbar__item"
23
+ data-ouia-component-id="DataViewTextFilter"
24
+ >
25
+ <div
26
+ class="pf-v5-c-text-input-group"
27
+ data-ouia-component-id="DataViewTextFilter-input"
28
+ >
29
+ <div
30
+ class="pf-v5-c-text-input-group__main pf-m-icon"
31
+ >
32
+ <span
33
+ class="pf-v5-c-text-input-group__text"
34
+ >
35
+ <span
36
+ class="pf-v5-c-text-input-group__icon"
37
+ >
38
+ <svg
39
+ aria-hidden="true"
40
+ class="pf-v5-svg"
41
+ fill="currentColor"
42
+ height="1em"
43
+ role="img"
44
+ viewBox="0 0 512 512"
45
+ width="1em"
46
+ >
47
+ <path
48
+ d="M505 442.7L405.3 343c-4.5-4.5-10.6-7-17-7H372c27.6-35.3 44-79.7 44-128C416 93.1 322.9 0 208 0S0 93.1 0 208s93.1 208 208 208c48.3 0 92.7-16.4 128-44v16.3c0 6.4 2.5 12.5 7 17l99.7 99.7c9.4 9.4 24.6 9.4 33.9 0l28.3-28.3c9.4-9.4 9.4-24.6.1-34zM208 336c-70.7 0-128-57.2-128-128 0-70.7 57.2-128 128-128 70.7 0 128 57.2 128 128 0 70.7-57.2 128-128 128z"
49
+ />
50
+ </svg>
51
+ </span>
52
+ <input
53
+ aria-label="Test Filter filter"
54
+ class="pf-v5-c-text-input-group__text-input"
55
+ id="test-filter"
56
+ placeholder="Filter by Test Filter"
57
+ type="text"
58
+ value="initial value"
59
+ />
60
+ </span>
61
+ </div>
62
+ <div
63
+ class="pf-v5-c-text-input-group__utilities"
64
+ >
65
+ <button
66
+ aria-disabled="false"
67
+ aria-label="Reset"
68
+ class="pf-v5-c-button pf-m-plain"
69
+ data-ouia-component-id="OUIA-Generated-Button-plain-2"
70
+ data-ouia-component-type="PF5/Button"
71
+ data-ouia-safe="true"
72
+ type="button"
73
+ >
74
+ <svg
75
+ aria-hidden="true"
76
+ class="pf-v5-svg"
77
+ fill="currentColor"
78
+ height="1em"
79
+ role="img"
80
+ viewBox="0 0 352 512"
81
+ width="1em"
82
+ >
83
+ <path
84
+ d="M242.72 256l100.07-100.07c12.28-12.28 12.28-32.19 0-44.48l-22.24-22.24c-12.28-12.28-32.19-12.28-44.48 0L176 189.28 75.93 89.21c-12.28-12.28-32.19-12.28-44.48 0L9.21 111.45c-12.28 12.28-12.28 32.19 0 44.48L109.28 256 9.21 356.07c-12.28 12.28-12.28 32.19 0 44.48l22.24 22.24c12.28 12.28 32.2 12.28 44.48 0L176 322.72l100.07 100.07c12.28 12.28 32.2 12.28 44.48 0l22.24-22.24c12.28-12.28 12.28-32.19 0-44.48L242.72 256z"
85
+ />
86
+ </svg>
87
+ </button>
88
+ </div>
89
+ </div>
90
+ </div>
91
+ </div>
92
+ </div>
93
+ </div>
94
+ <div
95
+ class="pf-v5-c-toolbar__content pf-m-chip-container"
96
+ >
97
+ <div
98
+ class="pf-v5-c-toolbar__group"
99
+ >
100
+ <div
101
+ class="pf-v5-c-toolbar__item pf-m-chip-group"
102
+ >
103
+ <div
104
+ aria-labelledby="pf-random-id-1"
105
+ class="pf-v5-c-chip-group pf-m-category"
106
+ data-ouia-component-type="PF5/ChipGroup"
107
+ data-ouia-safe="true"
108
+ role="group"
109
+ >
110
+ <div
111
+ class="pf-v5-c-chip-group__main"
112
+ >
113
+ <span
114
+ class="pf-v5-c-chip-group__label"
115
+ id="pf-random-id-1"
116
+ >
117
+ Test Filter
118
+ </span>
119
+ <ul
120
+ aria-labelledby="pf-random-id-1"
121
+ class="pf-v5-c-chip-group__list"
122
+ role="list"
123
+ >
124
+ <li
125
+ class="pf-v5-c-chip-group__list-item"
126
+ >
127
+ <div
128
+ class="pf-v5-c-chip"
129
+ data-ouia-component-id="OUIA-Generated-Chip-1"
130
+ data-ouia-component-type="PF5/Chip"
131
+ data-ouia-safe="true"
132
+ >
133
+ <span
134
+ class="pf-v5-c-chip__content"
135
+ >
136
+ <span
137
+ class="pf-v5-c-chip__text"
138
+ id="pf-random-id-2"
139
+ >
140
+ initial value
141
+ </span>
142
+ </span>
143
+ <span
144
+ class="pf-v5-c-chip__actions"
145
+ >
146
+ <button
147
+ aria-disabled="false"
148
+ aria-label="close"
149
+ aria-labelledby="remove_pf-random-id-2 pf-random-id-2"
150
+ class="pf-v5-c-button pf-m-plain"
151
+ data-ouia-component-id="close"
152
+ data-ouia-component-type="PF5/Button"
153
+ data-ouia-safe="true"
154
+ id="remove_pf-random-id-2"
155
+ type="button"
156
+ >
157
+ <svg
158
+ aria-hidden="true"
159
+ class="pf-v5-svg"
160
+ fill="currentColor"
161
+ height="1em"
162
+ role="img"
163
+ viewBox="0 0 352 512"
164
+ width="1em"
165
+ >
166
+ <path
167
+ d="M242.72 256l100.07-100.07c12.28-12.28 12.28-32.19 0-44.48l-22.24-22.24c-12.28-12.28-32.19-12.28-44.48 0L176 189.28 75.93 89.21c-12.28-12.28-32.19-12.28-44.48 0L9.21 111.45c-12.28 12.28-12.28 32.19 0 44.48L109.28 256 9.21 356.07c-12.28 12.28-12.28 32.19 0 44.48l22.24 22.24c12.28 12.28 32.2 12.28 44.48 0L176 322.72l100.07 100.07c12.28 12.28 32.2 12.28 44.48 0l22.24-22.24c12.28-12.28 12.28-32.19 0-44.48L242.72 256z"
168
+ />
169
+ </svg>
170
+ </button>
171
+ </span>
172
+ </div>
173
+ </li>
174
+ </ul>
175
+ </div>
176
+ </div>
177
+ </div>
178
+ </div>
179
+ <div
180
+ class="pf-v5-c-toolbar__item"
181
+ >
182
+ <button
183
+ aria-disabled="false"
184
+ class="pf-v5-c-button pf-m-link pf-m-inline"
185
+ data-ouia-component-id="DataViewToolbar-clear-all-filters"
186
+ data-ouia-component-type="PF5/Button"
187
+ data-ouia-safe="true"
188
+ type="button"
189
+ >
190
+ Clear filters
191
+ </button>
192
+ </div>
193
+ </div>
194
+ </div>
195
+ </div>
196
+ `;
@@ -0,0 +1,2 @@
1
+ export { default } from './DataViewTextFilter';
2
+ export * from './DataViewTextFilter';
@@ -1,41 +1,60 @@
1
- import React, { PropsWithChildren } from 'react';
2
- import { Toolbar, ToolbarContent, ToolbarItem, ToolbarItemVariant } from '@patternfly/react-core';
1
+ import React, { PropsWithChildren, useRef } from 'react';
2
+ import { Button, Toolbar, ToolbarContent, ToolbarItem, ToolbarItemVariant, ToolbarProps } from '@patternfly/react-core';
3
3
 
4
- export interface DataViewToolbarProps extends PropsWithChildren {
4
+ /** extends ToolbarProps */
5
+ export interface DataViewToolbarProps extends Omit<PropsWithChildren<ToolbarProps>, 'ref'> {
5
6
  /** Toolbar className */
6
7
  className?: string;
7
8
  /** Custom OUIA ID */
8
9
  ouiaId?: string;
9
- /** React component to display bulk select */
10
+ /** React node to display bulk select */
10
11
  bulkSelect?: React.ReactNode;
11
- /** React component to display pagination */
12
+ /** React node to display pagination */
12
13
  pagination?: React.ReactNode;
13
- /** React component to display actions */
14
+ /** React node to display actions */
14
15
  actions?: React.ReactNode;
16
+ /** React node to display filters */
17
+ filters?: React.ReactNode;
18
+ /** React node to display custom filter chips */
19
+ customChipGroupContent?: React.ReactNode;
15
20
  }
16
21
 
17
- export const DataViewToolbar: React.FC<DataViewToolbarProps> = ({ className, ouiaId = 'DataViewToolbar', bulkSelect, actions, pagination, children, ...props }: DataViewToolbarProps) => (
18
- <Toolbar ouiaId={ouiaId} className={className} {...props}>
19
- <ToolbarContent>
20
- {bulkSelect && (
21
- <ToolbarItem data-ouia-component-id={`${ouiaId}-bulk-select`}>
22
- {bulkSelect}
23
- </ToolbarItem>
24
- )}
25
- {actions && (
26
- <ToolbarItem variant={ToolbarItemVariant['overflow-menu']}>
27
- {actions}
28
- </ToolbarItem>
29
- )}
30
- {pagination && (
31
- <ToolbarItem variant={ToolbarItemVariant.pagination} data-ouia-component-id={`${ouiaId}-pagination`}>
32
- {pagination}
33
- </ToolbarItem>
34
- )}
35
- {children}
36
- </ToolbarContent>
37
- </Toolbar>
38
- )
22
+ export const DataViewToolbar: React.FC<DataViewToolbarProps> = ({ className, ouiaId = 'DataViewToolbar', bulkSelect, actions, pagination, filters, customChipGroupContent, clearAllFilters, children, ...props }: DataViewToolbarProps) => {
23
+ const defaultClearFilters = useRef(
24
+ <ToolbarItem>
25
+ <Button ouiaId={`${ouiaId}-clear-all-filters`} variant="link" onClick={clearAllFilters} isInline>
26
+ Clear filters
27
+ </Button>
28
+ </ToolbarItem>
29
+ );
30
+ return (
31
+ <Toolbar ouiaId={ouiaId} className={className} customChipGroupContent={customChipGroupContent ?? defaultClearFilters.current} {...props}>
32
+ <ToolbarContent>
33
+ {bulkSelect && (
34
+ <ToolbarItem data-ouia-component-id={`${ouiaId}-bulk-select`}>
35
+ {bulkSelect}
36
+ </ToolbarItem>
37
+ )}
38
+ {actions && (
39
+ <ToolbarItem variant={ToolbarItemVariant['overflow-menu']}>
40
+ {actions}
41
+ </ToolbarItem>
42
+ )}
43
+ {filters && (
44
+ <ToolbarItem variant={ToolbarItemVariant['search-filter']}>
45
+ {filters}
46
+ </ToolbarItem>
47
+ )}
48
+ {pagination && (
49
+ <ToolbarItem variant={ToolbarItemVariant.pagination} data-ouia-component-id={`${ouiaId}-pagination`}>
50
+ {pagination}
51
+ </ToolbarItem>
52
+ )}
53
+ {children}
54
+ </ToolbarContent>
55
+ </Toolbar>
56
+ )
57
+ };
39
58
 
40
59
  export default DataViewToolbar;
41
60
 
@@ -243,6 +243,20 @@ exports[`DataViewToolbar component should render correctly 1`] = `
243
243
  <div
244
244
  class="pf-v5-c-toolbar__group"
245
245
  />
246
+ <div
247
+ class="pf-v5-c-toolbar__item"
248
+ >
249
+ <button
250
+ aria-disabled="false"
251
+ class="pf-v5-c-button pf-m-link pf-m-inline"
252
+ data-ouia-component-id="DataViewToolbar-clear-all-filters"
253
+ data-ouia-component-type="PF5/Button"
254
+ data-ouia-safe="true"
255
+ type="button"
256
+ >
257
+ Clear filters
258
+ </button>
259
+ </div>
246
260
  </div>
247
261
  </div>
248
262
  </div>
@@ -486,6 +500,20 @@ exports[`DataViewToolbar component should render correctly 1`] = `
486
500
  <div
487
501
  class="pf-v5-c-toolbar__group"
488
502
  />
503
+ <div
504
+ class="pf-v5-c-toolbar__item"
505
+ >
506
+ <button
507
+ aria-disabled="false"
508
+ class="pf-v5-c-button pf-m-link pf-m-inline"
509
+ data-ouia-component-id="DataViewToolbar-clear-all-filters"
510
+ data-ouia-component-type="PF5/Button"
511
+ data-ouia-safe="true"
512
+ type="button"
513
+ >
514
+ Clear filters
515
+ </button>
516
+ </div>
489
517
  </div>
490
518
  </div>
491
519
  </div>,
@@ -0,0 +1,62 @@
1
+ import '@testing-library/jest-dom';
2
+ import { renderHook, act } from '@testing-library/react';
3
+ import { useDataViewFilters, UseDataViewFiltersProps } from './filters';
4
+
5
+ describe('useDataViewFilters', () => {
6
+ const initialFilters = { search: 'test', tags: [ 'tag1', 'tag2' ] };
7
+
8
+ it('should initialize with provided initial filters', () => {
9
+ const { result } = renderHook(() => useDataViewFilters({ initialFilters }));
10
+ expect(result.current.filters).toEqual(initialFilters);
11
+ });
12
+
13
+ it('should initialize with empty filters if no initialFilters provided', () => {
14
+ const { result } = renderHook(() => useDataViewFilters({}));
15
+ expect(result.current.filters).toEqual({});
16
+ });
17
+
18
+ it('should set filters correctly', () => {
19
+ const { result } = renderHook(() => useDataViewFilters({ initialFilters }));
20
+ const newFilters = { search: 'new search' };
21
+ act(() => result.current.onSetFilters(newFilters));
22
+
23
+ expect(result.current.filters).toEqual({ ...initialFilters, ...newFilters });
24
+ });
25
+
26
+ it('should delete specific filters without removing keys', () => {
27
+ const { result } = renderHook(() => useDataViewFilters({ initialFilters }));
28
+ const filtersToDelete = { search: 'test' };
29
+ act(() => result.current.onDeleteFilters(filtersToDelete));
30
+
31
+ expect(result.current.filters).toEqual({ search: '', tags: [ 'tag1', 'tag2' ] });
32
+ });
33
+
34
+ it('should clear all filters', () => {
35
+ const { result } = renderHook(() => useDataViewFilters({ initialFilters }));
36
+ act(() => result.current.clearAllFilters());
37
+
38
+ expect(result.current.filters).toEqual({ search: '', tags: [] });
39
+ });
40
+
41
+ it('should sync with URL search params if isUrlSyncEnabled', () => {
42
+ const searchParams = new URLSearchParams();
43
+ const setSearchParams = jest.fn();
44
+ const props: UseDataViewFiltersProps<typeof initialFilters> = {
45
+ initialFilters,
46
+ searchParams,
47
+ setSearchParams,
48
+ };
49
+
50
+ const { result } = renderHook(() => useDataViewFilters(props));
51
+ act(() => result.current.onSetFilters({ search: 'new search' }));
52
+
53
+ expect(setSearchParams).toHaveBeenCalled();
54
+ });
55
+
56
+ it('should reset filters to default values when clearAllFilters is called', () => {
57
+ const { result } = renderHook(() => useDataViewFilters({ initialFilters }));
58
+ act(() => result.current.clearAllFilters());
59
+
60
+ expect(result.current.filters).toEqual({ search: '', tags: [] });
61
+ });
62
+ });
@@ -0,0 +1,96 @@
1
+ import { useState, useCallback, useEffect, useMemo } from "react";
2
+
3
+ export interface UseDataViewFiltersProps<T extends object> {
4
+ /** Initial filters object */
5
+ initialFilters?: T;
6
+ /** Current search parameters as a string */
7
+ searchParams?: URLSearchParams;
8
+ /** Function to set search parameters */
9
+ setSearchParams?: (params: URLSearchParams) => void;
10
+ };
11
+
12
+ export const useDataViewFilters = <T extends object>({
13
+ initialFilters = {} as T,
14
+ searchParams,
15
+ setSearchParams,
16
+ }: UseDataViewFiltersProps<T>) => {
17
+ const isUrlSyncEnabled = useMemo(() => searchParams && !!setSearchParams, [ searchParams, setSearchParams ]);
18
+
19
+ const getInitialFilters = useCallback((): T => isUrlSyncEnabled ? Object.keys(initialFilters).reduce((loadedFilters, key) => {
20
+ const urlValue = searchParams?.get(key);
21
+ loadedFilters[key as keyof T] = urlValue
22
+ ? (urlValue as T[keyof T] | T[keyof T])
23
+ : initialFilters[key as keyof T];
24
+ return loadedFilters;
25
+ // eslint-disable-next-line react-hooks/exhaustive-deps
26
+ }, { ...initialFilters }) : initialFilters, [ isUrlSyncEnabled, JSON.stringify(initialFilters), searchParams?.toString() ]);
27
+
28
+ const [ filters, setFilters ] = useState<T>(getInitialFilters());
29
+
30
+ const updateSearchParams = useCallback(
31
+ (newFilters: T) => {
32
+ if (isUrlSyncEnabled) {
33
+ const params = new URLSearchParams(searchParams);
34
+ Object.entries(newFilters).forEach(([ key, value ]) => {
35
+ if (value) {
36
+ params.set(key, Array.isArray(value) ? value.join(',') : value);
37
+ } else {
38
+ params.delete(key);
39
+ }
40
+ });
41
+ setSearchParams?.(params);
42
+ }
43
+ },
44
+ [ isUrlSyncEnabled, searchParams, setSearchParams ]
45
+ );
46
+
47
+ useEffect(() => {
48
+ isUrlSyncEnabled && setFilters(getInitialFilters())
49
+ }, []); // eslint-disable-line react-hooks/exhaustive-deps
50
+
51
+ const onSetFilters = useCallback(
52
+ (newFilters: Partial<T>) => {
53
+ setFilters(prevFilters => {
54
+ const updatedFilters = { ...prevFilters, ...newFilters };
55
+ isUrlSyncEnabled && updateSearchParams(updatedFilters);
56
+ return updatedFilters;
57
+ });
58
+ },
59
+ [ isUrlSyncEnabled, updateSearchParams ]
60
+ );
61
+
62
+ // helper function to reset filters
63
+ const resetFilterValues = useCallback((filters: Partial<T>): Partial<T> => Object.entries(filters).reduce((acc, [ key, value ]) => {
64
+ if (Array.isArray(value)) {
65
+ acc[key as keyof T] = [] as T[keyof T];
66
+ } else {
67
+ acc[key as keyof T] = '' as T[keyof T];
68
+ }
69
+ return acc;
70
+ }, {} as Partial<T>), []);
71
+
72
+ const onDeleteFilters = useCallback(
73
+ (filtersToDelete: Partial<T>) => {
74
+ setFilters(prevFilters => {
75
+ const updatedFilters = { ...prevFilters,...resetFilterValues(filtersToDelete) };
76
+ isUrlSyncEnabled && updateSearchParams(updatedFilters);
77
+ return updatedFilters;
78
+ });
79
+ },
80
+ [ isUrlSyncEnabled, updateSearchParams, resetFilterValues ]
81
+ );
82
+
83
+ const clearAllFilters = useCallback(() => {
84
+ const clearedFilters = resetFilterValues(filters) as T;
85
+
86
+ setFilters(clearedFilters);
87
+ isUrlSyncEnabled && updateSearchParams(clearedFilters);
88
+ }, [ filters, isUrlSyncEnabled, updateSearchParams, resetFilterValues ]);
89
+
90
+ return {
91
+ filters,
92
+ onSetFilters,
93
+ onDeleteFilters,
94
+ clearAllFilters,
95
+ };
96
+ };
@@ -1,2 +1,3 @@
1
1
  export * from './pagination';
2
2
  export * from './selection';
3
+ export * from './filters';
@@ -20,6 +20,7 @@ export interface UseDataViewPaginationProps {
20
20
  perPageParam?: string;
21
21
  }
22
22
 
23
+ /** extends UseDataViewPaginationProps */
23
24
  export interface DataViewPaginationProps extends UseDataViewPaginationProps {
24
25
  /** Current page number */
25
26
  page: number;
@@ -8,8 +8,9 @@ export interface UseDataViewSelectionProps {
8
8
  initialSelected?: (any)[];
9
9
  }
10
10
 
11
- export const useDataViewSelection = ({ matchOption, initialSelected = [] }: UseDataViewSelectionProps) => {
12
- const [ selected, setSelected ] = useState<any[]>(initialSelected);
11
+ export const useDataViewSelection = (props?: UseDataViewSelectionProps) => {
12
+ const [ selected, setSelected ] = useState<any[]>(props?.initialSelected ?? []);
13
+ const matchOption = props?.matchOption ? props.matchOption : (option, another) => (option === another);
13
14
 
14
15
  const onSelect = (isSelecting: boolean, items?: any[] | any) => {
15
16
  isSelecting && items ?
@@ -17,6 +17,7 @@ export interface InternalContextProps {
17
17
  activeState?: DataViewState | string;
18
18
  }
19
19
 
20
+ /** extends InternalContextProps */
20
21
  export interface InternalContextValue extends InternalContextProps {
21
22
  /** Flag indicating if data view is selectable (auto-calculated) */
22
23
  isSelectable: boolean;
package/src/index.ts CHANGED
@@ -7,6 +7,9 @@ export * from './Hooks';
7
7
  export { default as DataViewToolbar } from './DataViewToolbar';
8
8
  export * from './DataViewToolbar';
9
9
 
10
+ export { default as DataViewTextFilter } from './DataViewTextFilter';
11
+ export * from './DataViewTextFilter';
12
+
10
13
  export { default as DataViewTableTree } from './DataViewTableTree';
11
14
  export * from './DataViewTableTree';
12
15