Skip to content

Commit 11bf6c7

Browse files
authored
feat(filters): use chipTitle prop for filter chip category (#633)
* feat(DataViewTextFilter): use chipTitle prop for filter chip category AssistedBy: Cursor * feat(DataViewCheckboxFilter): use chipTitle prop for filter chip category * feat(DataViewTreeFilter): use chipTitle prop for filter chip category * chore: add chipTitle prop example
1 parent 8ad3414 commit 11bf6c7

7 files changed

Lines changed: 56 additions & 13 deletions

File tree

packages/module/patternfly-docs/content/extensions/data-view/examples/Toolbar/FiltersExample.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const MyTable: React.FunctionComponent = () => {
7878
}
7979
filters={
8080
<DataViewFilters onChange={(_e, values) => onSetFilters(values)} values={filters}>
81-
<DataViewTextFilter filterId="name" title='Name' placeholder='Filter by name' />
81+
<DataViewTextFilter filterId="name" title='Name' chipTitle='Repo' placeholder='Filter by name' />
8282
<DataViewTextFilter filterId="branch" title='Branch' placeholder='Filter by branch' />
8383
<DataViewCheckboxFilter filterId="workspace" title='Workspace' placeholder='Filter by workspace' options={filterOptions} />
8484
</DataViewFilters>
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
1-
import { render } from '@testing-library/react';
1+
import { render, screen } from '@testing-library/react';
22
import DataViewCheckboxFilter, { DataViewCheckboxFilterProps } from './DataViewCheckboxFilter';
33
import DataViewToolbar from '../DataViewToolbar';
4+
import '@testing-library/jest-dom';
45

56
describe('DataViewCheckboxFilter component', () => {
67
const defaultProps: DataViewCheckboxFilterProps = {
78
filterId: 'test-checkbox-filter',
89
title: 'Test Checkbox Filter',
9-
value: [ 'workspace-one' ],
10+
value: ['workspace-one'],
1011
options: [
1112
{ label: 'Workspace one', value: 'workspace-one' },
1213
{ label: 'Workspace two', value: 'workspace-two' },
13-
{ label: 'Workspace three', value: 'workspace-three' },
14-
],
14+
{ label: 'Workspace three', value: 'workspace-three' }
15+
]
1516
};
1617

1718
it('should render correctly', () => {
18-
const { container } = render(
19-
<DataViewToolbar filters={<DataViewCheckboxFilter {...defaultProps} />} />
20-
);
19+
const { container } = render(<DataViewToolbar filters={<DataViewCheckboxFilter {...defaultProps} />} />);
2120
expect(container).toMatchSnapshot();
2221
});
22+
23+
it('should use chipTitle for the filter chip category when provided', () => {
24+
render(
25+
<DataViewToolbar
26+
filters={<DataViewCheckboxFilter {...defaultProps} chipTitle="Short name" />}
27+
/>
28+
);
29+
expect(screen.getByText('Short name')).toBeInTheDocument();
30+
expect(screen.getByText('Test Checkbox Filter')).toBeInTheDocument();
31+
});
2332
});

packages/module/src/DataViewCheckboxFilter/DataViewCheckboxFilter.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export interface DataViewCheckboxFilterProps extends Omit<MenuProps, 'onSelect'
3232
value?: string[];
3333
/** Filter title displayed in the toolbar */
3434
title: string;
35+
/** Label for the applied filter chip / category; defaults to title */
36+
chipTitle?: string;
3537
/** Placeholder text of the menu */
3638
placeholder?: string;
3739
/** Filter options displayed */
@@ -51,6 +53,7 @@ export interface DataViewCheckboxFilterProps extends Omit<MenuProps, 'onSelect'
5153
export const DataViewCheckboxFilter: FC<DataViewCheckboxFilterProps> = ({
5254
filterId,
5355
title,
56+
chipTitle,
5457
value = [],
5558
onChange,
5659
placeholder,
@@ -65,6 +68,7 @@ export const DataViewCheckboxFilter: FC<DataViewCheckboxFilterProps> = ({
6568
const toggleRef = useRef<HTMLButtonElement>(null);
6669
const menuRef = useRef<HTMLDivElement>(null);
6770
const containerRef = useRef<HTMLDivElement>(null);
71+
const categoryName = chipTitle ?? title;
6872

6973
const normalizeOptions = useMemo(
7074
() =>
@@ -120,7 +124,7 @@ export const DataViewCheckboxFilter: FC<DataViewCheckboxFilterProps> = ({
120124
deleteLabel={(_, label) =>
121125
onChange?.(undefined, value.filter(item => item !== (isToolbarLabel(label) ? label.key : label)))
122126
}
123-
categoryName={title}
127+
categoryName={categoryName}
124128
showToolbarItem={showToolbarItem}
125129
>
126130
<Popper

packages/module/src/DataViewTextFilter/DataViewTextFilter.test.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render } from '@testing-library/react';
1+
import { render, screen } from '@testing-library/react';
22
import '@testing-library/jest-dom';
33
import DataViewTextFilter, { DataViewTextFilterProps } from './DataViewTextFilter';
44
import DataViewToolbar from '../DataViewToolbar';
@@ -22,6 +22,16 @@ describe('DataViewTextFilter component', () => {
2222
expect(container).toMatchSnapshot();
2323
});
2424

25+
it('should use chipTitle for the filter chip category when provided', () => {
26+
render(<DataViewToolbar
27+
filters={
28+
<DataViewTextFilter {...defaultProps} chipTitle="Short name" />
29+
}
30+
/>);
31+
expect(screen.getByText('Short name')).toBeInTheDocument();
32+
expect(screen.getByPlaceholderText('Filter by Test Filter')).toBeInTheDocument();
33+
});
34+
2535
it('should focus the search input when "/" key is pressed and filter is visible', () => {
2636
render(<DataViewToolbar
2737
filters={

packages/module/src/DataViewTextFilter/DataViewTextFilter.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export interface DataViewTextFilterProps extends SearchInputProps {
99
value?: string;
1010
/** Filter title displayed in the toolbar */
1111
title: string;
12+
/** Label for the applied filter chip / category; defaults to title */
13+
chipTitle?: string;
1214
/** Callback for when the input value changes */
1315
onChange?: (event: React.FormEvent<HTMLInputElement> | undefined, value: string) => void;
1416
/** Controls visibility of the filter in the toolbar */
@@ -24,6 +26,7 @@ export interface DataViewTextFilterProps extends SearchInputProps {
2426
export const DataViewTextFilter: FC<DataViewTextFilterProps> = ({
2527
filterId,
2628
title,
29+
chipTitle,
2730
value = '',
2831
onChange,
2932
onClear = () => onChange?.(undefined, ''),
@@ -33,6 +36,8 @@ export const DataViewTextFilter: FC<DataViewTextFilterProps> = ({
3336
enableShortcut = true,
3437
...props
3538
}: DataViewTextFilterProps) => {
39+
const categoryName = chipTitle ?? title;
40+
3641
useEffect(() => {
3742
if (!enableShortcut) {
3843
return;
@@ -68,9 +73,9 @@ export const DataViewTextFilter: FC<DataViewTextFilterProps> = ({
6873
<ToolbarFilter
6974
key={ouiaId}
7075
data-ouia-component-id={ouiaId}
71-
labels={value.length > 0 ? [ { key: title, node: value } ] : []}
76+
labels={value.length > 0 ? [ { key: categoryName, node: value } ] : []}
7277
deleteLabel={() => onChange?.(undefined, '')}
73-
categoryName={title}
78+
categoryName={categoryName}
7479
showToolbarItem={showToolbarItem}
7580
>
7681
<SearchInput

packages/module/src/DataViewTreeFilter/DataViewTreeFilter.test.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,17 @@ describe('DataViewTreeFilter component', () => {
8787
);
8888
expect(container).toMatchSnapshot();
8989
});
90+
91+
it('should use chipTitle for the filter chip category when provided', () => {
92+
render(
93+
<DataViewToolbar
94+
filters={<DataViewTreeFilter {...defaultProps} chipTitle="Short name" />}
95+
/>
96+
);
97+
expect(screen.getByText('Short name')).toBeInTheDocument();
98+
expect(screen.getByText('Test Tree Filter')).toBeInTheDocument();
99+
});
100+
90101
describe('defaultExpanded', () => {
91102
it('should have expanded items by default', async () => {
92103
render(

packages/module/src/DataViewTreeFilter/DataViewTreeFilter.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ export interface DataViewTreeFilterProps {
8989
value?: string[];
9090
/** Filter title displayed in the toolbar */
9191
title: string;
92+
/** Label for the applied filter chip / category; defaults to title */
93+
chipTitle?: string;
9294
/** Callback for when the selection changes */
9395
onChange?: (event?: React.MouseEvent, values?: string[]) => void;
9496
/** Controls visibility of the filter in the toolbar */
@@ -108,6 +110,7 @@ export interface DataViewTreeFilterProps {
108110
export const DataViewTreeFilter: FC<DataViewTreeFilterProps> = ({
109111
filterId,
110112
title,
113+
chipTitle,
111114
value = [],
112115
onChange,
113116
showToolbarItem,
@@ -117,6 +120,7 @@ export const DataViewTreeFilter: FC<DataViewTreeFilterProps> = ({
117120
onSelect,
118121
defaultSelected = []
119122
}: DataViewTreeFilterProps) => {
123+
const categoryName = chipTitle ?? title;
120124
const classes = useStyles();
121125
const [isOpen, setIsOpen] = useState(false);
122126
const [treeData, setTreeData] = useState<TreeViewDataItem[]>(items || []);
@@ -351,7 +355,7 @@ export const DataViewTreeFilter: FC<DataViewTreeFilterProps> = ({
351355
onFilterSelectorClear(labelKey);
352356
}}
353357
deleteLabelGroup={uncheckAllItems}
354-
categoryName={title}
358+
categoryName={categoryName}
355359
showToolbarItem={showToolbarItem}>
356360
{dropdown}
357361
</ToolbarFilter>

0 commit comments

Comments
 (0)