@patternfly/react-data-view 7.0.0-prerelease.3 → 7.0.0-prerelease.5

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 (124) hide show
  1. package/dist/cjs/DataView/DataView.d.ts +3 -1
  2. package/dist/cjs/DataViewCheckboxFilter/DataViewCheckboxFilter.d.ts +29 -0
  3. package/dist/cjs/DataViewCheckboxFilter/DataViewCheckboxFilter.js +70 -0
  4. package/dist/cjs/DataViewCheckboxFilter/DataViewCheckboxFilter.test.d.ts +1 -0
  5. package/dist/cjs/DataViewCheckboxFilter/DataViewCheckboxFilter.test.js +25 -0
  6. package/dist/cjs/DataViewCheckboxFilter/index.d.ts +2 -0
  7. package/dist/cjs/DataViewCheckboxFilter/index.js +23 -0
  8. package/dist/cjs/DataViewFilters/DataViewFilters.d.ts +25 -0
  9. package/dist/cjs/DataViewFilters/DataViewFilters.js +85 -0
  10. package/dist/cjs/DataViewFilters/DataViewFilters.test.d.ts +1 -0
  11. package/dist/cjs/DataViewFilters/DataViewFilters.test.js +19 -0
  12. package/dist/cjs/DataViewFilters/index.d.ts +2 -0
  13. package/dist/cjs/DataViewFilters/index.js +23 -0
  14. package/dist/cjs/DataViewTable/DataViewTable.d.ts +8 -0
  15. package/dist/cjs/DataViewTableBasic/DataViewTableBasic.d.ts +1 -0
  16. package/dist/cjs/DataViewTableHead/DataViewTableHead.d.ts +1 -0
  17. package/dist/cjs/DataViewTableTree/DataViewTableTree.d.ts +1 -0
  18. package/dist/cjs/DataViewTableTree/DataViewTableTree.js +26 -14
  19. package/dist/cjs/DataViewTextFilter/DataViewTextFilter.d.ts +21 -0
  20. package/dist/cjs/DataViewTextFilter/DataViewTextFilter.js +26 -0
  21. package/dist/cjs/DataViewTextFilter/DataViewTextFilter.test.d.ts +1 -0
  22. package/dist/cjs/DataViewTextFilter/DataViewTextFilter.test.js +22 -0
  23. package/dist/cjs/DataViewTextFilter/index.d.ts +2 -0
  24. package/dist/cjs/DataViewTextFilter/index.js +23 -0
  25. package/dist/cjs/DataViewToolbar/DataViewToolbar.d.ts +10 -4
  26. package/dist/cjs/DataViewToolbar/DataViewToolbar.js +29 -6
  27. package/dist/cjs/Hooks/filters.d.ts +14 -0
  28. package/dist/cjs/Hooks/filters.js +69 -0
  29. package/dist/cjs/Hooks/filters.test.d.ts +1 -0
  30. package/dist/cjs/Hooks/filters.test.js +50 -0
  31. package/dist/cjs/Hooks/index.d.ts +2 -0
  32. package/dist/cjs/Hooks/index.js +2 -0
  33. package/dist/cjs/Hooks/pagination.d.ts +1 -0
  34. package/dist/cjs/Hooks/selection.d.ts +1 -1
  35. package/dist/cjs/Hooks/selection.js +4 -2
  36. package/dist/cjs/Hooks/sort.d.ts +32 -0
  37. package/dist/cjs/Hooks/sort.js +47 -0
  38. package/dist/cjs/Hooks/sort.test.d.ts +1 -0
  39. package/dist/cjs/Hooks/sort.test.js +68 -0
  40. package/dist/cjs/InternalContext/InternalContext.d.ts +1 -0
  41. package/dist/cjs/index.d.ts +4 -0
  42. package/dist/cjs/index.js +7 -1
  43. package/dist/dynamic/DataViewCheckboxFilter/package.json +1 -0
  44. package/dist/dynamic/DataViewFilters/package.json +1 -0
  45. package/dist/dynamic/DataViewTextFilter/package.json +1 -0
  46. package/dist/esm/DataView/DataView.d.ts +3 -1
  47. package/dist/esm/DataViewCheckboxFilter/DataViewCheckboxFilter.d.ts +29 -0
  48. package/dist/esm/DataViewCheckboxFilter/DataViewCheckboxFilter.js +62 -0
  49. package/dist/esm/DataViewCheckboxFilter/DataViewCheckboxFilter.test.d.ts +1 -0
  50. package/dist/esm/DataViewCheckboxFilter/DataViewCheckboxFilter.test.js +20 -0
  51. package/dist/esm/DataViewCheckboxFilter/index.d.ts +2 -0
  52. package/dist/esm/DataViewCheckboxFilter/index.js +2 -0
  53. package/dist/esm/DataViewFilters/DataViewFilters.d.ts +25 -0
  54. package/dist/esm/DataViewFilters/DataViewFilters.js +58 -0
  55. package/dist/esm/DataViewFilters/DataViewFilters.test.d.ts +1 -0
  56. package/dist/esm/DataViewFilters/DataViewFilters.test.js +14 -0
  57. package/dist/esm/DataViewFilters/index.d.ts +2 -0
  58. package/dist/esm/DataViewFilters/index.js +2 -0
  59. package/dist/esm/DataViewTable/DataViewTable.d.ts +8 -0
  60. package/dist/esm/DataViewTableBasic/DataViewTableBasic.d.ts +1 -0
  61. package/dist/esm/DataViewTableHead/DataViewTableHead.d.ts +1 -0
  62. package/dist/esm/DataViewTableTree/DataViewTableTree.d.ts +1 -0
  63. package/dist/esm/DataViewTableTree/DataViewTableTree.js +26 -14
  64. package/dist/esm/DataViewTextFilter/DataViewTextFilter.d.ts +21 -0
  65. package/dist/esm/DataViewTextFilter/DataViewTextFilter.js +19 -0
  66. package/dist/esm/DataViewTextFilter/DataViewTextFilter.test.d.ts +1 -0
  67. package/dist/esm/DataViewTextFilter/DataViewTextFilter.test.js +17 -0
  68. package/dist/esm/DataViewTextFilter/index.d.ts +2 -0
  69. package/dist/esm/DataViewTextFilter/index.js +2 -0
  70. package/dist/esm/DataViewToolbar/DataViewToolbar.d.ts +10 -4
  71. package/dist/esm/DataViewToolbar/DataViewToolbar.js +7 -4
  72. package/dist/esm/Hooks/filters.d.ts +14 -0
  73. package/dist/esm/Hooks/filters.js +65 -0
  74. package/dist/esm/Hooks/filters.test.d.ts +1 -0
  75. package/dist/esm/Hooks/filters.test.js +48 -0
  76. package/dist/esm/Hooks/index.d.ts +2 -0
  77. package/dist/esm/Hooks/index.js +2 -0
  78. package/dist/esm/Hooks/pagination.d.ts +1 -0
  79. package/dist/esm/Hooks/selection.d.ts +1 -1
  80. package/dist/esm/Hooks/selection.js +4 -2
  81. package/dist/esm/Hooks/sort.d.ts +32 -0
  82. package/dist/esm/Hooks/sort.js +43 -0
  83. package/dist/esm/Hooks/sort.test.d.ts +1 -0
  84. package/dist/esm/Hooks/sort.test.js +66 -0
  85. package/dist/esm/InternalContext/InternalContext.d.ts +1 -0
  86. package/dist/esm/index.d.ts +4 -0
  87. package/dist/esm/index.js +4 -0
  88. package/dist/tsconfig.tsbuildinfo +1 -1
  89. package/package.json +10 -9
  90. package/patternfly-docs/content/extensions/data-view/examples/Components/Components.md +5 -3
  91. package/patternfly-docs/content/extensions/data-view/examples/Components/DataViewTableExample.tsx +1 -1
  92. package/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsContext.md +1 -0
  93. package/patternfly-docs/content/extensions/data-view/examples/EventsContext/EventsExample.tsx +28 -6
  94. package/patternfly-docs/content/extensions/data-view/examples/Functionality/FiltersExample.tsx +107 -0
  95. package/patternfly-docs/content/extensions/data-view/examples/Functionality/Functionality.md +67 -2
  96. package/patternfly-docs/content/extensions/data-view/examples/Functionality/SortingExample.tsx +87 -0
  97. package/src/DataView/DataView.tsx +3 -2
  98. package/src/DataViewCheckboxFilter/DataViewCheckboxFilter.test.tsx +24 -0
  99. package/src/DataViewCheckboxFilter/DataViewCheckboxFilter.tsx +175 -0
  100. package/src/DataViewCheckboxFilter/__snapshots__/DataViewCheckboxFilter.test.tsx.snap +197 -0
  101. package/src/DataViewCheckboxFilter/index.ts +2 -0
  102. package/src/DataViewFilters/DataViewFilters.test.tsx +21 -0
  103. package/src/DataViewFilters/DataViewFilters.tsx +144 -0
  104. package/src/DataViewFilters/__snapshots__/DataViewFilters.test.tsx.snap +194 -0
  105. package/src/DataViewFilters/index.tsx +2 -0
  106. package/src/DataViewTable/DataViewTable.tsx +23 -3
  107. package/src/DataViewTableBasic/DataViewTableBasic.tsx +1 -0
  108. package/src/DataViewTableHead/DataViewTableHead.tsx +1 -0
  109. package/src/DataViewTableTree/DataViewTableTree.tsx +40 -18
  110. package/src/DataViewTextFilter/DataViewTextFilter.test.tsx +24 -0
  111. package/src/DataViewTextFilter/DataViewTextFilter.tsx +54 -0
  112. package/src/DataViewTextFilter/__snapshots__/DataViewTextFilter.test.tsx.snap +203 -0
  113. package/src/DataViewTextFilter/index.ts +2 -0
  114. package/src/DataViewToolbar/DataViewToolbar.tsx +47 -28
  115. package/src/DataViewToolbar/__snapshots__/DataViewToolbar.test.tsx.snap +44 -0
  116. package/src/Hooks/filters.test.tsx +62 -0
  117. package/src/Hooks/filters.ts +97 -0
  118. package/src/Hooks/index.ts +2 -0
  119. package/src/Hooks/pagination.ts +1 -0
  120. package/src/Hooks/selection.ts +3 -2
  121. package/src/Hooks/sort.test.tsx +84 -0
  122. package/src/Hooks/sort.ts +87 -0
  123. package/src/InternalContext/InternalContext.tsx +1 -0
  124. package/src/index.ts +6 -0
