Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cypress/component/DataViewFilters.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ describe('DataViewFilters', () => {
cy.get('[aria-label="Close Main branch"]').should('have.length', 0);
});

it('clears all filters using the clear-all button', () => {
it('clears all filters using the clear-all button and resets attribute to first', () => {
cy.mount(<DataViewToolbarWithState />);
cy.get('[data-ouia-component-id="DataViewFilters"] .pf-v6-c-menu-toggle').click();
cy.contains('Name').click();
Expand All @@ -108,6 +108,8 @@ describe('DataViewFilters', () => {
cy.contains('Branch').click();
cy.get('input[placeholder="Filter by branch"]').type('Main branch');

cy.get('[data-ouia-component-id="DataViewFilters"]').should('contain.text', 'Branch');
cy.get('[data-ouia-component-id="FiltersExampleHeader-clear-all-filters"]').should('exist').click();
cy.get('[data-ouia-component-id="DataViewFilters"]').should('contain.text', 'Name');
});
});
74 changes: 74 additions & 0 deletions packages/module/src/DataViewFilters/DataViewFilters.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,78 @@ describe('DataViewFilters component', () => {
fireEvent.input(input, { target: { value: 'abc' } });
expect(mockOnChange).toHaveBeenCalledWith('one', { one: 'abc' });
});

it('should use defaultActiveFilter as initial active filter', () => {
const { container } = render(
<DataViewToolbar
filters={
<DataViewFilters onChange={mockOnChange} values={{ one: '', two: '' }} defaultActiveFilter="two">
<DataViewTextFilter filterId="one" title="One" />
<DataViewTextFilter filterId="two" title="Two" />
</DataViewFilters>
}
/>
);

const toggle = container.querySelector('[data-ouia-component-id="DataViewFilters"] .pf-v6-c-menu-toggle') as HTMLButtonElement;
expect(toggle.textContent).toContain('Two');
});

it('should reset active filter to defaultActiveFilter when all values are cleared', () => {
const { container, rerender } = render(
<DataViewToolbar
filters={
<DataViewFilters onChange={mockOnChange} values={{ one: 'test', two: '' }} defaultActiveFilter="two">
<DataViewTextFilter filterId="one" title="One" />
<DataViewTextFilter filterId="two" title="Two" />
</DataViewFilters>
}
/>
);

const toggle = container.querySelector('[data-ouia-component-id="DataViewFilters"] .pf-v6-c-menu-toggle') as HTMLButtonElement;
expect(toggle.textContent).toContain('Two');

rerender(
<DataViewToolbar
filters={
<DataViewFilters onChange={mockOnChange} values={{ one: '', two: '' }} defaultActiveFilter="two">
<DataViewTextFilter filterId="one" title="One" />
<DataViewTextFilter filterId="two" title="Two" />
</DataViewFilters>
}
/>
);

expect(toggle.textContent).toContain('Two');
});

it('should reset active filter to first item when all values are cleared without defaultActiveFilter', () => {
const { container, rerender } = render(
<DataViewToolbar
filters={
<DataViewFilters onChange={mockOnChange} values={{ one: 'test', two: '' }}>
<DataViewTextFilter filterId="one" title="One" />
<DataViewTextFilter filterId="two" title="Two" />
</DataViewFilters>
}
/>
);

const toggle = container.querySelector('[data-ouia-component-id="DataViewFilters"] .pf-v6-c-menu-toggle') as HTMLButtonElement;
expect(toggle.textContent).toContain('One');

rerender(
<DataViewToolbar
filters={
<DataViewFilters onChange={mockOnChange} values={{ one: '', two: '' }}>
<DataViewTextFilter filterId="one" title="One" />
<DataViewTextFilter filterId="two" title="Two" />
</DataViewFilters>
}
/>
);

expect(toggle.textContent).toContain('One');
});
});
25 changes: 22 additions & 3 deletions packages/module/src/DataViewFilters/DataViewFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Children, isValidElement, cloneElement, useMemo, useState, useRef, useEffect, ReactElement, ReactNode } from 'react';
import { Children, isValidElement, cloneElement, useMemo, useCallback, useState, useRef, useEffect, ReactElement, ReactNode } from 'react';
import {
Menu, MenuContent, MenuItem, MenuList, MenuToggle, Popper, ToolbarGroup, ToolbarToggleGroup, ToolbarToggleGroupProps,
} from '@patternfly/react-core';
Expand Down Expand Up @@ -31,6 +31,8 @@
breakpoint?: ToolbarToggleGroupProps['breakpoint'];
/** Custom OUIA ID */
ouiaId?: string;
/** Optional filterId to use as the default active filter. Falls back to the first filter when not specified. The active filter resets to this value when all filter values are cleared. */
defaultActiveFilter?: string;
};


