-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Edit composer moves focus to main composer after press LHN menu item #24481
Changes from 11 commits
994fd6f
f5b834f
9480a6c
39b3902
d3ec5e4
c7fb2ff
76c3710
bc9b0d6
3a64c6e
56ceb26
0f744e4
fafeeaf
9b8f563
a623c7d
9add8d1
9005a8a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,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 */ | ||
|
@@ -159,6 +161,8 @@ function Composer({ | |
}) { | ||
const textRef = useRef(null); | ||
const textInput = useRef(null); | ||
const willBlurTextInputOnTapOutsideRef = useRef(); | ||
willBlurTextInputOnTapOutsideRef.current = willBlurTextInputOnTapOutside(); | ||
const initialValue = defaultValue ? `${defaultValue}` : `${value || ''}`; | ||
const [numberOfLines, setNumberOfLines] = useState(numberOfLinesProp); | ||
const [selection, setSelection] = useState({ | ||
|
@@ -370,6 +374,7 @@ function Composer({ | |
} | ||
|
||
return () => { | ||
ReportActionComposeFocusManager.clear(); | ||
unsubscribeFocus(); | ||
unsubscribeBlur(); | ||
document.removeEventListener('paste', handlePaste); | ||
|
@@ -448,6 +453,18 @@ function Composer({ | |
numberOfLines={numberOfLines} | ||
disabled={isDisabled} | ||
onKeyPress={handleKeyPress} | ||
onFocus={(e) => { | ||
ReportActionComposeFocusManager.onComposerFocus(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We shouldn't be using this There is no main or general composer inside composer/index.js. We handled this inside composer/index.js (web variant) but what about the native variant? Can you optimize this solution keeping scalability in mind? Can we handle this ReportActionCompose instead given that we allow onFocus prop as well as forward ref to inner textinput? cc: @Ollyws There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the feedback. Yes good point, we could pass the function to Composer from ReportActionCompose but we would also need to pass it in ReportActionItemMessageEdit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. Do you think we should do this @Ollyws? if not, why not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see why not, but we should make sure to check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So I thought we should handle the refactor in #25892, right? I'll update my proposal to handle that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That works for me. |
||
if (!willBlurTextInputOnTapOutsideRef.current || !textInput.current) { | ||
return; | ||
} | ||
|
||
textInput.current.focus(); | ||
}); | ||
if (props.onFocus) { | ||
props.onFocus(e); | ||
} | ||
}} | ||
/> | ||
{shouldCalculateCaretPosition && renderElementForCaretPosition} | ||
</> | ||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -2,16 +2,23 @@ import _ from 'underscore'; | |||||||
import React from 'react'; | ||||||||
|
||||||||
const composerRef = React.createRef(); | ||||||||
// There are two types of composer: general composer (edit composer) and main composer | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. |
||||||||
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 | ||||||||
* @param {Boolean} isMainComposer | ||||||||
*/ | ||||||||
function onComposerFocus(callback) { | ||||||||
focusCallback = callback; | ||||||||
function onComposerFocus(callback, isMainComposer = false) { | ||||||||
if (isMainComposer) { | ||||||||
mainComposerFocusCallback = callback; | ||||||||
} else { | ||||||||
focusCallback = callback; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
|
@@ -20,6 +27,11 @@ function onComposerFocus(callback) { | |||||||
*/ | ||||||||
function focus() { | ||||||||
if (!_.isFunction(focusCallback)) { | ||||||||
if (!_.isFunction(mainComposerFocusCallback)) { | ||||||||
return; | ||||||||
} | ||||||||
|
||||||||
mainComposerFocusCallback(); | ||||||||
return; | ||||||||
} | ||||||||
Comment on lines
30
to
37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if (!_.isFunction(focusCallback) && !_.isFunction(mainComposerFocusCallback)) {
return;
}
if (_.isFunction(mainComposerFocusCallback)) {
mainComposerFocusCallback();
return;
}
focusCallback(); This seems more readable to me. |
||||||||
|
||||||||
|
@@ -29,9 +41,14 @@ function focus() { | |||||||
/** | ||||||||
* Clear the registered focus callback | ||||||||
* | ||||||||
* @param {Boolean} isMainComposer | ||||||||
*/ | ||||||||
function clear() { | ||||||||
focusCallback = null; | ||||||||
function clear(isMainComposer = false) { | ||||||||
robertjchen marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
if (isMainComposer) { | ||||||||
mainComposerFocusCallback = null; | ||||||||
} else { | ||||||||
focusCallback = null; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
/** | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just out of curiosity, why did you change this to a ref?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to just directly call the function instead of creating the ref, right? I've updated that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we do it that way in reportActionCompose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But do we even need it allall seeing as we are only adding this to
composer
on web, andwillBlurTextInputOnTapOutside
only returns true on web. Correct me if I'm wrong but won't it return true 100% of the time here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree! I was trying to replicate the conditions from ReportActionCompose without noticing that.