From c01af10eec5d3ff75f709c8afb951869d4f32a82 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Fri, 27 Oct 2023 12:11:48 -0600 Subject: [PATCH] Revert "[Form Provider Refactor] RoomNameInput" --- src/components/Form/FormProvider.js | 24 +++---- src/components/Form/InputWrapper.js | 2 - src/components/RoomNameInput/index.js | 50 ++++++++++---- src/components/RoomNameInput/index.native.js | 66 +++++++++++++++++++ .../RoomNameInput/roomNameInputPropTypes.js | 6 +- src/pages/settings/Report/RoomNamePage.js | 8 +-- src/pages/workspace/WorkspaceNewRoomPage.js | 21 +++--- 7 files changed, 128 insertions(+), 49 deletions(-) create mode 100644 src/components/RoomNameInput/index.native.js diff --git a/src/components/Form/FormProvider.js b/src/components/Form/FormProvider.js index ce402976d097..add58dbef18c 100644 --- a/src/components/Form/FormProvider.js +++ b/src/components/Form/FormProvider.js @@ -229,15 +229,13 @@ function FormProvider({validate, formID, shouldValidateOnBlur, shouldValidateOnC .first() .value() || ''; - const value = !_.isUndefined(inputValues[`${inputID}ToDisplay`]) ? inputValues[`${inputID}ToDisplay`] : inputValues[inputID]; - return { ...propsToParse, ref: newRef, inputID, key: propsToParse.key || inputID, errorText: errors[inputID] || fieldErrorMessage, - value, + value: inputValues[inputID], // As the text input is controlled, we never set the defaultValue prop // as this is already happening by the value prop. defaultValue: undefined, @@ -277,19 +275,13 @@ function FormProvider({validate, formID, shouldValidateOnBlur, shouldValidateOnC propsToParse.onBlur(event); } }, - onInputChange: (inputValue, key) => { + onInputChange: (value, key) => { const inputKey = key || inputID; setInputValues((prevState) => { - const newState = _.isFunction(propsToParse.valueParser) - ? { - ...prevState, - [inputKey]: propsToParse.valueParser(inputValue), - [`${inputKey}ToDisplay`]: inputValue, - } - : { - ...prevState, - [inputKey]: inputValue, - }; + const newState = { + ...prevState, + [inputKey]: value, + }; if (shouldValidateOnChange) { onValidate(newState); @@ -298,11 +290,11 @@ function FormProvider({validate, formID, shouldValidateOnBlur, shouldValidateOnC }); if (propsToParse.shouldSaveDraft) { - FormActions.setDraftValues(propsToParse.formID, {[inputKey]: inputValue}); + FormActions.setDraftValues(propsToParse.formID, {[inputKey]: value}); } if (_.isFunction(propsToParse.onValueChange)) { - propsToParse.onValueChange(inputValue, inputKey); + propsToParse.onValueChange(value, inputKey); } }, }; diff --git a/src/components/Form/InputWrapper.js b/src/components/Form/InputWrapper.js index 74a741239a3f..8a87bc2f5a5a 100644 --- a/src/components/Form/InputWrapper.js +++ b/src/components/Form/InputWrapper.js @@ -7,13 +7,11 @@ const propTypes = { inputID: PropTypes.string.isRequired, valueType: PropTypes.string, forwardedRef: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({current: PropTypes.instanceOf(React.Component)})]), - valueParser: PropTypes.func, }; const defaultProps = { forwardedRef: undefined, valueType: 'string', - valueParser: undefined, }; function InputWrapper(props) { diff --git a/src/components/RoomNameInput/index.js b/src/components/RoomNameInput/index.js index 197b6b77acae..ec9bf7a090ab 100644 --- a/src/components/RoomNameInput/index.js +++ b/src/components/RoomNameInput/index.js @@ -1,23 +1,49 @@ -import React from 'react'; +import React, {useState} from 'react'; +import _ from 'underscore'; import CONST from '../../CONST'; import TextInput from '../TextInput'; import useLocalize from '../../hooks/useLocalize'; import * as roomNameInputPropTypes from './roomNameInputPropTypes'; -import InputWrapper from '../Form/InputWrapper'; -import getOperatingSystem from '../../libs/getOperatingSystem'; import * as RoomNameInputUtils from '../../libs/RoomNameInputUtils'; -function RoomNameInput({isFocused, autoFocus, disabled, errorText, forwardedRef, onBlur, shouldDelayFocus, inputID}) { +function RoomNameInput({isFocused, autoFocus, disabled, errorText, forwardedRef, value, onBlur, onChangeText, onInputChange, shouldDelayFocus}) { const {translate} = useLocalize(); - const keyboardType = getOperatingSystem() === CONST.OS.IOS ? CONST.KEYBOARD_TYPE.ASCII_CAPABLE : CONST.KEYBOARD_TYPE.VISIBLE_PASSWORD; + const [selection, setSelection] = useState(); - const valueParser = (roomName) => RoomNameInputUtils.modifyRoomName(roomName); + /** + * Calls the onChangeText callback with a modified room name + * @param {Event} event + */ + const setModifiedRoomName = (event) => { + const roomName = event.nativeEvent.text; + const modifiedRoomName = RoomNameInputUtils.modifyRoomName(roomName); + onChangeText(modifiedRoomName); + + // if custom component has onInputChange, use it to trigger changes (Form input) + if (_.isFunction(onInputChange)) { + onInputChange(modifiedRoomName); + } + + // Prevent cursor jump behaviour: + // Check if newRoomNameWithHash is the same as modifiedRoomName + // If it is then the room name is valid (does not contain unallowed characters); no action required + // If not then the room name contains unvalid characters and we must adjust the cursor position manually + // Read more: https://github.com/Expensify/App/issues/12741 + const oldRoomNameWithHash = value || ''; + const newRoomNameWithHash = `${CONST.POLICY.ROOM_PREFIX}${roomName}`; + if (modifiedRoomName !== newRoomNameWithHash) { + const offset = modifiedRoomName.length - oldRoomNameWithHash.length; + const newSelection = { + start: selection.start + offset, + end: selection.end + offset, + }; + setSelection(newSelection); + } + }; return ( - setSelection(event.nativeEvent.selection)} errorText={errorText} - valueParser={valueParser} autoCapitalize="none" onBlur={() => isFocused && onBlur()} shouldDelayFocus={shouldDelayFocus} @@ -34,7 +63,6 @@ function RoomNameInput({isFocused, autoFocus, disabled, errorText, forwardedRef, maxLength={CONST.REPORT.MAX_ROOM_NAME_LENGTH} spellCheck={false} shouldInterceptSwipe - keyboardType={keyboardType} // this is a bit hacky solution to a RN issue https://github.com/facebook/react-native/issues/27449 /> ); } diff --git a/src/components/RoomNameInput/index.native.js b/src/components/RoomNameInput/index.native.js new file mode 100644 index 000000000000..9e83a673982c --- /dev/null +++ b/src/components/RoomNameInput/index.native.js @@ -0,0 +1,66 @@ +import React from 'react'; +import _ from 'underscore'; +import CONST from '../../CONST'; +import useLocalize from '../../hooks/useLocalize'; +import TextInput from '../TextInput'; +import * as roomNameInputPropTypes from './roomNameInputPropTypes'; +import * as RoomNameInputUtils from '../../libs/RoomNameInputUtils'; +import getOperatingSystem from '../../libs/getOperatingSystem'; + +function RoomNameInput({isFocused, autoFocus, disabled, errorText, forwardedRef, value, onBlur, onChangeText, onInputChange, shouldDelayFocus}) { + const {translate} = useLocalize(); + + /** + * Calls the onChangeText callback with a modified room name + * @param {Event} event + */ + const setModifiedRoomName = (event) => { + const roomName = event.nativeEvent.text; + const modifiedRoomName = RoomNameInputUtils.modifyRoomName(roomName); + onChangeText(modifiedRoomName); + + // if custom component has onInputChange, use it to trigger changes (Form input) + if (_.isFunction(onInputChange)) { + onInputChange(modifiedRoomName); + } + }; + + const keyboardType = getOperatingSystem() === CONST.OS.IOS ? CONST.KEYBOARD_TYPE.ASCII_CAPABLE : CONST.KEYBOARD_TYPE.VISIBLE_PASSWORD; + + return ( + isFocused && onBlur()} + autoFocus={isFocused && autoFocus} + autoCapitalize="none" + shouldDelayFocus={shouldDelayFocus} + /> + ); +} + +RoomNameInput.propTypes = roomNameInputPropTypes.propTypes; +RoomNameInput.defaultProps = roomNameInputPropTypes.defaultProps; +RoomNameInput.displayName = 'RoomNameInput'; + +const RoomNameInputWithRef = React.forwardRef((props, ref) => ( + +)); + +RoomNameInputWithRef.displayName = 'RoomNameInputWithRef'; + +export default RoomNameInputWithRef; diff --git a/src/components/RoomNameInput/roomNameInputPropTypes.js b/src/components/RoomNameInput/roomNameInputPropTypes.js index 3d1ad18d27b3..7f8292f0123e 100644 --- a/src/components/RoomNameInput/roomNameInputPropTypes.js +++ b/src/components/RoomNameInput/roomNameInputPropTypes.js @@ -1,5 +1,4 @@ import PropTypes from 'prop-types'; -import refPropTypes from '../refPropTypes'; const propTypes = { /** Callback to execute when the text input is modified correctly */ @@ -15,10 +14,10 @@ const propTypes = { errorText: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.object]))]), /** A ref forwarded to the TextInput */ - forwardedRef: refPropTypes, + forwardedRef: PropTypes.func, /** The ID used to uniquely identify the input in a Form */ - inputID: PropTypes.string.isRequired, + inputID: PropTypes.string, /** Callback that is called when the text input is blurred */ onBlur: PropTypes.func, @@ -40,6 +39,7 @@ const defaultProps = { errorText: '', forwardedRef: () => {}, + inputID: undefined, onBlur: () => {}, autoFocus: false, shouldDelayFocus: false, diff --git a/src/pages/settings/Report/RoomNamePage.js b/src/pages/settings/Report/RoomNamePage.js index 8163b09ca943..4ce997533378 100644 --- a/src/pages/settings/Report/RoomNamePage.js +++ b/src/pages/settings/Report/RoomNamePage.js @@ -7,6 +7,7 @@ import CONST from '../../../CONST'; import ScreenWrapper from '../../../components/ScreenWrapper'; import HeaderWithBackButton from '../../../components/HeaderWithBackButton'; import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize'; +import Form from '../../../components/Form'; import ONYXKEYS from '../../../ONYXKEYS'; import styles from '../../../styles/styles'; import Navigation from '../../../libs/Navigation/Navigation'; @@ -20,7 +21,6 @@ import * as Report from '../../../libs/actions/Report'; import RoomNameInput from '../../../components/RoomNameInput'; import * as ReportUtils from '../../../libs/ReportUtils'; import FullPageNotFoundView from '../../../components/BlockingViews/FullPageNotFoundView'; -import FormProvider from '../../../components/Form/FormProvider'; const propTypes = { ...withLocalizePropTypes, @@ -90,7 +90,7 @@ function RoomNamePage(props) { title={translate('newRoomPage.roomName')} onBackButtonPress={() => Navigation.goBack(ROUTES.REPORT_SETTINGS.getRoute(report.reportID))} /> - Report.updatePolicyRoomNameAndNavigate(report, values.roomName)} @@ -100,13 +100,13 @@ function RoomNamePage(props) { > (roomNameInputRef.current = ref)} inputID="roomName" defaultValue={report.reportName} isFocused={isFocused} /> - + ); diff --git a/src/pages/workspace/WorkspaceNewRoomPage.js b/src/pages/workspace/WorkspaceNewRoomPage.js index 548b00254721..c059a54b7c21 100644 --- a/src/pages/workspace/WorkspaceNewRoomPage.js +++ b/src/pages/workspace/WorkspaceNewRoomPage.js @@ -20,14 +20,13 @@ import * as ErrorUtils from '../../libs/ErrorUtils'; import * as ValidationUtils from '../../libs/ValidationUtils'; import * as ReportUtils from '../../libs/ReportUtils'; import * as PolicyUtils from '../../libs/PolicyUtils'; +import Form from '../../components/Form'; import policyMemberPropType from '../policyMemberPropType'; import FullPageNotFoundView from '../../components/BlockingViews/FullPageNotFoundView'; import compose from '../../libs/compose'; import variables from '../../styles/variables'; import useDelayedInputFocus from '../../hooks/useDelayedInputFocus'; import ValuePicker from '../../components/ValuePicker'; -import FormProvider from '../../components/Form/FormProvider'; -import InputWrapper from '../../components/Form/InputWrapper'; const propTypes = { /** All reports shared with the user */ @@ -184,7 +183,7 @@ function WorkspaceNewRoomPage(props) { // This is because when wrapping whole screen the screen was freezing when changing Tabs. keyboardVerticalOffset={variables.contentHeaderHeight + variables.tabSelectorButtonHeight + variables.tabSelectorButtonPadding + insets.top} > - (roomNameInputRef.current = el)} inputID="roomName" isFocused={props.isFocused} shouldDelayFocus @@ -202,8 +201,7 @@ function WorkspaceNewRoomPage(props) { /> - - {isPolicyAdmin && ( - )} - {visibilityDescription} - + )}