Skip to content

Commit

Permalink
fix: 23898
Browse files Browse the repository at this point in the history
  • Loading branch information
tienifr committed Aug 8, 2023
1 parent fdcae9f commit 994fd6f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/components/Composer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import Text from '../Text';
import isEnterWhileComposition from '../../libs/KeyboardShortcut/isEnterWhileComposition';
import CONST from '../../CONST';
import withNavigation from '../withNavigation';
import ReportActionComposeFocusManager from '../../libs/ReportActionComposeFocusManager';
import willBlurTextInputOnTapOutside from '../../libs/willBlurTextInputOnTapOutside';

const propTypes = {
/** Maximum number of lines in the text input */
Expand Down Expand Up @@ -140,6 +142,7 @@ class Composer extends React.Component {
this.textRef = React.createRef(null);
this.unsubscribeBlur = () => null;
this.unsubscribeFocus = () => null;
this.willBlurTextInputOnTapOutside = willBlurTextInputOnTapOutside();
}

componentDidMount() {
Expand Down Expand Up @@ -458,6 +461,18 @@ class Composer extends React.Component {
numberOfLines={this.state.numberOfLines}
disabled={this.props.isDisabled}
onKeyPress={this.handleKeyPress}
onFocus={(e) => {
ReportActionComposeFocusManager.onComposerFocus(() => {
if (!this.willBlurTextInputOnTapOutside) {
return;
}

this.textInput.focus();
});
if (this.props.onFocus) {
this.props.onFocus(e);
}
}}
/>
{this.props.shouldCalculateCaretPosition && renderElementForCaretPosition}
</>
Expand Down
28 changes: 24 additions & 4 deletions src/libs/ReportActionComposeFocusManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,55 @@ import React from 'react';

const composerRef = React.createRef();
let focusCallback = null;
let mainComposerFocusCallback = null;

/**
* Register a callback to be called when focus is requested.
* Typical uses of this would be call the focus on the ReportActionComposer.
*
* @param {Function} callback callback to register
*/
function onComposerFocus(callback) {
focusCallback = callback;
function onComposerFocus(callback, isMainComposer = false) {
if (isMainComposer) {
console.log('registering mainComposerFocusCallback')
mainComposerFocusCallback = callback;
} else {
focusCallback = callback;
}
}

/**
* Request focus on the ReportActionComposer
*
*/
function focus() {
console.log('calling focussss')
if (!_.isFunction(focusCallback)) {
if (!_.isFunction(mainComposerFocusCallback)) {
console.log('mainComposerFocusCallbackkkk', mainComposerFocusCallback)
return;
}

console.log('mainComposerFocusCallback');
mainComposerFocusCallback();
return;
}

console.log('calling focusCallback')

focusCallback();
}

/**
* Clear the registered focus callback
*
*/
function clear() {
focusCallback = null;
function clear(isMainComposer = false) {
if (isMainComposer) {
mainComposerFocusCallback = null;
} else {
focusCallback = null;
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/pages/home/report/ReportActionCompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class ReportActionCompose extends React.Component {
}

componentWillUnmount() {
ReportActionComposeFocusManager.clear();
ReportActionComposeFocusManager.clear(true);

KeyDownListener.removeKeyDownPressListner(this.focusComposerOnKeyPress);
this.unsubscribeNavigationBlur();
Expand Down Expand Up @@ -320,7 +320,7 @@ class ReportActionCompose extends React.Component {
}

this.focus(false);
});
}, true);
}

getDefaultSuggestionsValues() {
Expand Down

0 comments on commit 994fd6f

Please sign in to comment.