@@ -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 labels */
19
+ customLabelGroupContent?: 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>
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, customLabelGroupContent, 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} customLabelGroupContent={customLabelGroupContent ?? 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>
40
+ {actions}
41
+ </ToolbarItem>
42
+ )}
43
+ {filters && (
44
+ <ToolbarItem>
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
 
@@ -263,6 +263,28 @@ exports[`DataViewToolbar component should render correctly 1`] = `
263
263
  <div
264
264
  class="pf-v6-c-toolbar__group"
265
265
  />
266
+ <div
267
+ class="pf-v6-c-toolbar__group pf-m-action-group-inline"
268
+ >
269
+ <div
270
+ class="pf-v6-c-toolbar__item"
271
+ >
272
+ <button
273
+ aria-disabled="false"
274
+ class="pf-v6-c-button pf-m-link pf-m-inline"
275
+ data-ouia-component-id="DataViewToolbar-clear-all-filters"
276
+ data-ouia-component-type="PF6/Button"
277
+ data-ouia-safe="true"
278
+ type="button"
279
+ >
280
+ <span
281
+ class="pf-v6-c-button__text"
282
+ >
283
+ Clear filters
284
+ </span>
285
+ </button>
286
+ </div>
287
+ </div>
266
288
  </div>
267
289
  </div>
268
290
  </div>
@@ -526,6 +548,28 @@ exports[`DataViewToolbar component should render correctly 1`] = `
526
548
  <div
527
549
  class="pf-v6-c-toolbar__group"
528
550
  />
551
+ <div
552
+ class="pf-v6-c-toolbar__group pf-m-action-group-inline"
553
+ >
554
+ <div
555
+ class="pf-v6-c-toolbar__item"
556
+ >
557
+ <button
558
+ aria-disabled="false"
559
+ class="pf-v6-c-button pf-m-link pf-m-inline"
560
+ data-ouia-component-id="DataViewToolbar-clear-all-filters"
561
+ data-ouia-component-type="PF6/Button"
562
+ data-ouia-safe="true"
563
+ type="button"
564
+ >
565
+ <span
566
+ class="pf-v6-c-button__text"
567
+ >
568
+ Clear filters
569
+ </span>
570
+ </button>
571
+ </div>
572
+ </div>
529
573
  </div>
530
574
  </div>
531
575
  </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,97 @@
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
20
+ ? Object.keys(initialFilters).reduce((loadedFilters, key) => {
21
+ const urlValue = searchParams?.get(key);
22
+ const isArrayFilter = Array.isArray(initialFilters[key]);
23
+
24
+ // eslint-disable-next-line no-nested-ternary
25
+ loadedFilters[key] = urlValue
26
+ ? (isArrayFilter && !Array.isArray(urlValue) ? [ urlValue ] : urlValue)
27
+ : initialFilters[key];
28
+
29
+ return loadedFilters;
30
+ }, { ...initialFilters })
31
+ : initialFilters, [ isUrlSyncEnabled, initialFilters, searchParams ]);
32
+ const [ filters, setFilters ] = useState<T>(getInitialFilters());
33
+
34
+ const updateSearchParams = useCallback(
35
+ (newFilters: T) => {
36
+ if (isUrlSyncEnabled) {
37
+ const params = new URLSearchParams(searchParams);
38
+ Object.entries(newFilters).forEach(([ key, value ]) => {
39
+ params.delete(key);
40
+ (Array.isArray(value) ? value : [ value ]).forEach((val) => value && params.append(key, val));
41
+ });
42
+ setSearchParams?.(params);
43
+ }
44
+ },
45
+ [ isUrlSyncEnabled, searchParams, setSearchParams ]
46
+ );
47
+
48
+ useEffect(() => {
49
+ isUrlSyncEnabled && setFilters(getInitialFilters())
50
+ }, []); // eslint-disable-line react-hooks/exhaustive-deps
51
+
52
+ const onSetFilters = useCallback(
53
+ (newFilters: Partial<T>) => {
54
+ setFilters(prevFilters => {
55
+ const updatedFilters = { ...prevFilters, ...newFilters };
56
+ isUrlSyncEnabled && updateSearchParams(updatedFilters);
57
+ return updatedFilters;
58
+ });
59
+ },
60
+ [ isUrlSyncEnabled, updateSearchParams ]
61
+ );
62
+
63
+ // helper function to reset filters
64
+ const resetFilterValues = useCallback((filters: Partial<T>): Partial<T> => Object.entries(filters).reduce((acc, [ key, value ]) => {
65
+ if (Array.isArray(value)) {
66
+ acc[key as keyof T] = [] as T[keyof T];
67
+ } else {
68
+ acc[key as keyof T] = '' as T[keyof T];
69
+ }
70
+ return acc;
71
+ }, {} as Partial<T>), []);
72
+
73
+ const onDeleteFilters = useCallback(
74
+ (filtersToDelete: Partial<T>) => {
75
+ setFilters(prevFilters => {
76
+ const updatedFilters = { ...prevFilters,...resetFilterValues(filtersToDelete) };
77
+ isUrlSyncEnabled && updateSearchParams(updatedFilters);
78
+ return updatedFilters;
79
+ });
80
+ },
81
+ [ isUrlSyncEnabled, updateSearchParams, resetFilterValues ]
82
+ );
83
+
84
+ const clearAllFilters = useCallback(() => {
85
+ const clearedFilters = resetFilterValues(filters) as T;
86
+
87
+ setFilters(clearedFilters);
88
+ isUrlSyncEnabled && updateSearchParams(clearedFilters);
89
+ }, [ filters, isUrlSyncEnabled, updateSearchParams, resetFilterValues ]);
90
+
91
+ return {
92
+ filters,
93
+ onSetFilters,
94
+ onDeleteFilters,
95
+ clearAllFilters,
96
+ };
97
+ };
@@ -1,2 +1,4 @@
1
1
  export * from './pagination';
