@patternfly/react-data-view 5.4.1 → 5.5.1

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 (89) 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/DataViewTableTree/DataViewTableTree.js +26 -14
  12. package/dist/cjs/DataViewTextFilter/DataViewTextFilter.d.ts +21 -0
  13. package/dist/cjs/DataViewTextFilter/DataViewTextFilter.js +26 -0
  14. package/dist/cjs/DataViewTextFilter/DataViewTextFilter.test.d.ts +1 -0
  15. package/dist/cjs/DataViewTextFilter/DataViewTextFilter.test.js +22 -0
  16. package/dist/cjs/DataViewTextFilter/index.d.ts +2 -0
  17. package/dist/cjs/DataViewTextFilter/index.js +23 -0
  18. package/dist/cjs/DataViewToolbar/DataViewToolbar.d.ts +10 -4
  19. package/dist/cjs/DataViewToolbar/DataViewToolbar.js +29 -6
  20. package/dist/cjs/Hooks/filters.d.ts +14 -0
  21. package/dist/cjs/Hooks/filters.js +70 -0
  22. package/dist/cjs/Hooks/filters.test.d.ts +1 -0
  23. package/dist/cjs/Hooks/filters.test.js +50 -0
  24. package/dist/cjs/Hooks/index.d.ts +1 -0
  25. package/dist/cjs/Hooks/index.js +1 -0
  26. package/dist/cjs/Hooks/pagination.d.ts +1 -0
  27. package/dist/cjs/Hooks/selection.d.ts +1 -1
  28. package/dist/cjs/Hooks/selection.js +4 -2
  29. package/dist/cjs/InternalContext/InternalContext.d.ts +1 -0
  30. package/dist/cjs/index.d.ts +2 -0
  31. package/dist/cjs/index.js +4 -1
  32. package/dist/dynamic/DataViewFilters/package.json +1 -0
  33. package/dist/dynamic/DataViewTextFilter/package.json +1 -0
  34. package/dist/esm/DataViewFilters/DataViewFilters.d.ts +19 -0
  35. package/dist/esm/DataViewFilters/DataViewFilters.js +43 -0
  36. package/dist/esm/DataViewFilters/DataViewFilters.test.d.ts +1 -0
  37. package/dist/esm/DataViewFilters/DataViewFilters.test.js +14 -0
  38. package/dist/esm/DataViewFilters/index.d.ts +2 -0
  39. package/dist/esm/DataViewFilters/index.js +2 -0
  40. package/dist/esm/DataViewTable/DataViewTable.d.ts +8 -0
  41. package/dist/esm/DataViewTableBasic/DataViewTableBasic.d.ts +1 -0
  42. package/dist/esm/DataViewTableHead/DataViewTableHead.d.ts +1 -0
  43. package/dist/esm/DataViewTableTree/DataViewTableTree.d.ts +1 -0
  44. package/dist/esm/DataViewTableTree/DataViewTableTree.js +26 -14
  45. package/dist/esm/DataViewTextFilter/DataViewTextFilter.d.ts +21 -0
  46. package/dist/esm/DataViewTextFilter/DataViewTextFilter.js +19 -0
  47. package/dist/esm/DataViewTextFilter/DataViewTextFilter.test.d.ts +1 -0
  48. package/dist/esm/DataViewTextFilter/DataViewTextFilter.test.js +17 -0
  49. package/dist/esm/DataViewTextFilter/index.d.ts +2 -0
  50. package/dist/esm/DataViewTextFilter/index.js +2 -0
  51. package/dist/esm/DataViewToolbar/DataViewToolbar.d.ts +10 -4
  52. package/dist/esm/DataViewToolbar/DataViewToolbar.js +7 -4
  53. package/dist/esm/Hooks/filters.d.ts +14 -0
  54. package/dist/esm/Hooks/filters.js +66 -0
  55. package/dist/esm/Hooks/filters.test.d.ts +1 -0
  56. package/dist/esm/Hooks/filters.test.js +48 -0
  57. package/dist/esm/Hooks/index.d.ts +1 -0
  58. package/dist/esm/Hooks/index.js +1 -0
  59. package/dist/esm/Hooks/pagination.d.ts +1 -0
  60. package/dist/esm/Hooks/selection.d.ts +1 -1
  61. package/dist/esm/Hooks/selection.js +4 -2
  62. package/dist/esm/InternalContext/InternalContext.d.ts +1 -0
  63. package/dist/esm/index.d.ts +2 -0
  64. package/dist/esm/index.js +2 -0
  65. package/package.json +1 -1
  66. package/patternfly-docs/content/extensions/data-view/examples/Components/Components.md +2 -2
  67. package/patternfly-docs/content/extensions/data-view/examples/Functionality/FiltersExample.tsx +92 -0
  68. package/patternfly-docs/content/extensions/data-view/examples/Functionality/Functionality.md +36 -2
  69. package/src/DataViewFilters/DataViewFilters.test.tsx +21 -0
  70. package/src/DataViewFilters/DataViewFilters.tsx +125 -0
  71. package/src/DataViewFilters/__snapshots__/DataViewFilters.test.tsx.snap +182 -0
  72. package/src/DataViewFilters/index.tsx +2 -0
  73. package/src/DataViewTable/DataViewTable.tsx +23 -3
  74. package/src/DataViewTableBasic/DataViewTableBasic.tsx +1 -0
  75. package/src/DataViewTableHead/DataViewTableHead.tsx +1 -0
  76. package/src/DataViewTableTree/DataViewTableTree.tsx +40 -18
  77. package/src/DataViewTextFilter/DataViewTextFilter.test.tsx +24 -0
  78. package/src/DataViewTextFilter/DataViewTextFilter.tsx +53 -0
  79. package/src/DataViewTextFilter/__snapshots__/DataViewTextFilter.test.tsx.snap +196 -0
  80. package/src/DataViewTextFilter/index.ts +2 -0
  81. package/src/DataViewToolbar/DataViewToolbar.tsx +47 -28
  82. package/src/DataViewToolbar/__snapshots__/DataViewToolbar.test.tsx.snap +28 -0
  83. package/src/Hooks/filters.test.tsx +62 -0
  84. package/src/Hooks/filters.ts +96 -0
  85. package/src/Hooks/index.ts +1 -0
  86. package/src/Hooks/pagination.ts +1 -0
  87. package/src/Hooks/selection.ts +3 -2
  88. package/src/InternalContext/InternalContext.tsx +1 -0
  89. package/src/index.ts +3 -0
