@patternfly/react-data-view 5.4.1 → 5.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/DataViewFilters/DataViewFilters.d.ts +19 -0
- package/dist/cjs/DataViewFilters/DataViewFilters.js +70 -0
- package/dist/cjs/DataViewFilters/DataViewFilters.test.d.ts +1 -0
- package/dist/cjs/DataViewFilters/DataViewFilters.test.js +19 -0
- package/dist/cjs/DataViewFilters/index.d.ts +2 -0
- package/dist/cjs/DataViewFilters/index.js +23 -0
- package/dist/cjs/DataViewTable/DataViewTable.d.ts +8 -0
- package/dist/cjs/DataViewTableBasic/DataViewTableBasic.d.ts +1 -0
- package/dist/cjs/DataViewTableHead/DataViewTableHead.d.ts +1 -0
- package/dist/cjs/DataViewTableTree/DataViewTableTree.d.ts +1 -0
- package/dist/cjs/DataViewTextFilter/DataViewTextFilter.d.ts +21 -0
- package/dist/cjs/DataViewTextFilter/DataViewTextFilter.js +26 -0
- package/dist/cjs/DataViewTextFilter/DataViewTextFilter.test.d.ts +1 -0
- package/dist/cjs/DataViewTextFilter/DataViewTextFilter.test.js +22 -0
- package/dist/cjs/DataViewTextFilter/index.d.ts +2 -0
- package/dist/cjs/DataViewTextFilter/index.js +23 -0
- package/dist/cjs/DataViewToolbar/DataViewToolbar.d.ts +10 -4
- package/dist/cjs/DataViewToolbar/DataViewToolbar.js +29 -6
- package/dist/cjs/Hooks/filters.d.ts +14 -0
- package/dist/cjs/Hooks/filters.js +70 -0
- package/dist/cjs/Hooks/filters.test.d.ts +1 -0
- package/dist/cjs/Hooks/filters.test.js +50 -0
- package/dist/cjs/Hooks/index.d.ts +1 -0
- package/dist/cjs/Hooks/index.js +1 -0
- package/dist/cjs/Hooks/pagination.d.ts +1 -0
- package/dist/cjs/Hooks/selection.d.ts +1 -1
- package/dist/cjs/Hooks/selection.js +4 -2
- package/dist/cjs/InternalContext/InternalContext.d.ts +1 -0
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +4 -1
- package/dist/dynamic/DataViewFilters/package.json +1 -0
- package/dist/dynamic/DataViewTextFilter/package.json +1 -0
- package/dist/esm/DataViewFilters/DataViewFilters.d.ts +19 -0
- package/dist/esm/DataViewFilters/DataViewFilters.js +43 -0
- package/dist/esm/DataViewFilters/DataViewFilters.test.d.ts +1 -0
- package/dist/esm/DataViewFilters/DataViewFilters.test.js +14 -0
- package/dist/esm/DataViewFilters/index.d.ts +2 -0
- package/dist/esm/DataViewFilters/index.js +2 -0
- package/dist/esm/DataViewTable/DataViewTable.d.ts +8 -0
- package/dist/esm/DataViewTableBasic/DataViewTableBasic.d.ts +1 -0
- package/dist/esm/DataViewTableHead/DataViewTableHead.d.ts +1 -0
- package/dist/esm/DataViewTableTree/DataViewTableTree.d.ts +1 -0
- package/dist/esm/DataViewTextFilter/DataViewTextFilter.d.ts +21 -0
- package/dist/esm/DataViewTextFilter/DataViewTextFilter.js +19 -0
- package/dist/esm/DataViewTextFilter/DataViewTextFilter.test.d.ts +1 -0
- package/dist/esm/DataViewTextFilter/DataViewTextFilter.test.js +17 -0
- package/dist/esm/DataViewTextFilter/index.d.ts +2 -0
- package/dist/esm/DataViewTextFilter/index.js +2 -0
- package/dist/esm/DataViewToolbar/DataViewToolbar.d.ts +10 -4
- package/dist/esm/DataViewToolbar/DataViewToolbar.js +7 -4
- package/dist/esm/Hooks/filters.d.ts +14 -0
- package/dist/esm/Hooks/filters.js +66 -0
- package/dist/esm/Hooks/filters.test.d.ts +1 -0
- package/dist/esm/Hooks/filters.test.js +48 -0
- package/dist/esm/Hooks/index.d.ts +1 -0
- package/dist/esm/Hooks/index.js +1 -0
- package/dist/esm/Hooks/pagination.d.ts +1 -0
- package/dist/esm/Hooks/selection.d.ts +1 -1
- package/dist/esm/Hooks/selection.js +4 -2
- package/dist/esm/InternalContext/InternalContext.d.ts +1 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -0
- package/package.json +1 -1
- package/patternfly-docs/content/extensions/data-view/examples/Components/Components.md +2 -2
- package/patternfly-docs/content/extensions/data-view/examples/Functionality/FiltersExample.tsx +92 -0
- package/patternfly-docs/content/extensions/data-view/examples/Functionality/Functionality.md +36 -2
- package/src/DataViewFilters/DataViewFilters.test.tsx +21 -0
- package/src/DataViewFilters/DataViewFilters.tsx +125 -0
- package/src/DataViewFilters/__snapshots__/DataViewFilters.test.tsx.snap +182 -0
- package/src/DataViewFilters/index.tsx +2 -0
- package/src/DataViewTable/DataViewTable.tsx +23 -3
- package/src/DataViewTableBasic/DataViewTableBasic.tsx +1 -0
- package/src/DataViewTableHead/DataViewTableHead.tsx +1 -0
- package/src/DataViewTableTree/DataViewTableTree.tsx +1 -0
- package/src/DataViewTextFilter/DataViewTextFilter.test.tsx +24 -0
- package/src/DataViewTextFilter/DataViewTextFilter.tsx +53 -0
- package/src/DataViewTextFilter/__snapshots__/DataViewTextFilter.test.tsx.snap +196 -0
- package/src/DataViewTextFilter/index.ts +2 -0
- package/src/DataViewToolbar/DataViewToolbar.tsx +47 -28
- package/src/DataViewToolbar/__snapshots__/DataViewToolbar.test.tsx.snap +28 -0
- package/src/Hooks/filters.test.tsx +62 -0
- package/src/Hooks/filters.ts +96 -0
- package/src/Hooks/index.ts +1 -0
- package/src/Hooks/pagination.ts +1 -0
- package/src/Hooks/selection.ts +3 -2
- package/src/InternalContext/InternalContext.tsx +1 -0
- package/src/index.ts +3 -0
|
@@ -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
|
+
});
|
|
@@ -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[];
|
|
@@ -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 @@
|
|
|
1
|
+
export {};
|
|
@@ -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
|
+
});
|
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import React, { PropsWithChildren } from 'react';
|
|
2
|
-
|
|
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
|
|
9
|
+
/** React node to display bulk select */
|
|
8
10
|
bulkSelect?: React.ReactNode;
|
|
9
|
-
/** React
|
|
11
|
+
/** React node to display pagination */
|
|
10
12
|
pagination?: React.ReactNode;
|
|
11
|
-
/** React
|
|
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
|
-
|
|
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
|
+
});
|
package/dist/esm/Hooks/index.js
CHANGED
|
@@ -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: (
|
|
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 = (
|
|
4
|
-
|
|
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;
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -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
|
@@ -11,7 +11,7 @@ source: react
|
|
|
11
11
|
# If you use typescript, the name of the interface to display props for
|
|
12
12
|
# These are found through the sourceProps function provided in patternfly-docs.source.js
|
|
13
13
|
sortValue: 4
|
|
14
|
-
propComponents: ['DataViewToolbar', 'DataViewTableBasic', 'DataViewTableTree']
|
|
14
|
+
propComponents: ['DataViewToolbar', 'DataViewTableBasic', 'DataViewTableTree', 'DataViewTrTree', 'DataViewTrObject']
|
|
15
15
|
sourceLink: https://github.com/patternfly/react-data-view/blob/main/packages/module/patternfly-docs/content/extensions/data-view/examples/Components/Components.md
|
|
16
16
|
---
|
|
17
17
|
import { Button, EmptyState, EmptyStateActions, EmptyStateBody, EmptyStateFooter, EmptyStateHeader, EmptyStateIcon } from '@patternfly/react-core';
|
|
@@ -26,7 +26,7 @@ import { DataView, DataViewState } from '@patternfly/react-data-view/dist/dynami
|
|
|
26
26
|
|
|
27
27
|
The **data view toolbar** component renders a default opinionated data view toolbar above or below the data section.
|
|
28
28
|
|
|
29
|
-
Data view toolbar can contain a `pagination`, `bulkSelect`, `actions` or other children content passed. The preffered way of passing children toolbar items is using the [toolbar item](/components/toolbar#toolbar-items) component.
|
|
29
|
+
Data view toolbar can contain a `pagination`, `bulkSelect`, `filters`, `actions` or other children content passed. The preffered way of passing children toolbar items is using the [toolbar item](/components/toolbar#toolbar-items) component.
|
|
30
30
|
|
|
31
31
|
### Basic toolbar example
|
|
32
32
|
|
package/patternfly-docs/content/extensions/data-view/examples/Functionality/FiltersExample.tsx
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import React, { useMemo } from 'react';
|
|
2
|
+
import { Pagination } from '@patternfly/react-core';
|
|
3
|
+
import { BrowserRouter, useSearchParams } from 'react-router-dom';
|
|
4
|
+
import { useDataViewFilters, useDataViewPagination } from '@patternfly/react-data-view/dist/dynamic/Hooks';
|
|
5
|
+
import { DataView } from '@patternfly/react-data-view/dist/dynamic/DataView';
|
|
6
|
+
import { DataViewTable } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
|
|
7
|
+
import { DataViewToolbar } from '@patternfly/react-data-view/dist/dynamic/DataViewToolbar';
|
|
8
|
+
import { DataViewFilters } from '@patternfly/react-data-view/dist/dynamic/DataViewFilters';
|
|
9
|
+
import { DataViewTextFilter } from '@patternfly/react-data-view/dist/dynamic/DataViewTextFilter';
|
|
10
|
+
|
|
11
|
+
const perPageOptions = [
|
|
12
|
+
{ title: '5', value: 5 },
|
|
13
|
+
{ title: '10', value: 10 }
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
interface Repository {
|
|
17
|
+
name: string;
|
|
18
|
+
branch: string | null;
|
|
19
|
+
prs: string | null;
|
|
20
|
+
workspaces: string;
|
|
21
|
+
lastCommit: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
interface RepositoryFilters {
|
|
25
|
+
name: string,
|
|
26
|
+
branch: string
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const repositories: Repository[] = [
|
|
30
|
+
{ name: 'Repository one', branch: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' },
|
|
31
|
+
{ name: 'Repository two', branch: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' },
|
|
32
|
+
{ name: 'Repository three', branch: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' },
|
|
33
|
+
{ name: 'Repository four', branch: 'Branch four', prs: 'Pull request four', workspaces: 'Workspace four', lastCommit: 'Timestamp four' },
|
|
34
|
+
{ name: 'Repository five', branch: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' },
|
|
35
|
+
{ name: 'Repository six', branch: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' }
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
const columns = [ 'Name', 'Branch', 'Pull requests', 'Workspaces', 'Last commit' ];
|
|
39
|
+
|
|
40
|
+
const ouiaId = 'LayoutExample';
|
|
41
|
+
|
|
42
|
+
const MyTable: React.FunctionComponent = () => {
|
|
43
|
+
const [ searchParams, setSearchParams ] = useSearchParams();
|
|
44
|
+
const pagination = useDataViewPagination({ perPage: 5 });
|
|
45
|
+
const { page, perPage } = pagination;
|
|
46
|
+
const { filters, onSetFilters, clearAllFilters } = useDataViewFilters<RepositoryFilters>({ initialFilters: { name: '', branch: '' }, searchParams, setSearchParams });
|
|
47
|
+
|
|
48
|
+
const pageRows = useMemo(() => repositories
|
|
49
|
+
.filter(item => (!filters.name || item.name?.toLocaleLowerCase().includes(filters.name?.toLocaleLowerCase())) && (!filters.branch || item.branch?.toLocaleLowerCase().includes(filters.branch?.toLocaleLowerCase())))
|
|
50
|
+
.slice((page - 1) * perPage, ((page - 1) * perPage) + perPage)
|
|
51
|
+
.map(item => Object.values(item)), [ page, perPage, filters ]);
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<DataView>
|
|
55
|
+
<DataViewToolbar
|
|
56
|
+
ouiaId='LayoutExampleHeader'
|
|
57
|
+
clearAllFilters = {clearAllFilters}
|
|
58
|
+
pagination={
|
|
59
|
+
<Pagination
|
|
60
|
+
perPageOptions={perPageOptions}
|
|
61
|
+
itemCount={repositories.length}
|
|
62
|
+
{...pagination}
|
|
63
|
+
/>
|
|
64
|
+
}
|
|
65
|
+
filters={
|
|
66
|
+
<DataViewFilters onChange={(_e, values) => onSetFilters(values)} values={filters}>
|
|
67
|
+
<DataViewTextFilter filterId="name" title='Name' placeholder='Filter by name' />
|
|
68
|
+
<DataViewTextFilter filterId="branch" title='Branch' placeholder='Filter by branch' />
|
|
69
|
+
</DataViewFilters>
|
|
70
|
+
}
|
|
71
|
+
/>
|
|
72
|
+
<DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={pageRows} />
|
|
73
|
+
<DataViewToolbar
|
|
74
|
+
ouiaId='LayoutExampleFooter'
|
|
75
|
+
pagination={
|
|
76
|
+
<Pagination
|
|
77
|
+
isCompact
|
|
78
|
+
perPageOptions={perPageOptions}
|
|
79
|
+
itemCount={repositories.length}
|
|
80
|
+
{...pagination}
|
|
81
|
+
/>
|
|
82
|
+
}
|
|
83
|
+
/>
|
|
84
|
+
</DataView>
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export const BasicExample: React.FunctionComponent = () => (
|
|
89
|
+
<BrowserRouter>
|
|
90
|
+
<MyTable/>
|
|
91
|
+
</BrowserRouter>
|
|
92
|
+
)
|