Skip to content

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

TypeScript

fix(PowerSearch)!: save an empty-operator filter from the choice, once (#7347)

Cindy Zhang89208bb10543
diff --git a/.changeset/powersearch-empty-operator-single-save.md b/.changeset/powersearch-empty-operator-single-save.md
new file mode 100644
index 0000000000..2002828f29
--- /dev/null
+++ b/.changeset/powersearch-empty-operator-single-save.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] The PowerSearch filter editor saves an "empty" operator filter (such as "is empty") exactly once, from the choice that completes it. Choosing such an operator, or switching to a field whose default operator is one, now saves from that choice instead of from an effect. The filter is no longer saved again when the parent re-renders with a new `onSave`, or twice in React StrictMode. Re-choosing the current operator or field saves nothing, and focus still returns to the search input after the editor closes. Clicking an existing empty-operator token in PowerSearch commits it once without opening the built-in editor; a `components.empty.Editor` override still opens.
+
+@cixzhang
diff --git a/.changeset/powersearch-filter-editor-no-save-on-open.md b/.changeset/powersearch-filter-editor-no-save-on-open.md
new file mode 100644
index 0000000000..0312f79801
--- /dev/null
+++ b/.changeset/powersearch-filter-editor-no-save-on-open.md
@@ -0,0 +1,13 @@
+---
+'@astryxdesign/core': minor
+---
+
+[breaking] `PowerSearchFilterEditor` no longer calls `onSave` when it opens on an already-complete empty-operator filter (such as "is empty").
+
+Old usage: opening a standalone `PowerSearchFilterEditor` on a complete empty-operator filter saved that filter immediately, with no Apply rendered for it, and saved it again whenever the editor remounted.
+
+Replacement: the editor saves only from a user choice in it. Opening it saves nothing.
+
+Migration: if you relied on that save-on-open, call your own `onSave` handler with the filter at the point where you open the editor (or skip opening the editor for empty-operator filters, as PowerSearch now does). This cannot be rewritten by a codemod, because the right place to commit depends on your code.
+
+@cixzhang
diff --git a/packages/core/src/PowerSearch/PowerSearch.tsx b/packages/core/src/PowerSearch/PowerSearch.tsx
index 43ef97a01d..e6772db070 100644
--- a/packages/core/src/PowerSearch/PowerSearch.tsx
+++ b/packages/core/src/PowerSearch/PowerSearch.tsx
@@ -755,6 +755,25 @@ export function PowerSearch({
return;
}
+ // An "empty" operator filter has no value for the built-in editor to
+ // edit: commit it as that editor's save would, the way adding one does,
+ // instead of opening it. A consumer's `empty` Editor override still opens.
+ const operator = config.getOperator(filter.field, filter.operator);
+ if (
+ operator?.value.type === 'empty' &&
+ componentOverrides?.empty?.Editor == null
+ ) {
+ const newFilters = [...filters];
+ newFilters[index] = {
+ field: filter.field,
+ operator: filter.operator,
+ value: {type: 'empty'},
+ };
+ onChange(newFilters, 'edit', index);
+ setPopoverState({type: 'idle'});
+ return;
+ }
+
setPopoverState({
type: 'editing',
filterIndex: index,
@@ -765,7 +784,15 @@ export function PowerSearch({
},
});
},
- [filters, isReadOnly, isDisabled, setPopoverState],
+ [
+ filters,
+ isReadOnly,
+ isDisabled,
+ config,
+ componentOverrides,
+ onChange,
+ setPopoverState,
+ ],
);
// Handle popover save
diff --git a/packages/core/src/PowerSearch/PowerSearchEditPopover.test.tsx b/packages/core/src/PowerSearch/PowerSearchEditPopover.test.tsx
index 09667a948f..df5f38c06d 100644
--- a/packages/core/src/PowerSearch/PowerSearchEditPopover.test.tsx
+++ b/packages/core/src/PowerSearch/PowerSearchEditPopover.test.tsx
@@ -826,3 +826,287 @@ describe('value save', () => {
});
});
});
+
+describe('empty-operator save', () => {
+ const emptyConfig: PowerSearchConfig = {
+ name: 'test-empty',
+ fields: [
+ {
+ key: 'title',
+ label: 'Title',
+ operators: [
+ {key: 'contains', label: 'contains', value: {type: 'string'}},
+ {key: 'is_empty', label: 'is empty', value: {type: 'empty'}},
+ ],
+ },
+ {
+ key: 'assignee',
+ label: 'Assignee',
+ defaultOperator: 'is_unset',
+ operators: [
+ {key: 'is_unset', label: 'is unset', value: {type: 'empty'}},
+ {key: 'is', label: 'is', value: {type: 'string'}},
+ ],
+ },
+ ],
+ };
+
+ type HarnessProps = {
+ filter: React.ComponentProps<typeof PowerSearchEditPopover>['filter'];
+ onSave: (filter: PowerSearchFilter | null) => void;
+ };
+
+ // A parent that keeps the editor mounted and re-renders with a fresh
+ // onSave identity on every render, as an un-memoized consumer does.
+ function Harness({filter, onSave}: HarnessProps) {
+ const config = useInternalConfig(emptyConfig);
+ const [, setTick] = useState(0);
+ return (
+ <>
+ <button type="button" onClick={() => setTick(n => n + 1)}>
+ Rerender
+ </button>
+ <PowerSearchEditPopover
+ config={config}
+ filter={filter}
+ mode="edit"
+ onSave={saved => onSave(saved)}
+ onCancel={() => {}}
+ />
+ </>
+ );
+ }
+
+ // The save is queued as a microtask after the choice; flush it.
+ const selectOption = async (comboboxName: string, optionName: string) => {
+ fireEvent.click(screen.getByRole('combobox', {name: comboboxName}));
+ fireEvent.click(
+ screen.getByRole('option', {name: optionName, hidden: true}),
+ );
+ await act(async () => {});
+ };
+
+ it('saves exactly once when an empty operator is chosen', async () => {
+ const onSave = vi.fn();
+ render(
+ <React.StrictMode>
+ <Harness
+ filter={{field: 'title', operator: 'contains', value: undefined}}
+ onSave={onSave}
+ />
+ </React.StrictMode>,
+ );
+ expect(onSave).not.toHaveBeenCalled();
+
+ await selectOption('Operator', 'is empty');
+ fireEvent.click(screen.getByRole('button', {name: 'Rerender'}));
+ fireEvent.click(screen.getByRole('button', {name: 'Rerender'}));
+
+ expect(onSave).toHaveBeenCalledTimes(1);
+ expect(onSave).toHaveBeenCalledWith({
+ field: 'title',
+ operator: 'is_empty',
+ value: {type: 'empty'},
+ });
+ });
+
+ it('saves exactly once when the field switches to one whose default operator is empty', async () => {
+ const onSave = vi.fn();
+ render(
+ <React.StrictMode>
+ <Harness
+ filter={{field: 'title', operator: 'contains', value: undefined}}
+ onSave={onSave}
+ />
+ </React.StrictMode>,
+ );
+
+ await selectOption('Field', 'Assignee');
+ fireEvent.click(screen.getByRole('button', {name: 'Rerender'}));
+
+ expect(onSave).toHaveBeenCalledTimes(1);
+ expect(onSave).toHaveBeenCalledWith({
+ field: 'assignee',
+ operator: 'is_unset',
+ value: {type: 'empty'},
+ });
+ });
+
+ it('does not save again when the current empty operator or field is re-chosen', async () => {
+ const onSave = vi.fn();
+ render(
+ <React.StrictMode>
+ <Harness
+ filter={{field: 'title', operator: 'contains', value: undefined}}
+ onSave={onSave}
+ />
+ </React.StrictMode>,
+ );
+
+ await selectOption('Operator', 'is empty');
+ expect(onSave).toHaveBeenCalledTimes(1);
+ await selectOption('Operator', 'is empty');
+ expect(onSave).toHaveBeenCalledTimes(1);
+
+ await selectOption('Field', 'Assignee');
+ expect(onSave).toHaveBeenCalledTimes(2);
+ await selectOption('Field', 'Assignee');
+ expect(onSave).toHaveBeenCalledTimes(2);
+ });
+
+ it('does not save just because it opens on a complete empty-operator filter', async () => {
+ const onSave = vi.fn();
+ render(
+ <React.StrictMode>
+ <Harness
+ filter={{field: 'title', operator: 'is_empty', value: undefined}}
+ onSave={onSave}
+ />
+ </React.StrictMode>,
+ );
+ fireEvent.click(screen.getByRole('button', {name: 'Rerender'}));
+ await act(async () => {});
+
+ expect(onSave).not.toHaveBeenCalled();
+ });
+});
+
+describe('empty-operator choices inside PowerSearch', () => {
+ const emptyConfig: PowerSearchConfig = {
+ name: 'test-empty-ps',
+ fields: [
+ {
+ key: 'title',
+ label: 'Title',
+ operators: [
+ {key: 'contains', label: 'contains', value: {type: 'string'}},
+ {key: 'is_empty', label: 'is empty', value: {type: 'empty'}},
+ ],
+ },
+ ],
+ };
+
+ function Harness({
+ initial,
+ onChange,
+ }: {
+ initial: PowerSearchFilter[];
+ onChange: (
+ filters: ReadonlyArray<PowerSearchFilter>,
+ type: string,
+ index: number,
+ ) => void;
+ }) {
+ const [filters, setFilters] = useState(initial);
+ return (
+ <PowerSearch
+ config={emptyConfig}
+ filters={filters}
+ onChange={(next, type, index) => {
+ onChange(next, type, index);
+ setFilters([...next]);
+ }}
+ />
+ );
+ }
+
+ it('returns focus to the search input after an empty operator is chosen', async () => {
+ const onChange = vi.fn();
+ render(
+ <Harness
+ initial={[
+ {
+ field: 'title',
+ operator: 'contains',
+ value: {type: 'string', value: 'x'},
+ },
+ ]}
+ onChange={onChange}
+ />,
+ );
+ act(() => {
+ fireEvent.click(screen.getByText('Title: contains'));
+ flushRAF();
+ });
+ fireEvent.click(
+ screen.getByRole('combobox', {name: 'Operator', hidden: true}),
+ );
+ fireEvent.click(
+ screen.getByRole('option', {name: 'is empty', hidden: true}),
+ );
+ await act(async () => {});
+ act(() => flushRAF());
+
+ expect(onChange).toHaveBeenCalledTimes(1);
+ expect(onChange.mock.calls[0][1]).toBe('edit');
+ expect(document.activeElement).not.toBe(document.body);
+ expect(screen.getAllByRole('combobox')[0]).toHaveFocus();
+ });
+
+ it('commits a clicked empty-operator token once without opening the editor', () => {
+ const onChange = vi.fn();
+ const {container} = render(
+ <Harness
+ initial={[
+ {field: 'title', operator: 'is_empty', value: {type: 'empty'}},
+ ]}
+ onChange={onChange}
+ />,
+ );
+ act(() => {
+ fireEvent.click(screen.getByText(/Title: is empty/));
+ flushRAF();
+ });
+
+ expect(onChange).toHaveBeenCalledTimes(1);
+ expect(onChange).toHaveBeenCalledWith(
+ [{field: 'title', operator: 'is_empty', value: {type: 'empty'}}],
+ 'edit',
+ 0,
+ );
+ expect(getEditPopoverText(container)).toBe('');
+ });
+});
+
+describe('empty-operator token with an Editor override', () => {
+ it('opens the consumer empty Editor instead of committing', () => {
+ const onChange = vi.fn();
+ const EmptyEditor = ({onCancel}: {onCancel: () => void}) => (
+ <div data-testid="custom-empty-editor">
+ <button type="button" onClick={onCancel}>
+ Close custom
+ </button>
+ </div>
+ );
+ const overrideConfig: PowerSearchConfig = {
+ name: 'test-empty-override',
+ fields: [
+ {
+ key: 'title',
+ label: 'Title',
+ operators: [
+ {key: 'contains', label: 'contains', value: {type: 'string'}},
+ {key: 'is_empty', label: 'is empty', value: {type: 'empty'}},
+ ],
+ },
+ ],
+ };
+ render(
+ <PowerSearch
+ config={overrideConfig}
+ filters={[
+ {field: 'title', operator: 'is_empty', value: {type: 'empty'}},
+ ]}
+ onChange={onChange}
+ components={{empty: {Editor: EmptyEditor}}}
+ />,
+ );
+ act(() => {
+ fireEvent.click(screen.getByText(/Title: is empty/));
+ flushRAF();
+ });
+
+ expect(screen.getByTestId('custom-empty-editor')).toBeInTheDocument();
+ expect(onChange).not.toHaveBeenCalled();
+ });
+});
diff --git a/packages/core/src/PowerSearch/PowerSearchEditPopover.tsx b/packages/core/src/PowerSearch/PowerSearchEditPopover.tsx
index 6d68addd92..b390779fe3 100644
--- a/packages/core/src/PowerSearch/PowerSearchEditPopover.tsx
+++ b/packages/core/src/PowerSearch/PowerSearchEditPopover.tsx
@@ -33,6 +33,7 @@ import type {
PartialFilter,
FilterValue,
OperatorValue,
+ PowerSearchOperator,
} from './types';
// Below the popover layer's 400px floor (see popoverLayerStyles in
@@ -689,6 +690,26 @@ export function PowerSearchEditPopover({
}));
}, [config, partialFilter.field, t]);
+ // An "empty" operator takes no value, so choosing one completes the filter:
+ // save it from the choice itself, once, rather than reacting to the state.
+ // The save is queued as a microtask so it runs after the Selector reporting
+ // the choice has finished closing and refocusing its trigger; a parent that
+ // closes the editor on save can then move focus without the Selector taking
+ // it back onto a node that is about to unmount.
+ const saveIfEmptyOperator = useCallback(
+ (fieldKey: string, operator: PowerSearchOperator | undefined) => {
+ if (fieldKey && operator?.value.type === 'empty') {
+ const filter: PowerSearchFilter = {
+ field: fieldKey,
+ operator: operator.key,
+ value: {type: 'empty'},
+ };
+ queueMicrotask(() => onSave(filter));
+ }
+ },
+ [onSave],
+ );
+
const handleFieldChange = useCallback(
(fieldKey: string) => {
const defaultOp = config.getDefaultOperator(fieldKey);
@@ -697,8 +718,16 @@ export function PowerSearchEditPopover({
operator: defaultOp?.key,
value: undefined,
});
+ // Re-choosing the current field with its current operator changes
+ // nothing, so it saves nothing.
+ if (
+ fieldKey !== partialFilter.field ||
+ defaultOp?.key !== partialFilter.operator
+ ) {
+ saveIfEmptyOperator(fieldKey, defaultOp);
+ }
},
- [config],
+ [config, partialFilter.field, partialFilter.operator, saveIfEmptyOperator],
);
const handleOperatorChange = useCallback(
@@ -712,8 +741,18 @@ export function PowerSearchEditPopover({
operator: operatorKey,
value: keepValue ? prev.value : undefined,
}));
+ // Re-choosing the current operator changes nothing, so it saves nothing.
+ if (operatorKey !== partialFilter.operator) {
+ saveIfEmptyOperator(partialFilter.field, newOp);
+ }
},
- [config, partialFilter.field, currentOperator],
+ [
+ config,
+ partialFilter.field,
+ partialFilter.operator,
+ currentOperator,
+ saveIfEmptyOperator,
+ ],
);
const handleValueChange = useCallback(
@@ -769,17 +808,6 @@ export function PowerSearchEditPopover({
const isEmptyType = operatorValue?.type === 'empty';
const isNestedType = operatorValue?.type === 'nested';
- // For empty type, auto-save on mount
- useEffect(() => {
- if (isEmptyType && partialFilter.field && partialFilter.operator) {
- onSave({
- field: partialFilter.field,
- operator: partialFilter.operator,
- value: {type: 'empty'},
- });
- }
- }, [isEmptyType, partialFilter.field, partialFilter.operator, onSave]);
-
const showOperatorSelector = operatorOptions.length > 1 || !isEmptyType;
// Nested filter editing