@@ -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 => {
@@ -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';
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.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);
@@ -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"}
@@ -0,0 +1,19 @@
1
+ import React from 'react';
2
+ import { ToolbarToggleGroupProps } from '@patternfly/react-core';
3
+ /** extends ToolbarToggleGroupProps */
4
+ export interface DataViewFiltersProps<T extends object> extends Omit<ToolbarToggleGroupProps, 'toggleIcon' | 'breakpoint' | 'onChange'> {
5
+ /** Content rendered inside the data view */
6
+ children: React.ReactNode;
7
+ /** Optional onChange callback shared across filters */
8
+ onChange?: (key: string, newValues: Partial<T>) => void;
9
+ /** Optional values shared across filters */
10
+ values?: T;
11
+ /** Icon for the toolbar toggle group */
12
+ toggleIcon?: ToolbarToggleGroupProps['toggleIcon'];
13
+ /** Breakpoint for the toolbar toggle group */
14
+ breakpoint?: ToolbarToggleGroupProps['breakpoint'];
15
+ /** Custom OUIA ID */
16
+ ouiaId?: string;
17
+ }
18
+ export declare const DataViewFilters: <T extends object>({ children, ouiaId, toggleIcon, breakpoint, onChange, values, ...props }: DataViewFiltersProps<T>) => React.JSX.Element;
19
+ export default DataViewFilters;
@@ -0,0 +1,43 @@
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, { useMemo, useState, useRef, useEffect } from 'react';
13
+ import { Menu, MenuContent, MenuItem, MenuList, MenuToggle, Popper, ToolbarGroup, ToolbarToggleGroup, } from '@patternfly/react-core';
14
+ import { FilterIcon } from '@patternfly/react-icons';
15
+ ;
16
+ export const DataViewFilters = (_a) => {
17
+ var { children, ouiaId = 'DataViewFilters', toggleIcon = React.createElement(FilterIcon, null), breakpoint = 'xl', onChange, values } = _a, props = __rest(_a, ["children", "ouiaId", "toggleIcon", "breakpoint", "onChange", "values"]);
18
+ const [activeAttributeMenu, setActiveAttributeMenu] = useState('');
19
+ const [isAttributeMenuOpen, setIsAttributeMenuOpen] = useState(false);
20
+ const attributeToggleRef = useRef(null);
21
+ const attributeMenuRef = useRef(null);
22
+ const attributeContainerRef = useRef(null);
23
+ const childrenHash = useMemo(() => JSON.stringify(React.Children.map(children, (child) => React.isValidElement(child) ? { type: child.type, key: child.key, props: child.props } : child)), [children]);
24
+ const filterItems = useMemo(() => React.Children.toArray(children)
25
+ .map(child => React.isValidElement(child) ? { filterId: String(child.props.filterId), title: String(child.props.title) } : undefined).filter((item) => !!item), [childrenHash]); // eslint-disable-line react-hooks/exhaustive-deps
26
+ useEffect(() => {
27
+ filterItems.length > 0 && setActiveAttributeMenu(filterItems[0].title);
28
+ }, [filterItems]);
29
+ const attributeToggle = (React.createElement(MenuToggle, { ref: attributeToggleRef, onClick: () => setIsAttributeMenuOpen(!isAttributeMenuOpen), isExpanded: isAttributeMenuOpen, icon: toggleIcon }, activeAttributeMenu));
30
+ const attributeMenu = (React.createElement(Menu, { ref: attributeMenuRef, onSelect: (_ev, itemId) => {
31
+ const selectedItem = filterItems.find(item => item.filterId === itemId);
32
+ selectedItem && setActiveAttributeMenu(selectedItem.title);
33
+ setIsAttributeMenuOpen(false);
34
+ } },
35
+ React.createElement(MenuContent, null,
36
+ React.createElement(MenuList, null, filterItems.map(item => (React.createElement(MenuItem, { key: item.filterId, itemId: item.filterId }, item.title)))))));
37
+ return (React.createElement(ToolbarToggleGroup, Object.assign({ "data-ouia-component-id": ouiaId, toggleIcon: toggleIcon, breakpoint: breakpoint }, props),
38
+ React.createElement(ToolbarGroup, { variant: "filter-group" },
39
+ React.createElement("div", { ref: attributeContainerRef },
40
+ React.createElement(Popper, { trigger: attributeToggle, triggerRef: attributeToggleRef, popper: attributeMenu, popperRef: attributeMenuRef, appendTo: attributeContainerRef.current || undefined, isVisible: isAttributeMenuOpen })),
41
+ React.Children.map(children, (child) => (React.isValidElement(child) ? (React.cloneElement(child, Object.assign({ showToolbarItem: activeAttributeMenu === child.props.title, onChange: (event, value) => onChange === null || onChange === void 0 ? void 0 : onChange(event, { [child.props.filterId]: value }), value: values === null || values === void 0 ? void 0 : values[child.props.filterId] }, child.props))) : child)))));
42
+ };
43
+ export default DataViewFilters;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,14 @@
1
+ import React from 'react';
2
+ import { render } from '@testing-library/react';
3
+ import DataViewFilters from './DataViewFilters';
4
+ import DataViewToolbar from '../DataViewToolbar';
5
+ import DataViewTextFilter from '../DataViewTextFilter';
6
+ describe('DataViewFilters component', () => {
7
+ const mockOnChange = jest.fn();
8
+ it('should render correctly', () => {
9
+ const { container } = render(React.createElement(DataViewToolbar, { filters: React.createElement(DataViewFilters, { onChange: mockOnChange, values: {} },
10
+ React.createElement(DataViewTextFilter, { filterId: "one", title: "One" }),
11
+ React.createElement(DataViewTextFilter, { filterId: "two", title: "Two" })) }));
12
+ expect(container).toMatchSnapshot();
13
+ });
14
+ });
@@ -0,0 +1,2 @@
1
+ export { default } from './DataViewFilters';
2
+ export * from './DataViewFilters';
@@ -0,0 +1,2 @@
1
+ export { default } from './DataViewFilters';
2
+ export * from './DataViewFilters';
@@ -3,7 +3,9 @@ import { TdProps, ThProps, TrProps } from '@patternfly/react-table';
3
3
  import { DataViewTableTreeProps } from '../DataViewTableTree';
