Skip to content

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

TypeScript

fix(PowerSearch): Enter on a focused editor button activates that button, not Apply (#7500)

Cindy Zhanga11a1a2618a7
diff --git a/.changeset/powersearch-enter-on-focused-button.md b/.changeset/powersearch-enter-on-focused-button.md
new file mode 100644
index 0000000000..8fe27a22b7
--- /dev/null
+++ b/.changeset/powersearch-enter-on-focused-button.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] `PowerSearch`: pressing Enter on a focused button in the filter editor now activates that button. Enter on Cancel closes the editor without applying the draft, and Enter on Delete removes the filter; before, both applied the draft instead. Enter in a text or number value field still applies the filter; Enter on the date picker's calendar button now opens the calendar.
+
+@cixzhang
diff --git a/packages/core/src/PowerSearch/PowerSearchEditPopover.test.tsx b/packages/core/src/PowerSearch/PowerSearchEditPopover.test.tsx
index df5f38c06d..2b7856aa87 100644
--- a/packages/core/src/PowerSearch/PowerSearchEditPopover.test.tsx
+++ b/packages/core/src/PowerSearch/PowerSearchEditPopover.test.tsx
@@ -18,6 +18,7 @@ import {
waitFor,
within,
} from '@testing-library/react';
+import userEvent from '@testing-library/user-event';
import React, {useState} from 'react';
import * as stylex from '@stylexjs/stylex';
import {layerViewportInset} from '../Layer/layerViewportInset.stylex';
@@ -1110,3 +1111,80 @@ describe('empty-operator token with an Editor override', () => {
expect(onChange).not.toHaveBeenCalled();
});
});
+
+describe('Enter on a focused footer button (#7437)', () => {
+ const validFilter: PowerSearchFilter = {
+ field: 'status',
+ operator: 'is',
+ value: {type: 'string', value: 'example'},
+ };
+
+ function Harness({
+ onSave,
+ onCancel,
+ }: {
+ onSave: (filter: PowerSearchFilter | null) => void;
+ onCancel: () => void;
+ }) {
+ const internalConfig = useInternalConfig(testConfig);
+ return (
+ <PowerSearchEditPopover
+ config={internalConfig}
+ filter={validFilter}
+ mode="edit"
+ onSave={onSave}
+ onCancel={onCancel}
+ />
+ );
+ }
+
+ it('cancels without saving when Cancel has focus', async () => {
+ const onSave = vi.fn();
+ const onCancel = vi.fn();
+ const user = userEvent.setup();
+ render(<Harness onSave={onSave} onCancel={onCancel} />);
+
+ screen.getByRole('button', {name: 'Cancel'}).focus();
+ await user.keyboard('{Enter}');
+
+ expect(onCancel).toHaveBeenCalledTimes(1);
+ expect(onSave).not.toHaveBeenCalled();
+ });
+
+ it('deletes the filter when Delete has focus', async () => {
+ const onSave = vi.fn();
+ const user = userEvent.setup();
+ render(<Harness onSave={onSave} onCancel={() => {}} />);
+
+ screen.getByRole('button', {name: 'Delete'}).focus();
+ await user.keyboard('{Enter}');
+
+ expect(onSave).toHaveBeenCalledTimes(1);
+ expect(onSave).toHaveBeenCalledWith(null);
+ });
+
+ it('saves once when Apply has focus', async () => {
+ const onSave = vi.fn();
+ const user = userEvent.setup();
+ render(<Harness onSave={onSave} onCancel={() => {}} />);
+
+ screen.getByRole('button', {name: 'Apply'}).focus();
+ await user.keyboard('{Enter}');
+
+ expect(onSave).toHaveBeenCalledTimes(1);
+ expect(onSave).toHaveBeenCalledWith(validFilter);
+ });
+
+ it('still cancels on Escape when a button has focus', async () => {
+ const onSave = vi.fn();
+ const onCancel = vi.fn();
+ const user = userEvent.setup();
+ render(<Harness onSave={onSave} onCancel={onCancel} />);
+
+ screen.getByRole('button', {name: 'Delete'}).focus();
+ await user.keyboard('{Escape}');
+
+ expect(onCancel).toHaveBeenCalledTimes(1);
+ expect(onSave).not.toHaveBeenCalled();
+ });
+});
diff --git a/packages/core/src/PowerSearch/PowerSearchEditPopover.tsx b/packages/core/src/PowerSearch/PowerSearchEditPopover.tsx
index b390779fe3..156e7a87fd 100644
--- a/packages/core/src/PowerSearch/PowerSearchEditPopover.tsx
+++ b/packages/core/src/PowerSearch/PowerSearchEditPopover.tsx
@@ -793,6 +793,12 @@ export function PowerSearchEditPopover({
if (isImeKeyEvent(e.nativeEvent)) {
return;
}
+ // A focused button owns its Enter: Cancel cancels, Delete deletes, and
+ // the date picker's calendar button opens the calendar. Enter in a
+ // text or number value field still saves.
+ if (e.key === 'Enter' && e.target instanceof HTMLButtonElement) {
+ return;
+ }
if (e.key === 'Enter' && !isSaveDisabled && !e.defaultPrevented) {
e.preventDefault();
handleSave();