From 79e65b593890bd6aa4d6bd10ff7d7f11ecf2bccb Mon Sep 17 00:00:00 2001 From: jczekalski Date: Fri, 21 Jul 2023 13:19:58 +0200 Subject: [PATCH 1/3] migrate web --- src/components/RoomNameInput/index.js | 97 ++++++++++----------------- 1 file changed, 35 insertions(+), 62 deletions(-) diff --git a/src/components/RoomNameInput/index.js b/src/components/RoomNameInput/index.js index 0acfde3dedcf..08a6c434f224 100644 --- a/src/components/RoomNameInput/index.js +++ b/src/components/RoomNameInput/index.js @@ -1,4 +1,4 @@ -import React, {Component} from 'react'; +import React, {forwardRef, useState} from 'react'; import _ from 'underscore'; import CONST from '../../CONST'; import withLocalize from '../withLocalize'; @@ -6,30 +6,21 @@ import TextInput from '../TextInput'; import * as roomNameInputPropTypes from './roomNameInputPropTypes'; import * as RoomNameInputUtils from '../../libs/RoomNameInputUtils'; -class RoomNameInput extends Component { - constructor(props) { - super(props); - - this.setModifiedRoomName = this.setModifiedRoomName.bind(this); - this.setSelection = this.setSelection.bind(this); - - this.state = { - selection: undefined, - }; - } +const RoomNameInput = forwardRef((props, ref) => { + const [selection, setSelection] = useState(); /** * Calls the onChangeText callback with a modified room name * @param {Event} event */ - setModifiedRoomName(event) { + const setModifiedRoomName = (event) => { const roomName = event.nativeEvent.text; const modifiedRoomName = RoomNameInputUtils.modifyRoomName(roomName); - this.props.onChangeText(modifiedRoomName); + props.onChangeText(modifiedRoomName); // if custom component has onInputChange, use it to trigger changes (Form input) - if (_.isFunction(this.props.onInputChange)) { - this.props.onInputChange(modifiedRoomName); + if (_.isFunction(props.onInputChange)) { + props.onInputChange(modifiedRoomName); } // Prevent cursor jump behaviour: @@ -37,59 +28,41 @@ class RoomNameInput extends Component { // 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 = this.props.value || ''; + const oldRoomNameWithHash = props.value || ''; const newRoomNameWithHash = `${CONST.POLICY.ROOM_PREFIX}${roomName}`; if (modifiedRoomName !== newRoomNameWithHash) { const offset = modifiedRoomName.length - oldRoomNameWithHash.length; - const selection = { - start: this.state.selection.start + offset, - end: this.state.selection.end + offset, + const newSelection = { + start: selection.start + offset, + end: selection.end + offset, }; - this.setSelection(selection); + setSelection(newSelection); } - } + }; - /** - * Set the selection - * @param {Object} selection - */ - setSelection(selection) { - this.setState({selection}); - } - - render() { - return ( - this.setSelection(event.nativeEvent.selection)} - errorText={this.props.errorText} - autoCapitalize="none" - onBlur={this.props.onBlur} - autoFocus={this.props.autoFocus} - maxLength={CONST.REPORT.MAX_ROOM_NAME_LENGTH} - /> - ); - } -} + return ( + setSelection(event.nativeEvent.selection)} + errorText={props.errorText} + autoCapitalize="none" + onBlur={props.onBlur} + autoFocus={props.autoFocus} + maxLength={CONST.REPORT.MAX_ROOM_NAME_LENGTH} + /> + ); +}); RoomNameInput.propTypes = roomNameInputPropTypes.propTypes; RoomNameInput.defaultProps = roomNameInputPropTypes.defaultProps; -export default withLocalize( - React.forwardRef((props, ref) => ( - - )), -); +export default withLocalize(RoomNameInput); From 9c2628ca1e7f8697a7dd9f47e39454063d11b429 Mon Sep 17 00:00:00 2001 From: jczekalski Date: Fri, 21 Jul 2023 13:58:29 +0200 Subject: [PATCH 2/3] migrate native --- src/components/RoomNameInput/index.native.js | 77 ++++++++------------ 1 file changed, 31 insertions(+), 46 deletions(-) diff --git a/src/components/RoomNameInput/index.native.js b/src/components/RoomNameInput/index.native.js index 699024934074..401df3d83472 100644 --- a/src/components/RoomNameInput/index.native.js +++ b/src/components/RoomNameInput/index.native.js @@ -1,4 +1,4 @@ -import React, {Component} from 'react'; +import React, {forwardRef} from 'react'; import _ from 'underscore'; import CONST from '../../CONST'; import withLocalize from '../withLocalize'; @@ -7,62 +7,47 @@ import * as roomNameInputPropTypes from './roomNameInputPropTypes'; import * as RoomNameInputUtils from '../../libs/RoomNameInputUtils'; import getOperatingSystem from '../../libs/getOperatingSystem'; -class RoomNameInput extends Component { - constructor(props) { - super(props); - - this.setModifiedRoomName = this.setModifiedRoomName.bind(this); - } - +const RoomNameInput = forwardRef((props, ref) => { /** * Calls the onChangeText callback with a modified room name * @param {Event} event */ - setModifiedRoomName(event) { + const setModifiedRoomName = (event) => { const roomName = event.nativeEvent.text; const modifiedRoomName = RoomNameInputUtils.modifyRoomName(roomName); - this.props.onChangeText(modifiedRoomName); + props.onChangeText(modifiedRoomName); // if custom component has onInputChange, use it to trigger changes (Form input) - if (_.isFunction(this.props.onInputChange)) { - this.props.onInputChange(modifiedRoomName); + if (_.isFunction(props.onInputChange)) { + props.onInputChange(modifiedRoomName); } - } + }; - render() { - const keyboardType = getOperatingSystem() === CONST.OS.IOS ? CONST.KEYBOARD_TYPE.ASCII_CAPABLE : CONST.KEYBOARD_TYPE.VISIBLE_PASSWORD; - return ( - - ); - } -} + const keyboardType = getOperatingSystem() === CONST.OS.IOS ? CONST.KEYBOARD_TYPE.ASCII_CAPABLE : CONST.KEYBOARD_TYPE.VISIBLE_PASSWORD; + + return ( + + ); +}); RoomNameInput.propTypes = roomNameInputPropTypes.propTypes; RoomNameInput.defaultProps = roomNameInputPropTypes.defaultProps; -export default withLocalize( - React.forwardRef((props, ref) => ( - - )), -); +export default withLocalize(RoomNameInput); From 6b2e85a5f5db11cbc91c5ae8d6cc7857ccb7e9c2 Mon Sep 17 00:00:00 2001 From: jczekalski Date: Mon, 24 Jul 2023 19:01:34 +0200 Subject: [PATCH 3/3] syntax improvements --- src/components/RoomNameInput/index.js | 45 +++++++++++-------- src/components/RoomNameInput/index.native.js | 45 +++++++++++-------- .../RoomNameInput/roomNameInputPropTypes.js | 3 -- 3 files changed, 54 insertions(+), 39 deletions(-) diff --git a/src/components/RoomNameInput/index.js b/src/components/RoomNameInput/index.js index 08a6c434f224..24824c306fe7 100644 --- a/src/components/RoomNameInput/index.js +++ b/src/components/RoomNameInput/index.js @@ -1,12 +1,14 @@ -import React, {forwardRef, useState} from 'react'; +import React, {useState} from 'react'; import _ from 'underscore'; import CONST from '../../CONST'; -import withLocalize from '../withLocalize'; import TextInput from '../TextInput'; +import useLocalize from '../../hooks/useLocalize'; import * as roomNameInputPropTypes from './roomNameInputPropTypes'; import * as RoomNameInputUtils from '../../libs/RoomNameInputUtils'; -const RoomNameInput = forwardRef((props, ref) => { +function RoomNameInput({autoFocus, disabled, errorText, forwardedRef, value, onBlur, onChangeText, onInputChange}) { + const {translate} = useLocalize(); + const [selection, setSelection] = useState(); /** @@ -16,11 +18,11 @@ const RoomNameInput = forwardRef((props, ref) => { const setModifiedRoomName = (event) => { const roomName = event.nativeEvent.text; const modifiedRoomName = RoomNameInputUtils.modifyRoomName(roomName); - props.onChangeText(modifiedRoomName); + onChangeText(modifiedRoomName); // if custom component has onInputChange, use it to trigger changes (Form input) - if (_.isFunction(props.onInputChange)) { - props.onInputChange(modifiedRoomName); + if (_.isFunction(onInputChange)) { + onInputChange(modifiedRoomName); } // Prevent cursor jump behaviour: @@ -28,7 +30,7 @@ const RoomNameInput = forwardRef((props, ref) => { // 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 = props.value || ''; + const oldRoomNameWithHash = value || ''; const newRoomNameWithHash = `${CONST.POLICY.ROOM_PREFIX}${roomName}`; if (modifiedRoomName !== newRoomNameWithHash) { const offset = modifiedRoomName.length - oldRoomNameWithHash.length; @@ -42,27 +44,34 @@ const RoomNameInput = forwardRef((props, ref) => { return ( setSelection(event.nativeEvent.selection)} - errorText={props.errorText} + errorText={errorText} autoCapitalize="none" - onBlur={props.onBlur} - autoFocus={props.autoFocus} + onBlur={onBlur} + autoFocus={autoFocus} maxLength={CONST.REPORT.MAX_ROOM_NAME_LENGTH} /> ); -}); +} RoomNameInput.propTypes = roomNameInputPropTypes.propTypes; RoomNameInput.defaultProps = roomNameInputPropTypes.defaultProps; +RoomNameInput.displayName = 'RoomNameInput'; -export default withLocalize(RoomNameInput); +export default React.forwardRef((props, ref) => ( + +)); diff --git a/src/components/RoomNameInput/index.native.js b/src/components/RoomNameInput/index.native.js index 401df3d83472..e263321dea0e 100644 --- a/src/components/RoomNameInput/index.native.js +++ b/src/components/RoomNameInput/index.native.js @@ -1,13 +1,15 @@ -import React, {forwardRef} from 'react'; +import React from 'react'; import _ from 'underscore'; import CONST from '../../CONST'; -import withLocalize from '../withLocalize'; +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'; -const RoomNameInput = forwardRef((props, ref) => { +function RoomNameInput({autoFocus, disabled, errorText, forwardedRef, value, onBlur, onChangeText, onInputChange, shouldDelayFocus}) { + const {translate} = useLocalize(); + /** * Calls the onChangeText callback with a modified room name * @param {Event} event @@ -15,11 +17,11 @@ const RoomNameInput = forwardRef((props, ref) => { const setModifiedRoomName = (event) => { const roomName = event.nativeEvent.text; const modifiedRoomName = RoomNameInputUtils.modifyRoomName(roomName); - props.onChangeText(modifiedRoomName); + onChangeText(modifiedRoomName); // if custom component has onInputChange, use it to trigger changes (Form input) - if (_.isFunction(props.onInputChange)) { - props.onInputChange(modifiedRoomName); + if (_.isFunction(onInputChange)) { + onInputChange(modifiedRoomName); } }; @@ -27,27 +29,34 @@ const RoomNameInput = forwardRef((props, ref) => { return ( ); -}); +} RoomNameInput.propTypes = roomNameInputPropTypes.propTypes; RoomNameInput.defaultProps = roomNameInputPropTypes.defaultProps; +RoomNameInput.displayName = 'RoomNameInput'; -export default withLocalize(RoomNameInput); +export default React.forwardRef((props, ref) => ( + +)); diff --git a/src/components/RoomNameInput/roomNameInputPropTypes.js b/src/components/RoomNameInput/roomNameInputPropTypes.js index de233b8d96d6..3eef833a1252 100644 --- a/src/components/RoomNameInput/roomNameInputPropTypes.js +++ b/src/components/RoomNameInput/roomNameInputPropTypes.js @@ -1,5 +1,4 @@ import PropTypes from 'prop-types'; -import {withLocalizePropTypes} from '../withLocalize'; const propTypes = { /** Callback to execute when the text input is modified correctly */ @@ -14,8 +13,6 @@ const propTypes = { /** Error text to show */ errorText: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.object]))]), - ...withLocalizePropTypes, - /** A ref forwarded to the TextInput */ forwardedRef: PropTypes.func,