@patternfly/react-data-view 7.0.0-prerelease.2 → 7.0.0-prerelease.4

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 +9 -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,4 +1,27 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
2
25
  var __rest = (this && this.__rest) || function (s, e) {
3
26
  var t = {};
4
27
  for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
@@ -10,19 +33,19 @@ var __rest = (this && this.__rest) || function (s, e) {
10
33
  }
11
34
  return t;
12
35
  };
13
- var __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
36
  Object.defineProperty(exports, "__esModule", { value: true });
17
37
  exports.DataViewToolbar = void 0;
18
- const react_1 = __importDefault(require("react"));
38
+ const react_1 = __importStar(require("react"));
19
39
  const react_core_1 = require("@patternfly/react-core");
20
40
  const DataViewToolbar = (_a) => {
21
- var { className, ouiaId = 'DataViewToolbar', bulkSelect, actions, pagination, children } = _a, props = __rest(_a, ["className", "ouiaId", "bulkSelect", "actions", "pagination", "children"]);
22
- return (react_1.default.createElement(react_core_1.Toolbar, Object.assign({ ouiaId: ouiaId, className: className }, props),
41
+ var { className, ouiaId = 'DataViewToolbar', bulkSelect, actions, pagination, filters, customLabelGroupContent, clearAllFilters, children } = _a, props = __rest(_a, ["className", "ouiaId", "bulkSelect", "actions", "pagination", "filters", "customLabelGroupContent", "clearAllFilters", "children"]);
42
+ const defaultClearFilters = (0, react_1.useRef)(react_1.default.createElement(react_core_1.ToolbarItem, null,
43
+ react_1.default.createElement(react_core_1.Button, { ouiaId: `${ouiaId}-clear-all-filters`, variant: "link", onClick: clearAllFilters, isInline: true }, "Clear filters")));
44
+ return (react_1.default.createElement(react_core_1.Toolbar, Object.assign({ ouiaId: ouiaId, className: className, customLabelGroupContent: customLabelGroupContent !== null && customLabelGroupContent !== void 0 ? customLabelGroupContent : defaultClearFilters.current }, props),
23
45
  react_1.default.createElement(react_core_1.ToolbarContent, null,
24
46
  bulkSelect && (react_1.default.createElement(react_core_1.ToolbarItem, { "data-ouia-component-id": `${ouiaId}-bulk-select` }, bulkSelect)),
25
47
  actions && (react_1.default.createElement(react_core_1.ToolbarItem, null, actions)),
48
+ filters && (react_1.default.createElement(react_core_1.ToolbarItem, null, filters)),
26
49
  pagination && (react_1.default.createElement(react_core_1.ToolbarItem, { variant: react_core_1.ToolbarItemVariant.pagination, "data-ouia-component-id": `${ouiaId}-pagination` }, pagination)),
27
50
  children)));
28
51
  };
@@ -0,0 +1,14 @@
1
+ export interface UseDataViewFiltersProps<T extends object> {
2
+ /** Initial filters object */
3
+ initialFilters?: T;
4
+ /** Current search parameters as a string */
5
+ searchParams?: URLSearchParams;
6
+ /** Function to set search parameters */
7
+ setSearchParams?: (params: URLSearchParams) => void;
8
+ }
9
+ export declare const useDataViewFilters: <T extends object>({ initialFilters, searchParams, setSearchParams, }: UseDataViewFiltersProps<T>) => {
10
+ filters: T;
11
+ onSetFilters: (newFilters: Partial<T>) => void;
12
+ onDeleteFilters: (filtersToDelete: Partial<T>) => void;
13
+ clearAllFilters: () => void;
14
+ };
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useDataViewFilters = void 0;
4
+ const react_1 = require("react");
5
+ ;
6
+ const useDataViewFilters = ({ initialFilters = {}, searchParams, setSearchParams, }) => {
7
+ const isUrlSyncEnabled = (0, react_1.useMemo)(() => searchParams && !!setSearchParams, [searchParams, setSearchParams]);
8
+ const getInitialFilters = (0, react_1.useCallback)(() => isUrlSyncEnabled
9
+ ? Object.keys(initialFilters).reduce((loadedFilters, key) => {
10
+ const urlValue = searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(key);
11
+ const isArrayFilter = Array.isArray(initialFilters[key]);
12
+ // eslint-disable-next-line no-nested-ternary
13
+ loadedFilters[key] = urlValue
14
+ ? (isArrayFilter && !Array.isArray(urlValue) ? [urlValue] : urlValue)
15
+ : initialFilters[key];
16
+ return loadedFilters;
17
+ }, Object.assign({}, initialFilters))
18
+ : initialFilters, [isUrlSyncEnabled, initialFilters, searchParams]);
19
+ const [filters, setFilters] = (0, react_1.useState)(getInitialFilters());
20
+ const updateSearchParams = (0, react_1.useCallback)((newFilters) => {
21
+ if (isUrlSyncEnabled) {
22
+ const params = new URLSearchParams(searchParams);
23
+ Object.entries(newFilters).forEach(([key, value]) => {
24
+ params.delete(key);
25
+ (Array.isArray(value) ? value : [value]).forEach((val) => value && params.append(key, val));
26
+ });
27
+ setSearchParams === null || setSearchParams === void 0 ? void 0 : setSearchParams(params);
28
+ }
29
+ }, [isUrlSyncEnabled, searchParams, setSearchParams]);
30
+ (0, react_1.useEffect)(() => {
31
+ isUrlSyncEnabled && setFilters(getInitialFilters());
32
+ }, []); // eslint-disable-line react-hooks/exhaustive-deps
33
+ const onSetFilters = (0, react_1.useCallback)((newFilters) => {
34
+ setFilters(prevFilters => {
35
+ const updatedFilters = Object.assign(Object.assign({}, prevFilters), newFilters);
36
+ isUrlSyncEnabled && updateSearchParams(updatedFilters);
37
+ return updatedFilters;
38
+ });
39
+ }, [isUrlSyncEnabled, updateSearchParams]);
40
+ // helper function to reset filters
41
+ const resetFilterValues = (0, react_1.useCallback)((filters) => Object.entries(filters).reduce((acc, [key, value]) => {
42
+ if (Array.isArray(value)) {
43
+ acc[key] = [];
44
+ }
45
+ else {
46
+ acc[key] = '';
47
+ }
48
+ return acc;
49
+ }, {}), []);
50
+ const onDeleteFilters = (0, react_1.useCallback)((filtersToDelete) => {
51
+ setFilters(prevFilters => {
52
+ const updatedFilters = Object.assign(Object.assign({}, prevFilters), resetFilterValues(filtersToDelete));
53
+ isUrlSyncEnabled && updateSearchParams(updatedFilters);
54
+ return updatedFilters;
55
+ });
56
+ }, [isUrlSyncEnabled, updateSearchParams, resetFilterValues]);
57
+ const clearAllFilters = (0, react_1.useCallback)(() => {
58
+ const clearedFilters = resetFilterValues(filters);
59
+ setFilters(clearedFilters);
60
+ isUrlSyncEnabled && updateSearchParams(clearedFilters);
61
+ }, [filters, isUrlSyncEnabled, updateSearchParams, resetFilterValues]);
62
+ return {
63
+ filters,
64
+ onSetFilters,
65
+ onDeleteFilters,
66
+ clearAllFilters,
67
+ };
68
+ };
69
+ exports.useDataViewFilters = useDataViewFilters;
@@ -0,0 +1 @@
1
+ import '@testing-library/jest-dom';
@@ -0,0 +1,50 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ require("@testing-library/jest-dom");
4
+ const react_1 = require("@testing-library/react");
5
+ const filters_1 = require("./filters");
6
+ describe('useDataViewFilters', () => {
7
+ const initialFilters = { search: 'test', tags: ['tag1', 'tag2'] };
8
+ it('should initialize with provided initial filters', () => {
9
+ const { result } = (0, react_1.renderHook)(() => (0, filters_1.useDataViewFilters)({ initialFilters }));
10
+ expect(result.current.filters).toEqual(initialFilters);
11
+ });
12
+ it('should initialize with empty filters if no initialFilters provided', () => {
13
+ const { result } = (0, react_1.renderHook)(() => (0, filters_1.useDataViewFilters)({}));
14
+ expect(result.current.filters).toEqual({});
15
+ });
16
+ it('should set filters correctly', () => {
17
+ const { result } = (0, react_1.renderHook)(() => (0, filters_1.useDataViewFilters)({ initialFilters }));
18
+ const newFilters = { search: 'new search' };
19
+ (0, react_1.act)(() => result.current.onSetFilters(newFilters));
20
+ expect(result.current.filters).toEqual(Object.assign(Object.assign({}, initialFilters), newFilters));
21
+ });
22
+ it('should delete specific filters without removing keys', () => {
23
+ const { result } = (0, react_1.renderHook)(() => (0, filters_1.useDataViewFilters)({ initialFilters }));
24
+ const filtersToDelete = { search: 'test' };
25
+ (0, react_1.act)(() => result.current.onDeleteFilters(filtersToDelete));
26
+ expect(result.current.filters).toEqual({ search: '', tags: ['tag1', 'tag2'] });
27
+ });
28
+ it('should clear all filters', () => {
29
+ const { result } = (0, react_1.renderHook)(() => (0, filters_1.useDataViewFilters)({ initialFilters }));
30
+ (0, react_1.act)(() => result.current.clearAllFilters());
31
+ expect(result.current.filters).toEqual({ search: '', tags: [] });
32
+ });
33
+ it('should sync with URL search params if isUrlSyncEnabled', () => {
34
+ const searchParams = new URLSearchParams();
35
+ const setSearchParams = jest.fn();
36
+ const props = {
37
+ initialFilters,
38
+ searchParams,
39
+ setSearchParams,
40
+ };
41
+ const { result } = (0, react_1.renderHook)(() => (0, filters_1.useDataViewFilters)(props));
42
+ (0, react_1.act)(() => result.current.onSetFilters({ search: 'new search' }));
43
+ expect(setSearchParams).toHaveBeenCalled();
44
+ });
45
+ it('should reset filters to default values when clearAllFilters is called', () => {
46
+ const { result } = (0, react_1.renderHook)(() => (0, filters_1.useDataViewFilters)({ initialFilters }));
47
+ (0, react_1.act)(() => result.current.clearAllFilters());
48
+ expect(result.current.filters).toEqual({ search: '', tags: [] });
49
+ });
50
+ });
@@ -1,2 +1,4 @@
1
1
  export * from './pagination';
2
2
  export * from './selection';
3
+ export * from './filters';
4
+ export * from './sort';
@@ -16,3 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./pagination"), exports);
18
18
  __exportStar(require("./selection"), exports);
19
+ __exportStar(require("./filters"), exports);
20
+ __exportStar(require("./sort"), exports);
@@ -12,6 +12,7 @@ export interface UseDataViewPaginationProps {
12
12
  /** Custom URL parameter name for per page */
13
13
  perPageParam?: string;
14
14
  }
15
+ /** extends UseDataViewPaginationProps */
15
16
  export interface DataViewPaginationProps extends UseDataViewPaginationProps {
16
17
  /** Current page number */
17
18
  page: number;
@@ -4,7 +4,7 @@ export interface UseDataViewSelectionProps {
4
4
  /** Array of initially selected entries */
5
5
  initialSelected?: (any)[];
6
6
  }
7
- export declare const useDataViewSelection: ({ matchOption, initialSelected }: UseDataViewSelectionProps) => {
7
+ export declare const useDataViewSelection: (props?: UseDataViewSelectionProps) => {
8
8
  selected: any[];
9
9
  onSelect: (isSelecting: boolean, items?: any[] | any) => void;
10
10
  isSelected: (item: any) => boolean;
@@ -3,8 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useDataViewSelection = void 0;
4
4
  /* eslint-disable @typescript-eslint/no-explicit-any */
5
5
  const react_1 = require("react");
6
- const useDataViewSelection = ({ matchOption, initialSelected = [] }) => {
7
- const [selected, setSelected] = (0, react_1.useState)(initialSelected);
6
+ const useDataViewSelection = (props) => {
7
+ var _a;
8
+ const [selected, setSelected] = (0, react_1.useState)((_a = props === null || props === void 0 ? void 0 : props.initialSelected) !== null && _a !== void 0 ? _a : []);
9
+ const matchOption = (props === null || props === void 0 ? void 0 : props.matchOption) ? props.matchOption : (option, another) => (option === another);
8
10
  const onSelect = (isSelecting, items) => {
9
11
  isSelecting && items ?
10
12
  setSelected(prev => {
@@ -0,0 +1,32 @@
1
+ import { ISortBy } from "@patternfly/react-table";
2
+ export declare enum DataViewSortParams {
3
+ SORT_BY = "sortBy",
4
+ DIRECTION = "direction"
5
+ }
6
+ export interface DataViewSortConfig {
7
+ /** Attribute to sort the entries by */
8
+ sortBy: string | undefined;
9
+ /** Sort direction */
10
+ direction: ISortBy['direction'];
11
+ }
12
+ export interface UseDataViewSortProps {
13
+ /** Initial sort config */
14
+ initialSort?: DataViewSortConfig;
15
+ /** Current search parameters as a string */
16
+ searchParams?: URLSearchParams;
17
+ /** Function to set search parameters */
18
+ setSearchParams?: (params: URLSearchParams) => void;
19
+ /** Default direction */
20
+ defaultDirection?: ISortBy['direction'];
21
+ /** Sort by URL param name */
22
+ sortByParam?: string;
23
+ /** Direction URL param name */
24
+ directionParam?: string;
25
+ }
26
+ export declare const useDataViewSort: (props?: UseDataViewSortProps) => {
27
+ onSort: (_event: React.MouseEvent | React.KeyboardEvent | MouseEvent | undefined, newSortBy: string, newSortDirection: ISortBy["direction"]) => void;
28
+ /** Attribute to sort the entries by */
29
+ sortBy: string | undefined;
30
+ /** Sort direction */
31
+ direction: ISortBy["direction"];
32
+ };
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useDataViewSort = exports.DataViewSortParams = void 0;
4
+ const react_1 = require("react");
5
+ var DataViewSortParams;
6
+ (function (DataViewSortParams) {
7
+ DataViewSortParams["SORT_BY"] = "sortBy";
8
+ DataViewSortParams["DIRECTION"] = "direction";
9
+ })(DataViewSortParams || (exports.DataViewSortParams = DataViewSortParams = {}));
10
+ ;
11
+ const validateDirection = (direction, defaultDirection) => (direction === 'asc' || direction === 'desc' ? direction : defaultDirection);
12
+ ;
13
+ ;
14
+ const useDataViewSort = (props) => {
15
+ var _a;
16
+ const { initialSort, searchParams, setSearchParams, defaultDirection = 'asc', sortByParam = DataViewSortParams.SORT_BY, directionParam = DataViewSortParams.DIRECTION } = props !== null && props !== void 0 ? props : {};
17
+ const isUrlSyncEnabled = (0, react_1.useMemo)(() => searchParams && !!setSearchParams, [searchParams, setSearchParams]);
18
+ const [state, setState] = (0, react_1.useState)({
19
+ sortBy: (_a = searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(sortByParam)) !== null && _a !== void 0 ? _a : initialSort === null || initialSort === void 0 ? void 0 : initialSort.sortBy,
20
+ direction: validateDirection(searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(directionParam), initialSort === null || initialSort === void 0 ? void 0 : initialSort.direction),
21
+ });
22
+ const updateSearchParams = (sortBy, direction) => {
23
+ if (isUrlSyncEnabled && sortBy) {
24
+ const params = new URLSearchParams(searchParams);
25
+ params.set(sortByParam, `${sortBy}`);
26
+ params.set(directionParam, `${direction}`);
27
+ setSearchParams === null || setSearchParams === void 0 ? void 0 : setSearchParams(params);
28
+ }
29
+ };
30
+ (0, react_1.useEffect)(() => {
31
+ state.sortBy && state.direction && updateSearchParams(state.sortBy, state.direction);
32
+ // eslint-disable-next-line react-hooks/exhaustive-deps
33
+ }, []);
34
+ (0, react_1.useEffect)(() => {
35
+ const currentSortBy = (searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(sortByParam)) || state.sortBy;
36
+ const currentDirection = (searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(directionParam)) || state.direction;
37
+ const validDirection = validateDirection(currentDirection, defaultDirection);
38
+ currentSortBy !== state.sortBy || validDirection !== state.direction && setState({ sortBy: currentSortBy, direction: validDirection });
39
+ // eslint-disable-next-line react-hooks/exhaustive-deps
40
+ }, [searchParams === null || searchParams === void 0 ? void 0 : searchParams.toString()]);
41
+ const onSort = (_event, newSortBy, newSortDirection) => {
42
+ setState({ sortBy: newSortBy, direction: newSortDirection });
43
+ updateSearchParams(newSortBy, newSortDirection);
44
+ };
45
+ return Object.assign(Object.assign({}, state), { onSort });
46
+ };
47
+ exports.useDataViewSort = useDataViewSort;
@@ -0,0 +1 @@
1
+ import '@testing-library/jest-dom';
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ require("@testing-library/jest-dom");
4
+ const react_1 = require("@testing-library/react");
5
+ const sort_1 = require("./sort");
6
+ describe('useDataViewSort', () => {
7
+ const initialSort = { sortBy: 'name', direction: 'asc' };
8
+ it('should initialize with provided initial sort config', () => {
9
+ const { result } = (0, react_1.renderHook)(() => (0, sort_1.useDataViewSort)({ initialSort }));
10
+ expect(result.current).toEqual(expect.objectContaining(initialSort));
11
+ });
12
+ it('should initialize with empty sort config if no initialSort is provided', () => {
13
+ const { result } = (0, react_1.renderHook)(() => (0, sort_1.useDataViewSort)());
14
+ expect(result.current).toEqual(expect.objectContaining({ sortBy: undefined, direction: 'asc' }));
15
+ });
16
+ it('should update sort state when onSort is called', () => {
17
+ const { result } = (0, react_1.renderHook)(() => (0, sort_1.useDataViewSort)({ initialSort }));
18
+ (0, react_1.act)(() => {
19
+ result.current.onSort(undefined, 'age', 'desc');
20
+ });
21
+ expect(result.current).toEqual(expect.objectContaining({ sortBy: 'age', direction: 'desc' }));
22
+ });
23
+ it('should sync with URL search params if isUrlSyncEnabled', () => {
24
+ const searchParams = new URLSearchParams();
25
+ const setSearchParams = jest.fn();
26
+ const props = {
27
+ initialSort,
28
+ searchParams,
29
+ setSearchParams,
30
+ };
31
+ const { result } = (0, react_1.renderHook)(() => (0, sort_1.useDataViewSort)(props));
32
+ expect(setSearchParams).toHaveBeenCalledTimes(1);
33
+ expect(result.current).toEqual(expect.objectContaining(initialSort));
34
+ });
35
+ it('should validate direction and fallback to default direction if invalid direction is provided', () => {
36
+ const searchParams = new URLSearchParams();
37
+ searchParams.set(sort_1.DataViewSortParams.SORT_BY, 'name');
38
+ searchParams.set(sort_1.DataViewSortParams.DIRECTION, 'invalid-direction');
39
+ const { result } = (0, react_1.renderHook)(() => (0, sort_1.useDataViewSort)({ searchParams, defaultDirection: 'desc' }));
40
+ expect(result.current).toEqual(expect.objectContaining({ sortBy: 'name', direction: 'desc' }));
41
+ });
42
+ it('should update search params when URL sync is enabled and sort changes', () => {
43
+ const searchParams = new URLSearchParams();
44
+ const setSearchParams = jest.fn();
45
+ const props = {
46
+ initialSort,
47
+ searchParams,
48
+ setSearchParams,
49
+ };
50
+ const { result } = (0, react_1.renderHook)(() => (0, sort_1.useDataViewSort)(props));
51
+ (0, react_1.act)(() => {
52
+ expect(setSearchParams).toHaveBeenCalledTimes(1);
53
+ result.current.onSort(undefined, 'priority', 'desc');
54
+ });
55
+ expect(setSearchParams).toHaveBeenCalledTimes(2);
56
+ expect(result.current).toEqual(expect.objectContaining({ sortBy: 'priority', direction: 'desc' }));
57
+ });
58
+ it('should prioritize searchParams values', () => {
59
+ const searchParams = new URLSearchParams();
60
+ searchParams.set(sort_1.DataViewSortParams.SORT_BY, 'category');
61
+ searchParams.set(sort_1.DataViewSortParams.DIRECTION, 'desc');
62
+ const { result } = (0, react_1.renderHook)((props) => (0, sort_1.useDataViewSort)(props), { initialProps: { initialSort, searchParams } });
63
+ expect(result.current).toEqual(expect.objectContaining({
64
+ sortBy: 'category',
65
+ direction: 'desc',
66
+ }));
67
+ });
68
+ });
@@ -14,6 +14,7 @@ export interface InternalContextProps {
14
14
  /** Currently active state */
15
15
  activeState?: DataViewState | string;
16
16
  }
17
+ /** extends InternalContextProps */
17
18
  export interface InternalContextValue extends InternalContextProps {
18
19
  /** Flag indicating if data view is selectable (auto-calculated) */
19
20
  isSelectable: boolean;
@@ -3,6 +3,8 @@ export * from './InternalContext';
3
3
  export * from './Hooks';
4
4
  export { default as DataViewToolbar } from './DataViewToolbar';
5
5
  export * from './DataViewToolbar';
6
+ export { default as DataViewTextFilter } from './DataViewTextFilter';
7
+ export * from './DataViewTextFilter';
6
8
  export { default as DataViewTableTree } from './DataViewTableTree';
7
9
  export * from './DataViewTableTree';
8
10
  export { default as DataViewTableHead } from './DataViewTableHead';
@@ -13,5 +15,7 @@ export { default as DataViewTable } from './DataViewTable';
13
15
  export * from './DataViewTable';
14
16
  export { default as DataViewEventsContext } from './DataViewEventsContext';
15
17
  export * from './DataViewEventsContext';
18
+ export { default as DataViewCheckboxFilter } from './DataViewCheckboxFilter';
19
+ export * from './DataViewCheckboxFilter';
16
20
  export { default as DataView } from './DataView';
17
21
  export * from './DataView';
package/dist/cjs/index.js CHANGED
@@ -18,7 +18,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
18
18
  return (mod && mod.__esModule) ? mod : { "default": mod };
19
19
  };
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.DataView = exports.DataViewEventsContext = exports.DataViewTable = exports.DataViewTableBasic = exports.DataViewTableHead = exports.DataViewTableTree = exports.DataViewToolbar = exports.InternalContext = void 0;
21
+ exports.DataView = exports.DataViewCheckboxFilter = exports.DataViewEventsContext = exports.DataViewTable = exports.DataViewTableBasic = exports.DataViewTableHead = exports.DataViewTableTree = exports.DataViewTextFilter = exports.DataViewToolbar = exports.InternalContext = void 0;
22
22
  var InternalContext_1 = require("./InternalContext");
23
23
  Object.defineProperty(exports, "InternalContext", { enumerable: true, get: function () { return __importDefault(InternalContext_1).default; } });
24
24
  __exportStar(require("./InternalContext"), exports);
@@ -26,6 +26,9 @@ __exportStar(require("./Hooks"), exports);
26
26
  var DataViewToolbar_1 = require("./DataViewToolbar");
27
27
  Object.defineProperty(exports, "DataViewToolbar", { enumerable: true, get: function () { return __importDefault(DataViewToolbar_1).default; } });
28
28
  __exportStar(require("./DataViewToolbar"), exports);
29
+ var DataViewTextFilter_1 = require("./DataViewTextFilter");
30
+ Object.defineProperty(exports, "DataViewTextFilter", { enumerable: true, get: function () { return __importDefault(DataViewTextFilter_1).default; } });
31
+ __exportStar(require("./DataViewTextFilter"), exports);
29
32
  var DataViewTableTree_1 = require("./DataViewTableTree");
30
33
  Object.defineProperty(exports, "DataViewTableTree", { enumerable: true, get: function () { return __importDefault(DataViewTableTree_1).default; } });
31
34
  __exportStar(require("./DataViewTableTree"), exports);
@@ -41,6 +44,9 @@ __exportStar(require("./DataViewTable"), exports);
41
44
  var DataViewEventsContext_1 = require("./DataViewEventsContext");
42
45
  Object.defineProperty(exports, "DataViewEventsContext", { enumerable: true, get: function () { return __importDefault(DataViewEventsContext_1).default; } });
43
46
  __exportStar(require("./DataViewEventsContext"), exports);
47
+ var DataViewCheckboxFilter_1 = require("./DataViewCheckboxFilter");
48
+ Object.defineProperty(exports, "DataViewCheckboxFilter", { enumerable: true, get: function () { return __importDefault(DataViewCheckboxFilter_1).default; } });
49
+ __exportStar(require("./DataViewCheckboxFilter"), exports);
44
50
  var DataView_1 = require("./DataView");
45
51
  Object.defineProperty(exports, "DataView", { enumerable: true, get: function () { return __importDefault(DataView_1).default; } });
46
52
  __exportStar(require("./DataView"), exports);
@@ -0,0 +1 @@
1
+ {"main":"../../cjs/DataViewCheckboxFilter/index.js","module":"../../esm/DataViewCheckboxFilter/index.js","typings":"../../esm/DataViewCheckboxFilter/index.d.ts"}
@@ -0,0 +1 @@
1
+ {"main":"../../cjs/DataViewFilters/index.js","module":"../../esm/DataViewFilters/index.js","typings":"../../esm/DataViewFilters/index.d.ts"}
@@ -0,0 +1 @@
1
+ {"main":"../../cjs/DataViewTextFilter/index.js","module":"../../esm/DataViewTextFilter/index.js","typings":"../../esm/DataViewTextFilter/index.d.ts"}
@@ -1,4 +1,5 @@
1
1
  import React from 'react';
2
+ import { StackProps } from '@patternfly/react-core';
2
3
  import { DataViewSelection } from '../InternalContext';
3
4
  export declare const DataViewState: {
4
5
  readonly empty: "empty";
@@ -6,7 +7,8 @@ export declare const DataViewState: {
6
7
  readonly error: "error";
7
8
  };
8
9
  export type DataViewState = typeof DataViewState[keyof typeof DataViewState];
9
- export interface DataViewProps {
10
+ /** extends StackProps */
11
+ export interface DataViewProps extends StackProps {
10
12
  /** Content rendered inside the data view */
11
13
  children: React.ReactNode;
12
14
  /** Custom OUIA ID */
@@ -0,0 +1,29 @@
1
+ import React from 'react';
2
+ import { MenuProps } from '@patternfly/react-core';
3
+ import { DataViewFilterOption } from '../DataViewFilters';
4
+ export declare const isDataViewFilterOption: (obj: unknown) => obj is DataViewFilterOption;
5
+ /** extends MenuProps */
6
+ export interface DataViewCheckboxFilterProps extends Omit<MenuProps, 'onSelect' | 'onChange'> {
7
+ /** Unique key for the filter attribute */
8
+ filterId: string;
9
+ /** Array of current filter values */
10
+ value?: string[];
11
+ /** Filter title displayed in the toolbar */
12
+ title: string;
13
+ /** Placeholder text of the menu */
14
+ placeholder?: string;
15
+ /** Filter options displayed */
16
+ options: (DataViewFilterOption | string)[];
17
+ /** Callback for updating when item selection changes. */
18
+ onChange?: (event?: React.MouseEvent, values?: string[]) => void;
19
+ /** Controls visibility of the filter in the toolbar */
20
+ showToolbarItem?: boolean;
21
+ /** Controls visibility of the filter icon */
22
+ showIcon?: boolean;
23
+ /** Controls visibility of the selected items badge */
24
+ showBadge?: boolean;
25
+ /** Custom OUIA ID */
26
+ ouiaId?: string;
27
+ }
28
+ export declare const DataViewCheckboxFilter: React.FC<DataViewCheckboxFilterProps>;
29
+ export default DataViewCheckboxFilter;
@@ -0,0 +1,62 @@
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import React from 'react';
13
+ import { Badge, Menu, MenuContent, MenuItem, MenuList, MenuToggle, Popper, ToolbarFilter, } from '@patternfly/react-core';
14
+ import { FilterIcon } from '@patternfly/react-icons';
15
+ const isToolbarLabel = (label) => typeof label === 'object' && 'key' in label;
16
+ export const isDataViewFilterOption = (obj) => !!obj &&
17
+ typeof obj === 'object' &&
18
+ 'label' in obj &&
19
+ 'value' in obj &&
20
+ typeof obj.value === 'string';
21
+ export const DataViewCheckboxFilter = (_a) => {
22
+ var { filterId, title, value = [], onChange, placeholder, options = [], showToolbarItem, showIcon = !placeholder, showBadge = !placeholder, ouiaId = 'DataViewCheckboxFilter' } = _a, props = __rest(_a, ["filterId", "title", "value", "onChange", "placeholder", "options", "showToolbarItem", "showIcon", "showBadge", "ouiaId"]);
23
+ const [isOpen, setIsOpen] = React.useState(false);
24
+ const toggleRef = React.useRef(null);
25
+ const menuRef = React.useRef(null);
26
+ const containerRef = React.useRef(null);
27
+ const normalizeOptions = React.useMemo(() => options.map(option => typeof option === 'string'
28
+ ? { label: option, value: option }
29
+ : option), [options]);
30
+ const handleToggleClick = (event) => {
31
+ event.stopPropagation();
32
+ setTimeout(() => {
33
+ var _a;
34
+ const firstElement = (_a = menuRef.current) === null || _a === void 0 ? void 0 : _a.querySelector('li > button:not(:disabled)');
35
+ firstElement === null || firstElement === void 0 ? void 0 : firstElement.focus();
36
+ }, 0);
37
+ setIsOpen(prev => !prev);
38
+ };
39
+ const handleSelect = (event, itemId) => {
40
+ const activeItem = String(itemId);
41
+ const isSelected = value.includes(activeItem);
42
+ onChange === null || onChange === void 0 ? void 0 : onChange(event, isSelected ? value.filter(item => item !== activeItem) : [activeItem, ...value]);
43
+ };
44
+ const handleClickOutside = (event) => isOpen &&
45
+ menuRef.current && toggleRef.current &&
46
+ !menuRef.current.contains(event.target) && !toggleRef.current.contains(event.target)
47
+ && setIsOpen(false);
48
+ React.useEffect(() => {
49
+ window.addEventListener('click', handleClickOutside);
50
+ return () => {
51
+ window.removeEventListener('click', handleClickOutside);
52
+ };
53
+ }, [isOpen]); // eslint-disable-line react-hooks/exhaustive-deps
54
+ return (React.createElement(ToolbarFilter, { key: ouiaId, "data-ouia-component-id": ouiaId, labels: value.map(item => {
55
+ const activeOption = normalizeOptions.find(option => option.value === item);
56
+ return ({ key: activeOption === null || activeOption === void 0 ? void 0 : activeOption.value, node: activeOption === null || activeOption === void 0 ? void 0 : activeOption.label });
57
+ }), deleteLabel: (_, label) => onChange === null || onChange === void 0 ? void 0 : onChange(undefined, value.filter(item => item !== (isToolbarLabel(label) ? label.key : label))), categoryName: title, showToolbarItem: showToolbarItem },
58
+ React.createElement(Popper, { trigger: React.createElement(MenuToggle, { ouiaId: `${ouiaId}-toggle`, ref: toggleRef, onClick: handleToggleClick, isExpanded: isOpen, icon: showIcon ? React.createElement(FilterIcon, null) : undefined, badge: value.length > 0 && showBadge ? React.createElement(Badge, { "data-ouia-component-id": `${ouiaId}-badge`, isRead: true }, value.length) : undefined, style: { width: '200px' } }, placeholder !== null && placeholder !== void 0 ? placeholder : title), triggerRef: toggleRef, popper: React.createElement(Menu, Object.assign({ ref: menuRef, ouiaId: `${ouiaId}-menu`, onSelect: handleSelect, selected: value }, props),
59
+ React.createElement(MenuContent, null,
60
+ React.createElement(MenuList, null, normalizeOptions.map(option => (React.createElement(MenuItem, { "data-ouia-component-id": `${ouiaId}-filter-item-${option.value}`, key: option.value, itemId: option.value, isSelected: value.includes(option.value), hasCheckbox: true }, option.label)))))), popperRef: menuRef, appendTo: containerRef.current || undefined, "aria-label": `${title !== null && title !== void 0 ? title : filterId} filter`, isVisible: isOpen })));
61
+ };
62
+ export default DataViewCheckboxFilter;
@@ -0,0 +1,20 @@
1
+ import React from 'react';
2
+ import { render } from '@testing-library/react';
3
+ import DataViewCheckboxFilter from './DataViewCheckboxFilter';
4
+ import DataViewToolbar from '../DataViewToolbar';
5
+ describe('DataViewCheckboxFilter component', () => {
6
+ const defaultProps = {
7
+ filterId: 'test-checkbox-filter',
8
+ title: 'Test Checkbox Filter',
9
+ value: ['workspace-one'],
10
+ options: [
11
+ { label: 'Workspace one', value: 'workspace-one' },
12
+ { label: 'Workspace two', value: 'workspace-two' },
13
+ { label: 'Workspace three', value: 'workspace-three' },
14
+ ],
15
+ };
16
+ it('should render correctly', () => {
17
+ const { container } = render(React.createElement(DataViewToolbar, { filters: React.createElement(DataViewCheckboxFilter, Object.assign({}, defaultProps)) }));
18
+ expect(container).toMatchSnapshot();
19
+ });
20
+ });
@@ -0,0 +1,2 @@
1
+ export { default } from './DataViewCheckboxFilter';
2
+ export * from './DataViewCheckboxFilter';
@@ -0,0 +1,2 @@
1
+ export { default } from './DataViewCheckboxFilter';
2
+ export * from './DataViewCheckboxFilter';
@@ -0,0 +1,25 @@
1
+ import React, { ReactNode } from 'react';
2
+ import { ToolbarToggleGroupProps } from '@patternfly/react-core';
3
+ export interface DataViewFilterOption {
4
+ /** Filter option label */
5
+ label: ReactNode;
6
+ /** Filter option value */
7
+ value: string;
8
+ }
9
+ /** extends ToolbarToggleGroupProps */
10
+ export interface DataViewFiltersProps<T extends object> extends Omit<ToolbarToggleGroupProps, 'toggleIcon' | 'breakpoint' | 'onChange'> {
11
+ /** Content rendered inside the data view */
12
+ children: React.ReactNode;
13
+ /** Optional onChange callback shared across filters */
14
+ onChange?: (key: string, newValues: Partial<T>) => void;
15
+ /** Optional values shared across filters */
16
+ values?: T;
17
+ /** Icon for the toolbar toggle group */
18
+ toggleIcon?: ToolbarToggleGroupProps['toggleIcon'];
19
+ /** Breakpoint for the toolbar toggle group */
20
+ breakpoint?: ToolbarToggleGroupProps['breakpoint'];
21
+ /** Custom OUIA ID */
22
+ ouiaId?: string;
23
+ }
24
+ export declare const DataViewFilters: <T extends object>({ children, ouiaId, toggleIcon, breakpoint, onChange, values, ...props }: DataViewFiltersProps<T>) => React.JSX.Element;
25
+ export default DataViewFilters;