diff --git a/src/pages/inbox/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx b/src/pages/inbox/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx index ad113029fe8e..fee78bd6b41a 100644 --- a/src/pages/inbox/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx +++ b/src/pages/inbox/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx @@ -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 & 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 & setIsCommentEmpty: (isCommentEmpty: boolean) => void; /** Function to handle sending a message */ - handleSendMessage: () => void; + onEnterKeyPress: () => void; /** Whether the compose input should show */ shouldShowComposeInput: OnyxEntry; @@ -142,7 +142,7 @@ type ComposerWithSuggestionsProps = Partial & didHideComposerInput?: boolean; /** Reference to the outer element */ - ref?: ForwardedRef; + ref?: Ref; }; 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(() => ({start: value.length, end: value.length, positionX: 0, positionY: 0})); - const [composerHeight, setComposerHeight] = useState(0); + const [composerHeightAfterClear, setDefaultComposerHeight] = useState(null); + const emptyComposerHeightRef = useRef(null); const textInputRef = useRef(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( - (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 diff --git a/src/pages/inbox/report/ReportActionCompose/ReportActionCompose.tsx b/src/pages/inbox/report/ReportActionCompose/ReportActionCompose.tsx index 44f929a8b2b0..54f82235396b 100644 --- a/src/pages/inbox/report/ReportActionCompose/ReportActionCompose.tsx +++ b/src/pages/inbox/report/ReportActionCompose/ReportActionCompose.tsx @@ -197,7 +197,7 @@ function ReportActionCompose({ const icons = useMemoizedLazyExpensifyIcons(['MessageInABottle']); const suggestionsRef = useRef(null); - const composerRef = useRef(undefined); + const composerRef = useRef(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>({}); const handleSendMessage = useCallback(() => { if (isSendDisabled || !debouncedValidate.flush()) { return; } - scheduleOnUI(() => { - 'worklet'; + 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({ /> { - 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} diff --git a/src/styles/index.ts b/src/styles/index.ts index 7e00d781a487..19f0acb9b69c 100644 --- a/src/styles/index.ts +++ b/src/styles/index.ts @@ -2135,7 +2135,6 @@ const staticStyles = (theme: ThemeColors) => ...(Platform.OS === 'android' && { height: undefined, lineHeight: undefined, - alignSelf: 'stretch', flexGrow: 1, flexShrink: 1, }),