Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(PowerSearch): clear all filters with one press (#6417)

zakelstorm855cc9562805
diff --git a/.changeset/powersearch-clear-all-filters.md b/.changeset/powersearch-clear-all-filters.md
new file mode 100644
index 0000000000..af57a2b446
--- /dev/null
+++ b/.changeset/powersearch-clear-all-filters.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] PowerSearch: clear every filter with one press of the clear-all button instead of removing only the last filter.
+
+@korkt-kim
diff --git a/packages/core/src/PowerSearch/PowerSearch.test.tsx b/packages/core/src/PowerSearch/PowerSearch.test.tsx
index 4fea3252ee..6a7f57b78a 100644
--- a/packages/core/src/PowerSearch/PowerSearch.test.tsx
+++ b/packages/core/src/PowerSearch/PowerSearch.test.tsx
@@ -165,6 +165,91 @@ describe('PowerSearch', () => {
expect(screen.getByRole('combobox')).toHaveFocus();
});
+ it('clears all added filters when the clear button is clicked', async () => {
+ const user = userEvent.setup();
+ render(<PowerSearchWrapper config={config} />);
+ const input = screen.getByRole('combobox', {name: 'Search'});
+ await user.type(input, 'status open');
+ await user.click(
+ await screen.findByRole('option', {name: 'Status is Open', hidden: true}),
+ );
+ await user.type(input, 'title login');
+ await user.click(
+ await screen.findByRole('option', {
+ name: 'Title contains "login"',
+ hidden: true,
+ }),
+ );
+
+ await user.type(input, 'status closed');
+ await user.click(
+ await screen.findByRole('option', {
+ name: 'Status is Closed',
+ hidden: true,
+ }),
+ );
+
+ expect(screen.getAllByRole('button', {name: /^Remove /})).toHaveLength(3);
+
+ await user.click(screen.getByRole('button', {name: 'Clear all'}));
+
+ expect(screen.queryAllByRole('button', {name: /^Remove /})).toHaveLength(0);
+ expect(
+ screen.queryByRole('button', {name: 'Clear all'}),
+ ).not.toBeInTheDocument();
+ });
+
+ it('clicking the remove button on a filter removes only that filter', async () => {
+ const user = userEvent.setup();
+ render(<PowerSearchWrapper config={config} />);
+ const input = screen.getByRole('combobox', {name: 'Search'});
+ await user.type(input, 'status open');
+ await user.click(
+ await screen.findByRole('option', {name: 'Status is Open', hidden: true}),
+ );
+ await user.type(input, 'title login');
+ await user.click(
+ await screen.findByRole('option', {
+ name: 'Title contains "login"',
+ hidden: true,
+ }),
+ );
+
+ expect(screen.getAllByRole('button', {name: /^Remove /})).toHaveLength(2);
+
+ await user.click(
+ screen.getAllByRole('button', {name: 'Remove Status: is'})[0],
+ );
+
+ expect(screen.getAllByRole('button', {name: /^Remove /})).toHaveLength(1);
+ expect(screen.getByText('login')).toBeInTheDocument();
+ });
+
+ it('Backspace on an empty input removes only the last filter', async () => {
+ const user = userEvent.setup();
+ render(<PowerSearchWrapper config={config} />);
+ const input = screen.getByRole('combobox', {name: 'Search'});
+ await user.type(input, 'status open');
+ await user.click(
+ await screen.findByRole('option', {name: 'Status is Open', hidden: true}),
+ );
+ await user.type(input, 'title login');
+ await user.click(
+ await screen.findByRole('option', {
+ name: 'Title contains "login"',
+ hidden: true,
+ }),
+ );
+
+ expect(screen.getAllByRole('button', {name: /^Remove /})).toHaveLength(2);
+
+ await user.click(input);
+ await user.keyboard('{Backspace}');
+
+ expect(screen.getAllByRole('button', {name: /^Remove /})).toHaveLength(1);
+ expect(screen.getByText('Open')).toBeInTheDocument();
+ });
+
describe('token value truncation (#4759)', () => {
it('truncates token values by characters, not code units', () => {
const truncConfig: PowerSearchConfig = {
diff --git a/packages/core/src/PowerSearch/PowerSearch.tsx b/packages/core/src/PowerSearch/PowerSearch.tsx
index e6772db070..598696810f 100644
--- a/packages/core/src/PowerSearch/PowerSearch.tsx
+++ b/packages/core/src/PowerSearch/PowerSearch.tsx
@@ -733,8 +733,8 @@ export function PowerSearch({
} else if (change.type === 'remove' && change.item) {
const auxData = change.item.auxiliaryData as PowerSearchAuxData;
if (auxData?.filterIndex != null) {
- const newFilters = filters.filter(
- (_, i) => i !== auxData.filterIndex,
+ const newFilters = filters.filter((_, i) =>
+ _items.some(item => item.auxiliaryData?.filterIndex === i),
);
onChange(newFilters, 'remove', auxData.filterIndex);
}