From f2e4b4a706fe2b3425ef3b300e21a8a2415441e7 Mon Sep 17 00:00:00 2001 From: Andrew Patton Date: Tue, 19 Nov 2024 14:44:13 -0800 Subject: [PATCH] =?UTF-8?q?Add=20support=20for=20=E2=8C=98-Enter=20to=20su?= =?UTF-8?q?bmit=20in=20InputText?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/input-text/src/InputText.tsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/input-text/src/InputText.tsx b/packages/input-text/src/InputText.tsx index 1560329f..16cdb3c6 100644 --- a/packages/input-text/src/InputText.tsx +++ b/packages/input-text/src/InputText.tsx @@ -211,8 +211,9 @@ export default React.forwardRef(function InputText( (event: React.KeyboardEvent) => { if (onKeyDown) onKeyDown(event); if ( - submitOnEnter && event.key === 'Enter' && + // for multi-line inputs, ⌘-Enter should always submit + (submitOnEnter || (multiLine && isPrimaryModifierPressed(event))) && // for multi-line inputs, shift/alt/ctrl-Enter should insert newlines (!multiLine || (!event.shiftKey && !event.altKey && !event.ctrlKey)) ) { @@ -276,3 +277,10 @@ export default React.forwardRef(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; +}