Skip to content

Commit

Permalink
Add support for ⌘-Enter to submit in InputText
Browse files Browse the repository at this point in the history
  • Loading branch information
acusti committed Nov 19, 2024
1 parent 15a7a50 commit f2e4b4a
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/input-text/src/InputText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,9 @@ export default React.forwardRef<HTMLInputElement, Props>(function InputText(
(event: React.KeyboardEvent<HTMLInputElement>) => {
if (onKeyDown) onKeyDown(event);
if (
submitOnEnter &&
event.key === 'Enter' &&
// for multi-line inputs, ⌘-Enter should always submit
(submitOnEnter || (multiLine && isPrimaryModifierPressed(event))) &&

Check failure on line 216 in packages/input-text/src/InputText.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

Argument of type 'KeyboardEvent<HTMLInputElement>' is not assignable to parameter of type 'KeyboardEvent'.

Check failure on line 216 in packages/input-text/src/InputText.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

Argument of type 'KeyboardEvent<HTMLInputElement>' is not assignable to parameter of type 'KeyboardEvent'.
// for multi-line inputs, shift/alt/ctrl-Enter should insert newlines
(!multiLine || (!event.shiftKey && !event.altKey && !event.ctrlKey))
) {
Expand Down Expand Up @@ -276,3 +277,10 @@ export default React.forwardRef<HTMLInputElement, Props>(function InputText(
/>
);
});

const IS_APPLE_REGEXP = /mac|iphone|ipad|ipod/i;

function isPrimaryModifierPressed(event: KeyboardEvent) {
const platform = globalThis.navigator?.platform ?? '';
return IS_APPLE_REGEXP.test(platform) ? event.metaKey : event.ctrlKey;
}

0 comments on commit f2e4b4a

Please sign in to comment.