2
2
  export * from './selection';
3
+ export * from './filters';
4
+ export * from './sort';
@@ -15,6 +15,7 @@ export interface UseDataViewPaginationProps {
15
15
  perPageParam?: string;
16
16
  }
17
17
 
18
+ /** extends UseDataViewPaginationProps */
18
19
  export interface DataViewPaginationProps extends UseDataViewPaginationProps {
19
20
  /** Current page number */
20
21
  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 ?
@@ -0,0 +1,84 @@
1
+ import '@testing-library/jest-dom';
2
+ import { renderHook, act } from '@testing-library/react';
3
+ import { useDataViewSort, UseDataViewSortProps, DataViewSortConfig, DataViewSortParams } from './sort';
4
+
5
+ describe('useDataViewSort', () => {
6
+ const initialSort: DataViewSortConfig = { sortBy: 'name', direction: 'asc' };
7
+
8
+ it('should initialize with provided initial sort config', () => {
9
+ const { result } = renderHook(() => useDataViewSort({ initialSort }));
10
+ expect(result.current).toEqual(expect.objectContaining(initialSort));
11
+ });
12
+
13
+ it('should initialize with empty sort config if no initialSort is provided', () => {
14
+ const { result } = renderHook(() => useDataViewSort());
15
+ expect(result.current).toEqual(expect.objectContaining({ sortBy: undefined, direction: 'asc' }));
16
+ });
17
+
18
+ it('should update sort state when onSort is called', () => {
19
+ const { result } = renderHook(() => useDataViewSort({ initialSort }));
20
+ act(() => {
21
+ result.current.onSort(undefined, 'age', 'desc');
22
+ });
23
+ expect(result.current).toEqual(expect.objectContaining({ sortBy: 'age', direction: 'desc' }));
24
+ });
25
+
26
+ it('should sync with URL search params if isUrlSyncEnabled', () => {
27
+ const searchParams = new URLSearchParams();
28
+ const setSearchParams = jest.fn();
29
+ const props: UseDataViewSortProps = {
30
+ initialSort,
31
+ searchParams,
32
+ setSearchParams,
33
+ };
34
+
35
+ const { result } = renderHook(() => useDataViewSort(props));
36
+
37
+ expect(setSearchParams).toHaveBeenCalledTimes(1);
38
+ expect(result.current).toEqual(expect.objectContaining(initialSort));
39
+ });
40
+
41
+ it('should validate direction and fallback to default direction if invalid direction is provided', () => {
42
+ const searchParams = new URLSearchParams();
43
+ searchParams.set(DataViewSortParams.SORT_BY, 'name');
44
+ searchParams.set(DataViewSortParams.DIRECTION, 'invalid-direction');
45
+ const { result } = renderHook(() => useDataViewSort({ searchParams, defaultDirection: 'desc' }));
46
+
47
+ expect(result.current).toEqual(expect.objectContaining({ sortBy: 'name', direction: 'desc' }));
48
+ });
49
+
50
+ it('should update search params when URL sync is enabled and sort changes', () => {
51
+ const searchParams = new URLSearchParams();
52
+ const setSearchParams = jest.fn();
53
+ const props: UseDataViewSortProps = {
54
+ initialSort,
55
+ searchParams,
56
+ setSearchParams,
57
+ };
58
+
59
+ const { result } = renderHook(() => useDataViewSort(props));
60
+ act(() => {
61
+ expect(setSearchParams).toHaveBeenCalledTimes(1);
62
+ result.current.onSort(undefined, 'priority', 'desc');
63
+ });
64
+
65
+ expect(setSearchParams).toHaveBeenCalledTimes(2);
66
+ expect(result.current).toEqual(expect.objectContaining({ sortBy: 'priority', direction: 'desc' }));
67
+ });
68
+
69
+ it('should prioritize searchParams values', () => {
70
+ const searchParams = new URLSearchParams();
71
+ searchParams.set(DataViewSortParams.SORT_BY, 'category');
72
+ searchParams.set(DataViewSortParams.DIRECTION, 'desc');
73
+
74
+ const { result } = renderHook(
75
+ (props: UseDataViewSortProps) => useDataViewSort(props),
76
+ { initialProps: { initialSort, searchParams } }
77
+ );
78
+
79
+ expect(result.current).toEqual(expect.objectContaining({
80
+ sortBy: 'category',
81
+ direction: 'desc',
82
+ }));
83
+ });
84
+ });
@@ -0,0 +1,87 @@
1
+ import { ISortBy } from "@patternfly/react-table";
2
+ import { useState, useEffect, useMemo } from "react";
3
+
4
+ export enum DataViewSortParams {
5
+ SORT_BY = 'sortBy',
6
+ DIRECTION = 'direction'
7
+ };
8
+
9
+ const validateDirection = (direction: string | null | undefined, defaultDirection: ISortBy['direction']): ISortBy['direction'] => (
10
+ direction === 'asc' || direction === 'desc' ? direction : defaultDirection
11
+ );
12
+
13
+ export interface DataViewSortConfig {
14
+ /** Attribute to sort the entries by */
15
+ sortBy: string | undefined;
16
+ /** Sort direction */
17
+ direction: ISortBy['direction'];
18
+ };
19
+
20
+ export interface UseDataViewSortProps {
21
+ /** Initial sort config */
22
+ initialSort?: DataViewSortConfig;
23
+ /** Current search parameters as a string */
24
+ searchParams?: URLSearchParams;
25
+ /** Function to set search parameters */
26
+ setSearchParams?: (params: URLSearchParams) => void;
27
+ /** Default direction */
28
+ defaultDirection?: ISortBy['direction'];
29
+ /** Sort by URL param name */
30
+ sortByParam?: string;
31
+ /** Direction URL param name */
32
+ directionParam?: string;
33
+ };
34
+
35
+ export const useDataViewSort = (props?: UseDataViewSortProps) => {
36
+ const {
37
+ initialSort,
38
+ searchParams,
39
+ setSearchParams,
40
+ defaultDirection = 'asc',
41
+ sortByParam = DataViewSortParams.SORT_BY,
42
+ directionParam = DataViewSortParams.DIRECTION
43
+ } = props ?? {};
44
+
45
+ const isUrlSyncEnabled = useMemo(() => searchParams && !!setSearchParams, [ searchParams, setSearchParams ]);
46
+
47
+ const [ state, setState ] = useState<DataViewSortConfig>({
48
+ sortBy: searchParams?.get(sortByParam) ?? initialSort?.sortBy,
49
+ direction: validateDirection(searchParams?.get(directionParam) as ISortBy['direction'], initialSort?.direction),
50
+ });
51
+
52
+ const updateSearchParams = (sortBy: string, direction: ISortBy['direction']) => {
53
+ if (isUrlSyncEnabled && sortBy) {
54
+ const params = new URLSearchParams(searchParams);
55
+ params.set(sortByParam, `${sortBy}`);
56
+ params.set(directionParam, `${direction}`);
57
+ setSearchParams?.(params);
58
+ }
59
+ };
60
+
61
+ useEffect(() => {
62
+ state.sortBy && state.direction && updateSearchParams(state.sortBy, state.direction);
63
+ // eslint-disable-next-line react-hooks/exhaustive-deps
64
+ }, []);
65
+
66
+ useEffect(() => {
67
+ const currentSortBy = searchParams?.get(sortByParam) || state.sortBy;
68
+ const currentDirection = searchParams?.get(directionParam) as ISortBy['direction'] || state.direction;
69
+ const validDirection = validateDirection(currentDirection, defaultDirection);
70
+ currentSortBy !== state.sortBy || validDirection !== state.direction && setState({ sortBy: currentSortBy, direction: validDirection });
71
+ // eslint-disable-next-line react-hooks/exhaustive-deps
72
+ }, [ searchParams?.toString() ]);
73
+
74
+ const onSort = (
75
+ _event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined,
76
+ newSortBy: string,
77
+ newSortDirection: ISortBy['direction']
78
+ ) => {
79
+ setState({ sortBy: newSortBy, direction: newSortDirection });
80
+ updateSearchParams(newSortBy, newSortDirection);
81
+ };
82
+
83
+ return {
84
+ ...state,
85
+ onSort
86
+ };
87
+ };
@@ -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
 
@@ -22,5 +25,8 @@ export * from './DataViewTable';
22
25
  export { default as DataViewEventsContext } from './DataViewEventsContext';
23
26
  export * from './DataViewEventsContext';
24
27
 
28
+ export { default as DataViewCheckboxFilter } from './DataViewCheckboxFilter';
29
+ export * from './DataViewCheckboxFilter';
30
+
25
31
  export { default as DataView } from './DataView';
26
32
  export * from './DataView';