Repository navigation
Fix cursor moves to write something briefly with expand compose box #77147
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
Changes from all commits
019954b
9f17e7e
b24156c
4c63d3b
7c04bf2
55f072e
b8bccac
b1383fc
6f34303
ed3c905
8ee1d07
f1a8be7
9643987
02832f3
c9fe00b
e8ef799
d184a52
fe716f5
6462fea
6734dcb
cab19fa
38d7731
6369b4a
55027e2
4855c2c
c4abcda
cb7f151
827ddc5
f45283f
4e3daec
ae9b154
fb2ac0e
3fc77cc
f15fdff
d2c83db
cd032b2
be44f89
b69fc29
0faa700
07cc25d
4608dde
6085a1d
de6c791
5a9e88e
71d1a02
92c335d
f82db7a
331c0d5
9ffcb74
589d991
c6c0e61
9d91f16
df874bb
60ce20d
1c37b54
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 |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import {useIsFocused, useNavigation, useRoute} from '@react-navigation/native'; | ||
| import lodashDebounce from 'lodash/debounce'; | ||
| import type {ForwardedRef, RefObject} from 'react'; | ||
| import type {Ref, RefObject} from 'react'; | ||
| import React, {memo, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState} from 'react'; | ||
| import type {BlurEvent, LayoutChangeEvent, MeasureInWindowOnSuccessCallback, TextInput, TextInputContentSizeChangeEvent, TextInputKeyPressEvent, TextInputScrollEvent} from 'react-native'; | ||
| import {DeviceEventEmitter, InteractionManager, NativeModules, StyleSheet, View} from 'react-native'; | ||
|
|
@@ -82,7 +82,7 @@ type ComposerWithSuggestionsProps = Partial<ChildrenProps> & | |
| onValueChange: (value: string) => void; | ||
|
|
||
| /** Callback when the composer got cleared on the UI thread */ | ||
| onCleared?: (text: string) => void; | ||
| onClear?: (text: string) => void; | ||
|
|
||
| /** Whether the composer is full size */ | ||
| isComposerFullSize: boolean; | ||
|
|
@@ -106,7 +106,7 @@ type ComposerWithSuggestionsProps = Partial<ChildrenProps> & | |
| setIsCommentEmpty: (isCommentEmpty: boolean) => void; | ||
|
|
||
| /** Function to handle sending a message */ | ||
| handleSendMessage: () => void; | ||
| onEnterKeyPress: () => void; | ||
|
Contributor
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. Rename property to make component more "dumb" and make clearer for what this event is actually triggered. |
||
|
|
||
| /** Whether the compose input should show */ | ||
| shouldShowComposeInput: OnyxEntry<boolean>; | ||
|
|
@@ -142,7 +142,7 @@ type ComposerWithSuggestionsProps = Partial<ChildrenProps> & | |
| didHideComposerInput?: boolean; | ||
|
|
||
| /** Reference to the outer element */ | ||
| ref?: ForwardedRef<ComposerRef>; | ||
| ref?: Ref<ComposerRef | null>; | ||
| }; | ||
|
|
||
| type SwitchToCurrentReportProps = { | ||
|
|
@@ -157,11 +157,17 @@ type ComposerRef = { | |
| replaceSelectionWithText: OnEmojiSelected; | ||
| getCurrentText: () => string; | ||
| isFocused: () => boolean; | ||
|
|
||
| /** | ||
| * Calling clear will immediately clear the input on the UI thread (its a worklet). | ||
| * Once the composer ahs cleared onCleared will be called with the value that was cleared. | ||
| */ | ||
| clear: () => void; | ||
| clearWorklet: () => void; | ||
|
|
||
| /** | ||
| * Reset the height of the composer. | ||
| */ | ||
| resetHeight: () => void; | ||
| }; | ||
|
|
||
| const {RNTextInputReset} = NativeModules; | ||
|
|
@@ -215,13 +221,13 @@ function ComposerWithSuggestions({ | |
| onPasteFile, | ||
| disabled, | ||
| setIsCommentEmpty, | ||
| handleSendMessage, | ||
| onEnterKeyPress, | ||
| shouldShowComposeInput, | ||
| measureParentContainer = () => {}, | ||
| isScrollLikelyLayoutTriggered, | ||
| raiseIsScrollLikelyLayoutTriggered, | ||
| onCleared = () => {}, | ||
| onLayout: onLayoutProps, | ||
| onClear: onClearProp = () => {}, | ||
| onLayout, | ||
|
|
||
| // Refs | ||
| suggestionsRef, | ||
|
|
@@ -282,7 +288,8 @@ function ComposerWithSuggestions({ | |
|
|
||
| const [selection, setSelection] = useState<TextSelection>(() => ({start: value.length, end: value.length, positionX: 0, positionY: 0})); | ||
|
|
||
| const [composerHeight, setComposerHeight] = useState(0); | ||
| const [composerHeightAfterClear, setDefaultComposerHeight] = useState<number | null>(null); | ||
| const emptyComposerHeightRef = useRef<number | null>(null); | ||
|
|
||
| const textInputRef = useRef<TextInput | null>(null); | ||
|
|
||
|
|
@@ -511,7 +518,7 @@ function ComposerWithSuggestions({ | |
| // Submit the form when Enter is pressed | ||
| if (webEvent.key === CONST.KEYBOARD_SHORTCUTS.ENTER.shortcutKey && !webEvent.shiftKey) { | ||
| webEvent.preventDefault(); | ||
| handleSendMessage(); | ||
| onEnterKeyPress(); | ||
| } | ||
|
|
||
| // Trigger the edit box for last sent message if ArrowUp is pressed and the comment is empty and Chronos is not in the participants | ||
|
|
@@ -554,11 +561,26 @@ function ComposerWithSuggestions({ | |
| } | ||
| } | ||
| }, | ||
| [shouldUseNarrowLayout, isKeyboardShown, suggestionsRef, selection.start, includeChronos, handleSendMessage, lastReportAction, reportID, updateComment, selection.end], | ||
| [shouldUseNarrowLayout, isKeyboardShown, suggestionsRef, selection.start, includeChronos, onEnterKeyPress, lastReportAction, reportID, updateComment, selection.end], | ||
| ); | ||
|
|
||
| /** | ||
| * Once we cleared the input and the composer finished rendering, we need to reset the manual height value. | ||
| * After that, the composer will adjust it's height based on it's parent flex layout. | ||
| */ | ||
| const clearComposerHeight = useCallback(() => { | ||
| if (composerHeightAfterClear == null) { | ||
| return; | ||
| } | ||
| setDefaultComposerHeight(null); | ||
| }, [composerHeightAfterClear]); | ||
|
|
||
| const onChangeText = useCallback( | ||
| (commentValue: string) => { | ||
| // When we clear the input, we set the composer height to a specific value. | ||
| // Upon text change, we can reset the height to allow flex layout to adjust the height. | ||
| clearComposerHeight(); | ||
|
|
||
| updateComment(commentValue, true); | ||
|
|
||
| if (isIOSNative && syncSelectionWithOnChangeTextRef.current) { | ||
|
|
@@ -574,7 +596,7 @@ function ComposerWithSuggestions({ | |
| }); | ||
| } | ||
| }, | ||
| [updateComment], | ||
| [clearComposerHeight, updateComment], | ||
| ); | ||
|
|
||
| const onSelectionChange = useCallback( | ||
|
|
@@ -676,12 +698,19 @@ function ComposerWithSuggestions({ | |
| textInputRef.current.blur(); | ||
| }, []); | ||
|
|
||
| const clear = useCallback(() => { | ||
| const clearWorklet = useCallback(() => { | ||
| 'worklet'; | ||
|
|
||
| forceClearInput(animatedRef); | ||
| }, [animatedRef]); | ||
|
|
||
| const resetHeight = useCallback(() => { | ||
| if (!emptyComposerHeightRef.current) { | ||
| return; | ||
| } | ||
| setDefaultComposerHeight(emptyComposerHeightRef.current); | ||
| }, []); | ||
|
|
||
| const getCurrentText = useCallback(() => { | ||
| return commentRef.current; | ||
| }, []); | ||
|
|
@@ -755,36 +784,25 @@ function ComposerWithSuggestions({ | |
| focus, | ||
| replaceSelectionWithText, | ||
| isFocused: () => !!textInputRef.current?.isFocused(), | ||
| clear, | ||
| getCurrentText, | ||
| clearWorklet, | ||
| resetHeight, | ||
| }), | ||
| [blur, clear, focus, replaceSelectionWithText, getCurrentText], | ||
| [blur, focus, replaceSelectionWithText, clearWorklet, resetHeight, getCurrentText], | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| onValueChange(value); | ||
| }, [onValueChange, value]); | ||
|
|
||
| const onLayout = useCallback( | ||
|
Contributor
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. Not needed anymore. |
||
| (e: LayoutChangeEvent) => { | ||
| onLayoutProps?.(e); | ||
| const composerLayoutHeight = e.nativeEvent.layout.height; | ||
| if (composerHeight === composerLayoutHeight) { | ||
| return; | ||
| } | ||
| setComposerHeight(composerLayoutHeight); | ||
| }, | ||
| [composerHeight, onLayoutProps], | ||
| ); | ||
|
|
||
| const onClear = useCallback( | ||
| (text: string) => { | ||
| mobileInputScrollPosition.current = 0; | ||
| // Note: use the value when the clear happened, not the current value which might have changed already | ||
| onCleared(text); | ||
| onClearProp(text); | ||
| updateComment('', true); | ||
| }, | ||
| [onCleared, updateComment], | ||
| [onClearProp, updateComment], | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
|
|
@@ -828,15 +846,25 @@ function ComposerWithSuggestions({ | |
| const isTouchEndedRef = useRef(false); | ||
| const containerComposeStyles = StyleSheet.flatten(StyleUtils.getContainerComposeStyles()); | ||
|
|
||
| const updateIsFullComposerAvailable = useCallback( | ||
| const handleContentSizeChange = useCallback( | ||
| (e: TextInputContentSizeChangeEvent) => { | ||
| const paddingTopAndBottom = (containerComposeStyles.paddingVertical as number) * 2; | ||
| const inputHeight = e.nativeEvent.contentSize.height; | ||
| const totalHeight = inputHeight + paddingTopAndBottom; | ||
|
|
||
| // When we clear the input, we set the composer height to a specific value. | ||
| // Upon any content size change, we can reset the height to allow flex layout to adjust the height. | ||
| clearComposerHeight(); | ||
|
|
||
| // Store the default collapsed composer height, so we can later reset the height when we clear the input. | ||
| if (emptyComposerHeightRef.current === null && inputHeight > 0 && !valueRef.current.includes('\n')) { | ||
| emptyComposerHeightRef.current = inputHeight; | ||
| } | ||
|
|
||
| const isFullComposerAvailable = totalHeight >= CONST.COMPOSER.FULL_COMPOSER_MIN_HEIGHT; | ||
| setIsFullComposerAvailable?.(isFullComposerAvailable); | ||
| }, | ||
| [setIsFullComposerAvailable, containerComposeStyles], | ||
| [containerComposeStyles.paddingVertical, clearComposerHeight, setIsFullComposerAvailable], | ||
| ); | ||
|
|
||
| const handleFocus = useCallback(() => { | ||
|
|
@@ -878,7 +906,11 @@ function ComposerWithSuggestions({ | |
| onChangeText={onChangeText} | ||
| onKeyPress={handleKeyPress} | ||
| textAlignVertical="top" | ||
| style={[styles.textInputCompose, isComposerFullSize ? styles.textInputFullCompose : styles.textInputCollapseCompose]} | ||
| style={[ | ||
| styles.textInputCompose, | ||
| isComposerFullSize ? styles.textInputFullCompose : styles.textInputCollapseCompose, | ||
| composerHeightAfterClear != null && {height: composerHeightAfterClear}, | ||
| ]} | ||
| maxLines={maxComposerLines} | ||
| onFocus={handleFocus} | ||
| onBlur={onBlur} | ||
|
|
@@ -892,7 +924,7 @@ function ComposerWithSuggestions({ | |
| selection={selection} | ||
| onSelectionChange={onSelectionChange} | ||
| isComposerFullSize={isComposerFullSize} | ||
| onContentSizeChange={updateIsFullComposerAvailable} | ||
| onContentSizeChange={handleContentSizeChange} | ||
| value={value} | ||
| testID="composer" | ||
| shouldCalculateCaretPosition | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -197,7 +197,7 @@ function ReportActionCompose({ | |
| const icons = useMemoizedLazyExpensifyIcons(['MessageInABottle']); | ||
|
|
||
| const suggestionsRef = useRef<SuggestionsRef>(null); | ||
| const composerRef = useRef<ComposerRef | undefined>(undefined); | ||
| const composerRef = useRef<ComposerRef | null>(null); | ||
| const reportParticipantIDs = useMemo( | ||
| () => | ||
| Object.keys(report?.participants ?? {}) | ||
|
|
@@ -310,12 +310,14 @@ function ReportActionCompose({ | |
|
|
||
| const addAttachment = useCallback((file: FileObject | FileObject[]) => { | ||
| attachmentFileRef.current = file; | ||
| const clear = composerRef.current?.clear; | ||
| if (!clear) { | ||
| throw new Error('The composerRef.clear function is not set yet. This should never happen, and indicates a developer error.'); | ||
|
|
||
| const clearWorklet = composerRef.current?.clearWorklet; | ||
|
|
||
| if (!clearWorklet) { | ||
| throw new Error('The composerRef.clearWorklet function is not set yet. This should never happen, and indicates a developer error.'); | ||
| } | ||
|
|
||
| scheduleOnUI(clear); | ||
| scheduleOnUI(clearWorklet); | ||
| }, []); | ||
|
|
||
| /** | ||
|
|
@@ -365,7 +367,18 @@ function ReportActionCompose({ | |
| onSubmit(newCommentTrimmed); | ||
| } | ||
| }, | ||
| [isConciergeChat, kickoffWaitingIndicator, transactionThreadReport, report, reportID, ancestors, personalDetail.timezone, onSubmit, isInSidePanel], | ||
| [ | ||
| isConciergeChat, | ||
| kickoffWaitingIndicator, | ||
| transactionThreadReport, | ||
| report, | ||
| reportID, | ||
| ancestors, | ||
| currentUserPersonalDetails.accountID, | ||
| personalDetail.timezone, | ||
| isInSidePanel, | ||
| onSubmit, | ||
| ], | ||
| ); | ||
|
|
||
| const onTriggerAttachmentPicker = useCallback(() => { | ||
|
|
@@ -443,28 +456,28 @@ function ReportActionCompose({ | |
|
|
||
| // Note: using JS refs is not well supported in reanimated, thus we need to store the function in a shared value | ||
| // useSharedValue on web doesn't support functions, so we need to wrap it in an object. | ||
| const composerRefShared = useSharedValue<{ | ||
| clear: (() => void) | undefined; | ||
| }>({clear: undefined}); | ||
| const composerRefShared = useSharedValue<Partial<ComposerRef>>({}); | ||
|
|
||
| const handleSendMessage = useCallback(() => { | ||
| if (isSendDisabled || !debouncedValidate.flush()) { | ||
| return; | ||
| } | ||
|
|
||
| scheduleOnUI(() => { | ||
| 'worklet'; | ||
|
Member
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. Might be this... |
||
| composerRef.current?.resetHeight(); | ||
| if (isComposerFullSize) { | ||
| setIsComposerFullSize(reportID, false); | ||
| } | ||
|
|
||
| const {clear: clearComposer} = composerRefShared.get(); | ||
| scheduleOnUI(() => { | ||
| const {clearWorklet} = composerRefShared.get(); | ||
|
|
||
| if (!clearComposer) { | ||
| throw new Error('The composerRefShared.clear function is not set yet. This should never happen, and indicates a developer error.'); | ||
| if (!clearWorklet) { | ||
| throw new Error('The composerRef.clearWorklet function is not set yet. This should never happen, and indicates a developer error.'); | ||
| } | ||
|
|
||
| // This will cause onCleared to be triggered where we actually send the message | ||
| clearComposer?.(); | ||
| clearWorklet?.(); | ||
| }); | ||
| }, [isSendDisabled, debouncedValidate, composerRefShared]); | ||
| }, [isSendDisabled, debouncedValidate, isComposerFullSize, reportID, composerRefShared]); | ||
|
|
||
| onSubmitAction = handleSendMessage; | ||
|
|
||
|
|
@@ -575,9 +588,9 @@ function ReportActionCompose({ | |
| /> | ||
| <ComposerWithSuggestions | ||
| ref={(ref) => { | ||
| composerRef.current = ref ?? undefined; | ||
| composerRef.current = ref; | ||
| composerRefShared.set({ | ||
| clear: ref?.clear, | ||
| clearWorklet: ref?.clearWorklet, | ||
| }); | ||
| }} | ||
| suggestionsRef={suggestionsRef} | ||
|
|
@@ -594,10 +607,10 @@ function ReportActionCompose({ | |
| isComposerFullSize={isComposerFullSize} | ||
| setIsFullComposerAvailable={setIsFullComposerAvailable} | ||
| onPasteFile={(files) => validateAttachments({files})} | ||
| onCleared={submitForm} | ||
| onClear={submitForm} | ||
| disabled={isBlockedFromConcierge || isEmojiPickerVisible()} | ||
| setIsCommentEmpty={setIsCommentEmpty} | ||
| handleSendMessage={handleSendMessage} | ||
| onEnterKeyPress={handleSendMessage} | ||
| shouldShowComposeInput={shouldShowComposeInput} | ||
| onFocus={onFocus} | ||
| onBlur={onBlur} | ||
|
|
||
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.
Rename property to match event/callback best practices