Expand All @@ -41,6 +43,7 @@
breakpoint = 'xl',
onChange,
values,
defaultActiveFilter,
...props
}: DataViewFiltersProps<T>) => {
const [ activeAttributeMenu, setActiveAttributeMenu ] = useState<string>('');
Expand All @@ -57,12 +60,28 @@

const filterItems: DataViewFilterIdentifiers[] = useMemo(() => Children.toArray(children)
.map(child =>
isValidElement(child) ? { filterId: String((child.props as any).filterId), title: String((child.props as any).title) } : undefined

Check warning on line 63 in packages/module/src/DataViewFilters/DataViewFilters.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

Unexpected any. Specify a different type

Check warning on line 63 in packages/module/src/DataViewFilters/DataViewFilters.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

Unexpected any. Specify a different type
).filter((item): item is DataViewFilterIdentifiers => !!item), [ childrenHash ]); // eslint-disable-line react-hooks/exhaustive-deps

const getDefaultTitle = useCallback(() => {
if (defaultActiveFilter) {
const match = filterItems.find(item => item.filterId === defaultActiveFilter);
if (match) {
return match.title;
}
}
return filterItems.length > 0 ? filterItems[0].title : '';
}, [ defaultActiveFilter, filterItems ]);

useEffect(() => {
setActiveAttributeMenu(getDefaultTitle());
}, [ filterItems, getDefaultTitle ]);

useEffect(() => {
filterItems.length > 0 && setActiveAttributeMenu(filterItems[0].title);
}, [ filterItems ]);
if (values && Object.values(values).every(v => v === '' || v === undefined || (Array.isArray(v) && v.length === 0))) {
setActiveAttributeMenu(getDefaultTitle());
}
}, [ values, getDefaultTitle ]);

const handleClickOutside = (event: MouseEvent) =>
isAttributeMenuOpen &&
Expand Down Expand Up @@ -129,10 +148,10 @@
onChange: (_e: unknown, values: unknown) => void;
value: unknown;
}>, {
showToolbarItem: activeAttributeMenu === (child.props as any).title,

Check warning on line 151 in packages/module/src/DataViewFilters/DataViewFilters.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

Unexpected any. Specify a different type
onChange: (event, value) => onChange?.((child.props as any).filterId, { [(child.props as any).filterId]: value } as Partial<T>),

Check warning on line 152 in packages/module/src/DataViewFilters/DataViewFilters.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

Unexpected any. Specify a different type

Check warning on line 152 in packages/module/src/DataViewFilters/DataViewFilters.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

Unexpected any. Specify a different type
value: values?.[(child.props as any).filterId],

Check warning on line 153 in packages/module/src/DataViewFilters/DataViewFilters.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

Unexpected any. Specify a different type
...(child.props as any)

Check warning on line 154 in packages/module/src/DataViewFilters/DataViewFilters.tsx

View workflow job for this annotation

GitHub Actions / call-build-lint-test-workflow / lint

Unexpected any. Specify a different type
})
: child
)}
Expand Down
Loading