diff --git a/src/components/ExceededCommentLength.js b/src/components/ExceededCommentLength.js index 6353bdf40283..2cc1c50e2f66 100644 --- a/src/components/ExceededCommentLength.js +++ b/src/components/ExceededCommentLength.js @@ -1,52 +1,23 @@ -import {debounce} from 'lodash'; import PropTypes from 'prop-types'; -import React, {useEffect, useMemo, useState} from 'react'; -import {withOnyx} from 'react-native-onyx'; +import React from 'react'; import useLocalize from '@hooks/useLocalize'; -import * as ReportUtils from '@libs/ReportUtils'; import useThemeStyles from '@styles/useThemeStyles'; import CONST from '@src/CONST'; -import ONYXKEYS from '@src/ONYXKEYS'; import Text from './Text'; const propTypes = { - /** Report ID to get the comment from (used in withOnyx) */ - // eslint-disable-next-line react/no-unused-prop-types - reportID: PropTypes.string.isRequired, - - /** Text Comment */ - comment: PropTypes.string, - - /** Update UI on parent when comment length is exceeded */ - onExceededMaxCommentLength: PropTypes.func.isRequired, + shouldShowError: PropTypes.bool.isRequired, }; -const defaultProps = { - comment: '', -}; +const defaultProps = {}; function ExceededCommentLength(props) { const styles = useThemeStyles(); const {numberFormat, translate} = useLocalize(); - const [commentLength, setCommentLength] = useState(0); - const updateCommentLength = useMemo( - () => - debounce((comment, onExceededMaxCommentLength) => { - const newCommentLength = ReportUtils.getCommentLength(comment); - setCommentLength(newCommentLength); - onExceededMaxCommentLength(newCommentLength > CONST.MAX_COMMENT_LENGTH); - }, CONST.TIMING.COMMENT_LENGTH_DEBOUNCE_TIME), - [], - ); - - useEffect(() => { - updateCommentLength(props.comment, props.onExceededMaxCommentLength); - }, [props.comment, props.onExceededMaxCommentLength, updateCommentLength]); - if (commentLength <= CONST.MAX_COMMENT_LENGTH) { + if (!props.shouldShowError) { return null; } - return ( `${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`, - initialValue: '', - }, -})(ExceededCommentLength); +export default ExceededCommentLength; diff --git a/src/hooks/useHandleExceedMaxCommentLength.ts b/src/hooks/useHandleExceedMaxCommentLength.ts new file mode 100644 index 000000000000..9700999bb004 --- /dev/null +++ b/src/hooks/useHandleExceedMaxCommentLength.ts @@ -0,0 +1,27 @@ +import _ from 'lodash'; +import {useCallback, useMemo, useState} from 'react'; +import * as ReportUtils from '@libs/ReportUtils'; +import CONST from '@src/CONST'; + +const useHandleExceedMaxCommentLength = () => { + const [hasExceededMaxCommentLength, setHasExceededMaxCommentLength] = useState(false); + + const handleValueChange = useCallback( + (value: string) => { + if (ReportUtils.getCommentLength(value) <= CONST.MAX_COMMENT_LENGTH) { + if (hasExceededMaxCommentLength) { + setHasExceededMaxCommentLength(false); + } + return; + } + setHasExceededMaxCommentLength(true); + }, + [hasExceededMaxCommentLength], + ); + + const validateCommentMaxLength = useMemo(() => _.debounce(handleValueChange, 1500), [handleValueChange]); + + return {hasExceededMaxCommentLength, validateCommentMaxLength}; +}; + +export default useHandleExceedMaxCommentLength; diff --git a/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.js b/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.js index f5a321f72799..add50d2050b2 100644 --- a/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.js +++ b/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.js @@ -76,6 +76,7 @@ function ComposerWithSuggestions({ // Focus onFocus, onBlur, + onValueChange, // Composer isComposerFullSize, isMenuVisible, @@ -515,6 +516,10 @@ function ComposerWithSuggestions({ [blur, focus, prepareCommentAndResetComposer, replaceSelectionWithText], ); + useEffect(() => { + onValueChange(value); + }, [onValueChange, value]); + return ( <> diff --git a/src/pages/home/report/ReportActionCompose/ReportActionCompose.js b/src/pages/home/report/ReportActionCompose/ReportActionCompose.js index 2632324a963f..3d29aa4918ad 100644 --- a/src/pages/home/report/ReportActionCompose/ReportActionCompose.js +++ b/src/pages/home/report/ReportActionCompose/ReportActionCompose.js @@ -13,6 +13,7 @@ import OfflineIndicator from '@components/OfflineIndicator'; import OfflineWithFeedback from '@components/OfflineWithFeedback'; import {usePersonalDetails, withNetwork} from '@components/OnyxProvider'; import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsDefaultProps, withCurrentUserPersonalDetailsPropTypes} from '@components/withCurrentUserPersonalDetails'; +import useHandleExceedMaxCommentLength from '@hooks/useHandleExceedMaxCommentLength'; import useLocalize from '@hooks/useLocalize'; import useWindowDimensions from '@hooks/useWindowDimensions'; import canFocusInputOnScreenFocus from '@libs/canFocusInputOnScreenFocus'; @@ -144,7 +145,7 @@ function ReportActionCompose({ * Updates the composer when the comment length is exceeded * Shows red borders and prevents the comment from being sent */ - const [hasExceededMaxCommentLength, setExceededMaxCommentLength] = useState(false); + const {hasExceededMaxCommentLength, validateCommentMaxLength} = useHandleExceedMaxCommentLength(); const suggestionsRef = useRef(null); const composerRef = useRef(null); @@ -408,6 +409,7 @@ function ReportActionCompose({ onBlur={onBlur} measureParentContainer={measureContainer} listHeight={listHeight} + onValueChange={validateCommentMaxLength} /> { @@ -444,10 +446,7 @@ function ReportActionCompose({ > {!isSmallScreenWidth && } - + diff --git a/src/pages/home/report/ReportActionItemMessageEdit.js b/src/pages/home/report/ReportActionItemMessageEdit.js index fc03c67d70a0..ca66822e6215 100644 --- a/src/pages/home/report/ReportActionItemMessageEdit.js +++ b/src/pages/home/report/ReportActionItemMessageEdit.js @@ -13,6 +13,7 @@ import * as Expensicons from '@components/Icon/Expensicons'; import PressableWithFeedback from '@components/Pressable/PressableWithFeedback'; import refPropTypes from '@components/refPropTypes'; import Tooltip from '@components/Tooltip'; +import useHandleExceedMaxCommentLength from '@hooks/useHandleExceedMaxCommentLength'; import useKeyboardState from '@hooks/useKeyboardState'; import useLocalize from '@hooks/useLocalize'; import useReportScrollManager from '@hooks/useReportScrollManager'; @@ -116,7 +117,7 @@ function ReportActionItemMessageEdit(props) { }); const [selection, setSelection] = useState(getInitialSelection); const [isFocused, setIsFocused] = useState(false); - const [hasExceededMaxCommentLength, setHasExceededMaxCommentLength] = useState(false); + const {hasExceededMaxCommentLength, validateCommentMaxLength} = useHandleExceedMaxCommentLength(); const [modal, setModal] = useState(false); const [onyxFocused, setOnyxFocused] = useState(false); @@ -369,6 +370,10 @@ function ReportActionItemMessageEdit(props) { */ const focus = focusComposerWithDelay(textInputRef.current); + useEffect(() => { + validateCommentMaxLength(draft); + }, [draft, validateCommentMaxLength]); + return ( <> @@ -470,11 +475,7 @@ function ReportActionItemMessageEdit(props) { - setHasExceededMaxCommentLength(hasExceeded)} - /> + ); }