From a2565363f7f6e8434f752cad83c43a46549dfaf1 Mon Sep 17 00:00:00 2001 From: AHMED Date: Fri, 25 Feb 2022 20:17:27 +0200 Subject: [PATCH 1/7] pypass pressOnEnter when textArea active --- src/components/Button.js | 31 +++++++++++++++------ src/components/FormAlertWithSubmitButton.js | 6 +++- src/pages/workspace/WorkspaceInvitePage.js | 4 +++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/components/Button.js b/src/components/Button.js index 6c826576c69..11c9e860ebf 100644 --- a/src/components/Button.js +++ b/src/components/Button.js @@ -116,15 +116,17 @@ class Button extends Component { return; } - const shortcutConfig = CONST.KEYBOARD_SHORTCUTS.ENTER; + this.subscribeEnterKey(); + } - // Setup and attach keypress handler for pressing the button with Enter key - this.unsubscribe = KeyboardShortcut.subscribe(shortcutConfig.shortcutKey, () => { - if (this.props.isDisabled || this.props.isLoading) { - return; - } - this.props.onPress(); - }, shortcutConfig.descriptionKey, shortcutConfig.modifiers, true); + componentDidUpdate() { + // switch the keyboard listener on/off if pressOnEnter change. + if (!this.unsubscribe) { + this.subscribeEnterKey(); + } else if (!this.props.pressOnEnter) { + this.unsubscribe(); + this.unsubscribe = null; + } } componentWillUnmount() { @@ -135,6 +137,19 @@ class Button extends Component { this.unsubscribe(); } + /** + * Setup and attach keypress handler for pressing the button with Enter key + */ + subscribeEnterKey() { + const shortcutConfig = CONST.KEYBOARD_SHORTCUTS.ENTER; + this.unsubscribe = KeyboardShortcut.subscribe(shortcutConfig.shortcutKey, () => { + if (this.props.isDisabled || this.props.isLoading) { + return; + } + this.props.onPress(); + }, shortcutConfig.descriptionKey, shortcutConfig.modifiers, this.props.pressOnEnter); + } + renderContent() { const ContentComponent = this.props.ContentComponent; if (ContentComponent) { diff --git a/src/components/FormAlertWithSubmitButton.js b/src/components/FormAlertWithSubmitButton.js index 9c90472b016..9adbd7745fa 100644 --- a/src/components/FormAlertWithSubmitButton.js +++ b/src/components/FormAlertWithSubmitButton.js @@ -40,6 +40,9 @@ const propTypes = { /** Is the button in a loading state */ isLoading: PropTypes.bool, + /** Call the onPress function when Enter key is pressed */ + pressOnEnter: PropTypes.bool, + ...withLocalizePropTypes, }; @@ -49,6 +52,7 @@ const defaultProps = { isMessageHtml: false, containerStyles: [], isLoading: false, + pressOnEnter: true, }; const FormAlertWithSubmitButton = (props) => { @@ -104,7 +108,7 @@ const FormAlertWithSubmitButton = (props) => { )}