4
4
  import { DataViewTableBasicProps } from '../DataViewTableBasic';
5
5
  export type DataViewTh = ReactNode | {
6
+ /** Table head cell node */
6
7
  cell: ReactNode;
8
+ /** Props passed to Th */
7
9
  props?: ThProps;
8
10
  };
9
11
  export declare const isDataViewThObject: (value: DataViewTh) => value is {
@@ -11,12 +13,17 @@ export declare const isDataViewThObject: (value: DataViewTh) => value is {
11
13
  props?: ThProps | undefined;
12
14
  };
13
15
  export interface DataViewTrObject {
16
+ /** Array of rows */
14
17
  row: DataViewTd[];
18
+ /** Unique identifier of a row */
15
19
  id?: string;
20
+ /** Props passed to Tr */
16
21
  props?: TrProps;
17
22
  }
18
23
  export type DataViewTd = ReactNode | {
24
+ /** Table body cell node */
19
25
  cell: ReactNode;
26
+ /** Props passed to Td */
20
27
  props?: TdProps;
21
28
  };
22
29
  export type DataViewTr = DataViewTd[] | DataViewTrObject;
@@ -28,6 +35,7 @@ export declare const isDataViewTrObject: (value: DataViewTr) => value is {
28
35
  row: DataViewTd[];
29
36
  id?: string | undefined;
30
37
  };
38
+ /** extends DataViewTrObject */
31
39
  export interface DataViewTrTree extends DataViewTrObject {
32
40
  id: string;
33
41
  children?: DataViewTrTree[];
@@ -2,6 +2,7 @@ import React from 'react';
2
2
  import { TableProps } from '@patternfly/react-table';
3
3
  import { DataViewTh, DataViewTr } from '../DataViewTable';
4
4
  import { DataViewState } from '../DataView/DataView';
5
+ /** extends TableProps */
5
6
  export interface DataViewTableBasicProps extends Omit<TableProps, 'onSelect' | 'rows'> {
6
7
  /** Columns definition */
7
8
  columns: DataViewTh[];
@@ -1,6 +1,7 @@
1
1
  import React from 'react';
2
2
  import { TheadProps } from '@patternfly/react-table';
3
3
  import { DataViewTh } from '../DataViewTable';
4
+ /** extends TheadProps */
4
5
  export interface DataViewTableHeadProps extends TheadProps {
5
6
  /** Indicates whether table is a tree */
6
7
  isTreeTable?: boolean;
@@ -2,6 +2,7 @@ import React from 'react';
2
2
  import { TableProps } from '@patternfly/react-table';
3
3
  import { DataViewTh, DataViewTrTree } from '../DataViewTable';
4
4
  import { DataViewState } from '../DataView/DataView';
5
+ /** extends TableProps */
5
6
  export interface DataViewTableTreeProps extends Omit<TableProps, 'onSelect' | 'rows'> {
6
7
  /** Columns definition */
7
8
  columns: DataViewTh[];
@@ -14,19 +14,31 @@ import { Table, Tbody, Td, TreeRowWrapper, } from '@patternfly/react-table';
14
14
  import { useInternalContext } from '../InternalContext';
15
15
  import { DataViewTableHead } from '../DataViewTableHead';
16
16
  import { isDataViewTdObject } from '../DataViewTable';
17
- const getDescendants = (node) => (!node.children || !node.children.length) ? [node] : node.children.flatMap(getDescendants);
18
- const isNodeChecked = (node, isSelected) => {
19
- let allSelected = true;
20
- let someSelected = false;
21
- for (const descendant of getDescendants(node)) {
22
- const selected = !!(isSelected === null || isSelected === void 0 ? void 0 : isSelected(descendant));
23
- someSelected || (someSelected = selected);
24
- allSelected && (allSelected = selected);
25
- if (!allSelected && someSelected) {
26
- return null;
17
+ const getNodesAffectedBySelection = (allRows, node, isChecking, isSelected) => {
18
+ const getDescendants = (node) => node.children ? node.children.flatMap(getDescendants).concat(node) : [node];
19
+ const findParent = (child, rows) => {
20
+ var _a;
21
+ return (_a = rows.find(row => { var _a; return (_a = row.children) === null || _a === void 0 ? void 0 : _a.some(c => c === child); })) !== null && _a !== void 0 ? _a : rows.flatMap(row => { var _a; return (_a = row.children) !== null && _a !== void 0 ? _a : []; }).map(c => findParent(child, [c])).find(p => p);
22
+ };
23
+ const getAncestors = (node) => {
24
+ const ancestors = [];
25
+ let parent = findParent(node, allRows);
26
+ while (parent) {
27
+ ancestors.push(parent);
28
+ parent = findParent(parent, allRows);
27
29
  }
28
- }
29
- return allSelected;
30
+ return ancestors;
31
+ };
32
+ const affectedNodes = new Set([node, ...getDescendants(node)]);
33
+ getAncestors(node).forEach(ancestor => {
34
+ var _a, _b;
35
+ const allChildrenSelected = (_a = ancestor.children) === null || _a === void 0 ? void 0 : _a.every(child => (isSelected === null || isSelected === void 0 ? void 0 : isSelected(child)) || affectedNodes.has(child));
36
+ const anyChildAffected = (_b = ancestor.children) === null || _b === void 0 ? void 0 : _b.some(child => affectedNodes.has(child) || child.id === node.id);
37
+ if (isChecking ? !(isSelected === null || isSelected === void 0 ? void 0 : isSelected(ancestor)) && allChildrenSelected : (isSelected === null || isSelected === void 0 ? void 0 : isSelected(ancestor)) && anyChildAffected) {
38
+ affectedNodes.add(ancestor);
39
+ }
40
+ });
41
+ return Array.from(affectedNodes);
30
42
  };
31
43
  export const DataViewTableTree = (_a) => {
32
44
  var { columns, rows, headStates, bodyStates, leafIcon = null, expandedIcon = null, collapsedIcon = null, ouiaId = 'DataViewTableTree' } = _a, props = __rest(_a, ["columns", "rows", "headStates", "bodyStates", "leafIcon", "expandedIcon", "collapsedIcon", "ouiaId"]);
@@ -44,7 +56,7 @@ export const DataViewTableTree = (_a) => {
44
56
  }
45
57
  const isExpanded = expandedNodeIds.includes(node.id);
46
58
  const isDetailsExpanded = expandedDetailsNodeNames.includes(node.id);
47
- const isChecked = isSelected && isNodeChecked(node, isSelected);
59
+ const isChecked = isSelected === null || isSelected === void 0 ? void 0 : isSelected(node);
48
60
  let icon = leafIcon;
49
61
  if (node.children) {
50
62
  icon = isExpanded ? expandedIcon : collapsedIcon;
@@ -58,7 +70,7 @@ export const DataViewTableTree = (_a) => {
58
70
  const otherDetailsExpandedNodeIds = prevDetailsExpanded.filter(id => id !== node.id);
59
71
  return isDetailsExpanded ? otherDetailsExpandedNodeIds : [...otherDetailsExpandedNodeIds, node.id];
60
72
  }),
61
- onCheckChange: ((isSelectDisabled === null || isSelectDisabled === void 0 ? void 0 : isSelectDisabled(node)) || !onSelect) ? undefined : (_event, isChecking) => onSelect === null || onSelect === void 0 ? void 0 : onSelect(isChecking, getDescendants(node)),
73
+ onCheckChange: ((isSelectDisabled === null || isSelectDisabled === void 0 ? void 0 : isSelectDisabled(node)) || !onSelect) ? undefined : (_event, isChecking) => onSelect === null || onSelect === void 0 ? void 0 : onSelect(isChecking, getNodesAffectedBySelection(rows, node, isChecking, isSelected)),
62
74
  rowIndex,
63
75
  props: {
64
76
  isExpanded,
@@ -0,0 +1,21 @@
1
+ import React from 'react';
2
+ import { SearchInputProps, ToolbarFilterProps } from '@patternfly/react-core';
3
+ /** extends SearchInputProps */
4
+ export interface DataViewTextFilterProps extends SearchInputProps {
5
+ /** Unique key for the filter attribute */
6
+ filterId: string;
7
+ /** Current filter value */
8
+ value?: string;
9
+ /** Filter title displayed in the toolbar */
10
+ title: string;
11
+ /** Callback for when the input value changes */
12
+ onChange?: (event: React.FormEvent<HTMLInputElement> | undefined, value: string) => void;
13
+ /** Controls visibility of the filter in the toolbar */
14
+ showToolbarItem?: ToolbarFilterProps['showToolbarItem'];
15
+ /** Trims input value on change */
16
+ trimValue?: boolean;
17
+ /** Custom OUIA ID */
18
+ ouiaId?: string;
19
+ }
20
+ export declare const DataViewTextFilter: React.FC<DataViewTextFilterProps>;
21
+ export default DataViewTextFilter;
@@ -0,0 +1,19 @@
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 { SearchInput, ToolbarFilter } from '@patternfly/react-core';
14
+ export const DataViewTextFilter = (_a) => {
15
+ var { filterId, title, value = '', onChange, onClear = () => onChange === null || onChange === void 0 ? void 0 : onChange(undefined, ''), showToolbarItem, trimValue = true, ouiaId = 'DataViewTextFilter' } = _a, props = __rest(_a, ["filterId", "title", "value", "onChange", "onClear", "showToolbarItem", "trimValue", "ouiaId"]);
16
+ return (React.createElement(ToolbarFilter, { "data-ouia-component-id": ouiaId, chips: value.length > 0 ? [{ key: title, node: value }] : [], deleteChip: () => onChange === null || onChange === void 0 ? void 0 : onChange(undefined, ''), categoryName: title, showToolbarItem: showToolbarItem },
17
+ React.createElement(SearchInput, Object.assign({ searchInputId: filterId, value: value, onChange: (e, inputValue) => onChange === null || onChange === void 0 ? void 0 : onChange(e, trimValue ? inputValue.trim() : inputValue), onClear: onClear, placeholder: `Filter by ${title}`, "aria-label": `${title !== null && title !== void 0 ? title : filterId} filter`, "data-ouia-component-id": `${ouiaId}-input` }, props))));
18
+ };
19
+ export default DataViewTextFilter;
@@ -0,0 +1,17 @@
1
+ import React from 'react';
2
+ import { render } from '@testing-library/react';
3
+ import DataViewTextFilter from './DataViewTextFilter';
4
+ import DataViewToolbar from '../DataViewToolbar';
5
+ describe('DataViewTextFilter component', () => {
6
+ const mockOnChange = jest.fn();
7
+ const defaultProps = {
8
+ filterId: 'test-filter',
9
+ title: 'Test Filter',
10
+ value: 'initial value',
11
+ onChange: mockOnChange,
12
+ };
13
+ it('should render correctly', () => {
14
+ const { container } = render(React.createElement(DataViewToolbar, { filters: React.createElement(DataViewTextFilter, Object.assign({}, defaultProps)) }));
15
+ expect(container).toMatchSnapshot();
16
+ });
17
+ });
@@ -0,0 +1,2 @@
1
+ export { default } from './DataViewTextFilter';
2
+ export * from './DataViewTextFilter';
@@ -0,0 +1,2 @@
1
+ export { default } from './DataViewTextFilter';
2
+ export * from './DataViewTextFilter';
@@ -1,15 +1,21 @@
1
1
  import React, { PropsWithChildren } from 'react';
2
- export interface DataViewToolbarProps extends PropsWithChildren {
2
+ import { ToolbarProps } from '@patternfly/react-core';
3
+ /** extends ToolbarProps */
4
+ export interface DataViewToolbarProps extends Omit<PropsWithChildren<ToolbarProps>, 'ref'> {
3
5
  /** Toolbar className */
4
6
  className?: string;
5
7
  /** Custom OUIA ID */
6
8
  ouiaId?: string;
7
- /** React component to display bulk select */
9
+ /** React node to display bulk select */
8
10
  bulkSelect?: React.ReactNode;
9
- /** React component to display pagination */
11
+ /** React node to display pagination */
10
12
  pagination?: React.ReactNode;
11
- /** React component to display actions */
13
+ /** React node to display actions */
12
14
  actions?: React.ReactNode;
15
+ /** React node to display filters */
16
+ filters?: React.ReactNode;
17
+ /** React node to display custom filter chips */
18
+ customChipGroupContent?: React.ReactNode;
13
19
  }
14
20
  export declare const DataViewToolbar: React.FC<DataViewToolbarProps>;
15
21
  export default DataViewToolbar;
@@ -9,14 +9,17 @@ var __rest = (this && this.__rest) || function (s, e) {
9
9
  }
10
10
  return t;
11
11
  };
12
- import React from 'react';
13
- import { Toolbar, ToolbarContent, ToolbarItem, ToolbarItemVariant } from '@patternfly/react-core';
12
+ import React, { useRef } from 'react';
13
+ import { Button, Toolbar, ToolbarContent, ToolbarItem, ToolbarItemVariant } from '@patternfly/react-core';
14
14
  export const DataViewToolbar = (_a) => {
15
- var { className, ouiaId = 'DataViewToolbar', bulkSelect, actions, pagination, children } = _a, props = __rest(_a, ["className", "ouiaId", "bulkSelect", "actions", "pagination", "children"]);
16
- return (React.createElement(Toolbar, Object.assign({ ouiaId: ouiaId, className: className }, props),
15
+ var { className, ouiaId = 'DataViewToolbar', bulkSelect, actions, pagination, filters, customChipGroupContent, clearAllFilters, children } = _a, props = __rest(_a, ["className", "ouiaId", "bulkSelect", "actions", "pagination", "filters", "customChipGroupContent", "clearAllFilters", "children"]);
16
+ const defaultClearFilters = useRef(React.createElement(ToolbarItem, null,
17
+ React.createElement(Button, { ouiaId: `${ouiaId}-clear-all-filters`, variant: "link", onClick: clearAllFilters, isInline: true }, "Clear filters")));
18
+ return (React.createElement(Toolbar, Object.assign({ ouiaId: ouiaId, className: className, customChipGroupContent: customChipGroupContent !== null && customChipGroupContent !== void 0 ? customChipGroupContent : defaultClearFilters.current }, props),
17
19
  React.createElement(ToolbarContent, null,
18
20
  bulkSelect && (React.createElement(ToolbarItem, { "data-ouia-component-id": `${ouiaId}-bulk-select` }, bulkSelect)),
19
21
  actions && (React.createElement(ToolbarItem, { variant: ToolbarItemVariant['overflow-menu'] }, actions)),
22
+ filters && (React.createElement(ToolbarItem, { variant: ToolbarItemVariant['search-filter'] }, filters)),
20
23
  pagination && (React.createElement(ToolbarItem, { variant: ToolbarItemVariant.pagination, "data-ouia-component-id": `${ouiaId}-pagination` }, pagination)),
21
24
  children)));
22
25
  };
@@ -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,66 @@
1
+ import { useState, useCallback, useEffect, useMemo } from "react";
2
+ ;
3
+ export const useDataViewFilters = ({ initialFilters = {}, searchParams, setSearchParams, }) => {
4
+ const isUrlSyncEnabled = useMemo(() => searchParams && !!setSearchParams, [searchParams, setSearchParams]);
5
+ const getInitialFilters = useCallback(() => isUrlSyncEnabled ? Object.keys(initialFilters).reduce((loadedFilters, key) => {
6
+ const urlValue = searchParams === null || searchParams === void 0 ? void 0 : searchParams.get(key);
7
+ loadedFilters[key] = urlValue
8
+ ? urlValue
9
+ : initialFilters[key];
10
+ return loadedFilters;
11
+ // eslint-disable-next-line react-hooks/exhaustive-deps
12
+ }, Object.assign({}, initialFilters)) : initialFilters, [isUrlSyncEnabled, JSON.stringify(initialFilters), searchParams === null || searchParams === void 0 ? void 0 : searchParams.toString()]);
13
+ const [filters, setFilters] = useState(getInitialFilters());
14
+ const updateSearchParams = useCallback((newFilters) => {
15
+ if (isUrlSyncEnabled) {
16
+ const params = new URLSearchParams(searchParams);
17
+ Object.entries(newFilters).forEach(([key, value]) => {
18
+ if (value) {
19
+ params.set(key, Array.isArray(value) ? value.join(',') : value);
20
+ }
21
+ else {
22
+ params.delete(key);
23
+ }
24
+ });
25
+ setSearchParams === null || setSearchParams === void 0 ? void 0 : setSearchParams(params);
26
+ }
27
+ }, [isUrlSyncEnabled, searchParams, setSearchParams]);
28
+ useEffect(() => {
29
+ isUrlSyncEnabled && setFilters(getInitialFilters());
30
+ }, []); // eslint-disable-line react-hooks/exhaustive-deps
31
+ const onSetFilters = useCallback((newFilters) => {
32
+ setFilters(prevFilters => {
33
+ const updatedFilters = Object.assign(Object.assign({}, prevFilters), newFilters);
34
+ isUrlSyncEnabled && updateSearchParams(updatedFilters);
35
+ return updatedFilters;
36
+ });
37
+ }, [isUrlSyncEnabled, updateSearchParams]);
38
+ // helper function to reset filters
39
+ const resetFilterValues = useCallback((filters) => Object.entries(filters).reduce((acc, [key, value]) => {
40
+ if (Array.isArray(value)) {
41
+ acc[key] = [];
42
+ }
43
+ else {
44
+ acc[key] = '';
45
+ }
46
+ return acc;
47
+ }, {}), []);
48
+ const onDeleteFilters = useCallback((filtersToDelete) => {
49
+ setFilters(prevFilters => {
50
+ const updatedFilters = Object.assign(Object.assign({}, prevFilters), resetFilterValues(filtersToDelete));
51
+ isUrlSyncEnabled && updateSearchParams(updatedFilters);
52
+ return updatedFilters;
53
+ });
54
+ }, [isUrlSyncEnabled, updateSearchParams, resetFilterValues]);
55
+ const clearAllFilters = useCallback(() => {
56
+ const clearedFilters = resetFilterValues(filters);
57
+ setFilters(clearedFilters);
58
+ isUrlSyncEnabled && updateSearchParams(clearedFilters);
59
+ }, [filters, isUrlSyncEnabled, updateSearchParams, resetFilterValues]);
60
+ return {
61
+ filters,
62
+ onSetFilters,
63
+ onDeleteFilters,
64
+ clearAllFilters,
65
+ };
66
+ };
@@ -0,0 +1 @@
1
+ import '@testing-library/jest-dom';
@@ -0,0 +1,48 @@
1
+ import '@testing-library/jest-dom';
2
+ import { renderHook, act } from '@testing-library/react';
3
+ import { useDataViewFilters } from './filters';
4
+ describe('useDataViewFilters', () => {
5
+ const initialFilters = { search: 'test', tags: ['tag1', 'tag2'] };
6
+ it('should initialize with provided initial filters', () => {
7
+ const { result } = renderHook(() => useDataViewFilters({ initialFilters }));
8
+ expect(result.current.filters).toEqual(initialFilters);
9
+ });
10
+ it('should initialize with empty filters if no initialFilters provided', () => {
11
+ const { result } = renderHook(() => useDataViewFilters({}));
12
+ expect(result.current.filters).toEqual({});
13
+ });
14
+ it('should set filters correctly', () => {
15
+ const { result } = renderHook(() => useDataViewFilters({ initialFilters }));
16
+ const newFilters = { search: 'new search' };
17
+ act(() => result.current.onSetFilters(newFilters));
18
+ expect(result.current.filters).toEqual(Object.assign(Object.assign({}, initialFilters), newFilters));
19
+ });
20
+ it('should delete specific filters without removing keys', () => {
21
+ const { result } = renderHook(() => useDataViewFilters({ initialFilters }));
22
+ const filtersToDelete = { search: 'test' };
23
+ act(() => result.current.onDeleteFilters(filtersToDelete));
24
+ expect(result.current.filters).toEqual({ search: '', tags: ['tag1', 'tag2'] });
25
+ });
26
+ it('should clear all filters', () => {
27
+ const { result } = renderHook(() => useDataViewFilters({ initialFilters }));
28
+ act(() => result.current.clearAllFilters());
29
+ expect(result.current.filters).toEqual({ search: '', tags: [] });
30
+ });
31
+ it('should sync with URL search params if isUrlSyncEnabled', () => {
32
+ const searchParams = new URLSearchParams();
33
+ const setSearchParams = jest.fn();
34
+ const props = {
35
+ initialFilters,
36
+ searchParams,
37
+ setSearchParams,
38
+ };
39
+ const { result } = renderHook(() => useDataViewFilters(props));
40
+ act(() => result.current.onSetFilters({ search: 'new search' }));
41
+ expect(setSearchParams).toHaveBeenCalled();
42
+ });
43
+ it('should reset filters to default values when clearAllFilters is called', () => {
44
+ const { result } = renderHook(() => useDataViewFilters({ initialFilters }));
45
+ act(() => result.current.clearAllFilters());
46
+ expect(result.current.filters).toEqual({ search: '', tags: [] });
47
+ });
48
+ });
@@ -1,2 +1,3 @@
1
1
  export * from './pagination';
2
2
  export * from './selection';
3
+ export * from './filters';
@@ -1,2 +1,3 @@
1
1
  export * from './pagination';
2
2
  export * from './selection';
3
+ export * from './filters';
@@ -16,6 +16,7 @@ export interface UseDataViewPaginationProps {
16
16
  /** Custom URL parameter name for per page */
17
17
  perPageParam?: string;
18
18
  }
19
+ /** extends UseDataViewPaginationProps */
19
20
  export interface DataViewPaginationProps extends UseDataViewPaginationProps {
20
21
  /** Current page number */
21
22
  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;
@@ -1,7 +1,9 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  import { useState } from "react";
3
- export const useDataViewSelection = ({ matchOption, initialSelected = [] }) => {
4
- const [selected, setSelected] = useState(initialSelected);
3
+ export const useDataViewSelection = (props) => {
4
+ var _a;
5
+ const [selected, setSelected] = useState((_a = props === null || props === void 0 ? void 0 : props.initialSelected) !== null && _a !== void 0 ? _a : []);
6
+ const matchOption = (props === null || props === void 0 ? void 0 : props.matchOption) ? props.matchOption : (option, another) => (option === another);
5
7
  const onSelect = (isSelecting, items) => {
6
8
  isSelecting && items ?
7
9
  setSelected(prev => {
@@ -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';
package/dist/esm/index.js CHANGED
@@ -4,6 +4,8 @@ export * from './InternalContext';
4
4
  export * from './Hooks';
5
5
  export { default as DataViewToolbar } from './DataViewToolbar';
6
6
  export * from './DataViewToolbar';
7
+ export { default as DataViewTextFilter } from './DataViewTextFilter';
8
+ export * from './DataViewTextFilter';
7
9
  export { default as DataViewTableTree } from './DataViewTableTree';
8
10
  export * from './DataViewTableTree';
9
11
  export { default as DataViewTableHead } from './DataViewTableHead';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patternfly/react-data-view",
3
- "version": "5.4.1",
3
+ "version": "5.5.1",
4
4
  "description": "Data view used for Red Hat projects.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",