Skip to content

Commit

Permalink
Add TextField.onEscapeBubble.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenh committed Aug 30, 2024
1 parent e111a76 commit 8fc9a90
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/inputs/TextField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ export interface TextFieldProps<X> extends BeamTextFieldProps<X> {
clearable?: boolean;
api?: MutableRefObject<TextFieldApi | undefined>;
onEnter?: VoidFunction;
/**
* Allows a TextField to opt-in to bubbling up the escape key event to its parent.
*
* Usually this is a bad idea, because escape-in-a-modal might lose the user's WIP (without
* sufficient "are you sure" checking), and so instead we let callers opt-in to this.
*
* Note that react-aria's `useSearchField` / `useComboBox` seems to have this built-in:
* https://github.com/adobe/react-spectrum/issues/5480
*/
onEscapeBubble?: boolean;
endAdornment?: ReactNode;
startAdornment?: ReactNode;
hideErrorMessage?: boolean;
Expand All @@ -28,6 +38,7 @@ export function TextField<X extends Only<TextFieldXss, X>>(props: TextFieldProps
onFocus,
api,
onEnter,
onEscapeBubble,
hideErrorMessage,
...otherProps
} = props;
Expand All @@ -50,6 +61,9 @@ export function TextField<X extends Only<TextFieldXss, X>>(props: TextFieldProps
if (e.key === "Enter") {
maybeCall(onEnter);
inputRef.current?.blur();
} else if (e.key === "Escape" && onEscapeBubble) {
// Allow closing modals from within text fields...
e.continuePropagation();
}
},
},
Expand Down

0 comments on commit 8fc9a90

Please sign in to comment.