Skip to content

Commit

Permalink
Revert "Revert "[Form Provider Refactor] RoomNameInput fixes""
Browse files Browse the repository at this point in the history
This reverts commit a60110b.
  • Loading branch information
kowczarz committed Dec 4, 2023
1 parent 28d0ba9 commit 9fa1591
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 134 deletions.
22 changes: 15 additions & 7 deletions src/components/Form/FormProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ function FormProvider({validate, formID, shouldValidateOnBlur, shouldValidateOnC
.first()
.value() || '';

const value = !_.isUndefined(inputValues[`${inputID}ToDisplay`]) ? inputValues[`${inputID}ToDisplay`] : inputValues[inputID];

return {
...propsToParse,
ref:
Expand All @@ -254,7 +256,7 @@ function FormProvider({validate, formID, shouldValidateOnBlur, shouldValidateOnC
inputID,
key: propsToParse.key || inputID,
errorText: errors[inputID] || fieldErrorMessage,
value: inputValues[inputID],
value,
// As the text input is controlled, we never set the defaultValue prop
// as this is already happening by the value prop.
defaultValue: undefined,
Expand Down Expand Up @@ -314,13 +316,19 @@ function FormProvider({validate, formID, shouldValidateOnBlur, shouldValidateOnC
propsToParse.onBlur(event);
}
},
onInputChange: (value, key) => {
onInputChange: (inputValue, key) => {
const inputKey = key || inputID;
setInputValues((prevState) => {
const newState = {
...prevState,
[inputKey]: value,
};
const newState = _.isFunction(propsToParse.valueParser)
? {
...prevState,
[inputKey]: propsToParse.valueParser(inputValue),
[`${inputKey}ToDisplay`]: _.isFunction(propsToParse.displayParser) ? propsToParse.displayParser(inputValue) : inputValue,
}
: {
...prevState,
[inputKey]: inputValue,
};

if (shouldValidateOnChange) {
onValidate(newState);
Expand All @@ -333,7 +341,7 @@ function FormProvider({validate, formID, shouldValidateOnBlur, shouldValidateOnC
}

if (_.isFunction(propsToParse.onValueChange)) {
propsToParse.onValueChange(value, inputKey);
propsToParse.onValueChange(inputValue, inputKey);
}
},
};
Expand Down
4 changes: 4 additions & 0 deletions src/components/Form/InputWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ const propTypes = {
inputID: PropTypes.string.isRequired,
valueType: PropTypes.string,
forwardedRef: refPropTypes,
valueParser: PropTypes.func,
displayParser: PropTypes.func,
};

const defaultProps = {
forwardedRef: undefined,
valueType: 'string',
valueParser: undefined,
displayParser: undefined,
};

function InputWrapper(props) {
Expand Down
53 changes: 14 additions & 39 deletions src/components/RoomNameInput/index.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,43 @@
import React, {useState} from 'react';
import _ from 'underscore';
import React from 'react';
import InputWrapper from '@components/Form/InputWrapper';
import TextInput from '@components/TextInput';
import useLocalize from '@hooks/useLocalize';
import getOperatingSystem from '@libs/getOperatingSystem';
import * as RoomNameInputUtils from '@libs/RoomNameInputUtils';
import CONST from '@src/CONST';
import * as roomNameInputPropTypes from './roomNameInputPropTypes';

function RoomNameInput({isFocused, autoFocus, disabled, errorText, forwardedRef, value, onBlur, onChangeText, onInputChange, shouldDelayFocus}) {
function RoomNameInput({isFocused, autoFocus, disabled, errorText, forwardedRef, onBlur, shouldDelayFocus, inputID, roomName}) {
const {translate} = useLocalize();

const [selection, setSelection] = useState();
const keyboardType = getOperatingSystem() === CONST.OS.IOS ? CONST.KEYBOARD_TYPE.ASCII_CAPABLE : CONST.KEYBOARD_TYPE.VISIBLE_PASSWORD;

/**
* 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);
}
};
const valueParser = (innerRoomName) => RoomNameInputUtils.modifyRoomName(innerRoomName);
const displayParser = (innerRoomName) => RoomNameInputUtils.modifyRoomName(innerRoomName, true);

return (
<TextInput
<InputWrapper
InputComponent={TextInput}
inputID={inputID}
ref={forwardedRef}
disabled={disabled}
label={translate('newRoomPage.roomName')}
accessibilityLabel={translate('newRoomPage.roomName')}
role={CONST.ACCESSIBILITY_ROLE.TEXT}
prefixCharacter={CONST.POLICY.ROOM_PREFIX}
placeholder={translate('newRoomPage.social')}
onChange={setModifiedRoomName}
value={value.substring(1)} // Since the room name always starts with a prefix, we omit the first character to avoid displaying it twice.
selection={selection}
onSelectionChange={(event) => setSelection(event.nativeEvent.selection)}
errorText={errorText}
valueParser={valueParser}
displayParser={displayParser}
autoCapitalize="none"
onBlur={(event) => isFocused && onBlur(event)}
shouldDelayFocus={shouldDelayFocus}
autoFocus={isFocused && autoFocus}
maxLength={CONST.REPORT.MAX_ROOM_NAME_LENGTH}
defaultValue={roomName}
spellCheck={false}
shouldInterceptSwipe
keyboardType={keyboardType} // this is a bit hacky solution to a RN issue https://github.com/facebook/react-native/issues/27449
/>
);
}
Expand Down
66 changes: 0 additions & 66 deletions src/components/RoomNameInput/index.native.js

This file was deleted.

9 changes: 6 additions & 3 deletions src/components/RoomNameInput/roomNameInputPropTypes.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import PropTypes from 'prop-types';
import refPropTypes from '@components/refPropTypes';

const propTypes = {
/** Callback to execute when the text input is modified correctly */
Expand All @@ -14,10 +15,10 @@ const propTypes = {
errorText: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.object]))]),

/** A ref forwarded to the TextInput */
forwardedRef: PropTypes.func,
forwardedRef: refPropTypes,

/** The ID used to uniquely identify the input in a Form */
inputID: PropTypes.string,
inputID: PropTypes.string.isRequired,

/** Callback that is called when the text input is blurred */
onBlur: PropTypes.func,
Expand All @@ -30,6 +31,8 @@ const propTypes = {

/** Whether navigation is focused */
isFocused: PropTypes.bool.isRequired,

roomName: PropTypes.string,
};

const defaultProps = {
Expand All @@ -39,10 +42,10 @@ const defaultProps = {
errorText: '',
forwardedRef: () => {},

inputID: undefined,
onBlur: () => {},
autoFocus: false,
shouldDelayFocus: false,
roomName: '',
};

export {propTypes, defaultProps};
4 changes: 2 additions & 2 deletions src/libs/RoomNameInputUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import CONST from '@src/CONST';
/**
* Replaces spaces with dashes
*/
function modifyRoomName(roomName: string): string {
function modifyRoomName(roomName: string, skipPolicyPrefix?: boolean): string {
const modifiedRoomNameWithoutHash = roomName
.replace(/ /g, '-')

// Replaces the smart dash on iOS devices with two hyphens
.replace(//g, '--');

return `${CONST.POLICY.ROOM_PREFIX}${modifiedRoomNameWithoutHash}`;
return skipPolicyPrefix ? modifiedRoomNameWithoutHash : `${CONST.POLICY.ROOM_PREFIX}${modifiedRoomNameWithoutHash}`;
}

export {
Expand Down
16 changes: 6 additions & 10 deletions src/pages/settings/Report/RoomNamePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, {useCallback, useRef} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import Form from '@components/Form';
import FormProvider from '@components/Form/FormProvider';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import RoomNameInput from '@components/RoomNameInput';
import ScreenWrapper from '@components/ScreenWrapper';
Expand Down Expand Up @@ -42,13 +42,8 @@ const defaultProps = {
policy: {},
};

function RoomNamePage(props) {
function RoomNamePage({policy, report, reports, translate}) {
const styles = useThemeStyles();
const policy = props.policy;
const report = props.report;
const reports = props.reports;
const translate = props.translate;

const roomNameInputRef = useRef(null);
const isFocused = useIsFocused();

Expand Down Expand Up @@ -91,7 +86,7 @@ function RoomNamePage(props) {
title={translate('newRoomPage.roomName')}
onBackButtonPress={() => Navigation.goBack(ROUTES.REPORT_SETTINGS.getRoute(report.reportID))}
/>
<Form
<FormProvider
style={[styles.flexGrow1, styles.ph5]}
formID={ONYXKEYS.FORMS.ROOM_NAME_FORM}
onSubmit={(values) => Report.updatePolicyRoomNameAndNavigate(report, values.roomName)}
Expand All @@ -101,13 +96,14 @@ function RoomNamePage(props) {
>
<View style={styles.mb4}>
<RoomNameInput
ref={(ref) => (roomNameInputRef.current = ref)}
ref={roomNameInputRef}
inputID="roomName"
defaultValue={report.reportName}
isFocused={isFocused}
roomName={report.reportName.slice(1)}
/>
</View>
</Form>
</FormProvider>
</FullPageNotFoundView>
</ScreenWrapper>
);
Expand Down
19 changes: 12 additions & 7 deletions src/pages/workspace/WorkspaceNewRoomPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import Form from '@components/Form';
import FormProvider from '@components/Form/FormProvider';
import InputWrapper from '@components/Form/InputWrapper';
import KeyboardAvoidingView from '@components/KeyboardAvoidingView';
import OfflineIndicator from '@components/OfflineIndicator';
import RoomNameInput from '@components/RoomNameInput';
Expand Down Expand Up @@ -239,7 +240,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}
>
<Form
<FormProvider
formID={ONYXKEYS.FORMS.NEW_ROOM_FORM}
submitButtonText={translate('newRoomPage.createRoom')}
style={[styles.mh5, styles.flexGrow1]}
Expand All @@ -257,7 +258,8 @@ function WorkspaceNewRoomPage(props) {
/>
</View>
<View style={styles.mb5}>
<TextInput
<InputWrapper
InputComponent={TextInput}
inputID="welcomeMessage"
label={translate('welcomeMessagePage.welcomeMessageOptional')}
accessibilityLabel={translate('welcomeMessagePage.welcomeMessageOptional')}
Expand All @@ -269,7 +271,8 @@ function WorkspaceNewRoomPage(props) {
/>
</View>
<View style={[styles.mhn5]}>
<ValuePicker
<InputWrapper
InputComponent={ValuePicker}
inputID="policyID"
label={translate('workspace.common.workspace')}
items={workspaceOptions}
Expand All @@ -278,7 +281,8 @@ function WorkspaceNewRoomPage(props) {
</View>
{isPolicyAdmin && (
<View style={styles.mhn5}>
<ValuePicker
<InputWrapper
InputComponent={ValuePicker}
inputID="writeCapability"
label={translate('writeCapabilityPage.label')}
items={writeCapabilityOptions}
Expand All @@ -288,7 +292,8 @@ function WorkspaceNewRoomPage(props) {
</View>
)}
<View style={[styles.mb1, styles.mhn5]}>
<ValuePicker
<InputWrapper
InputComponent={ValuePicker}
inputID="visibility"
label={translate('newRoomPage.visibility')}
items={visibilityOptions}
Expand All @@ -297,7 +302,7 @@ function WorkspaceNewRoomPage(props) {
/>
</View>
<Text style={[styles.textLabel, styles.colorMuted]}>{visibilityDescription}</Text>
</Form>
</FormProvider>
{isSmallScreenWidth && <OfflineIndicator />}
</KeyboardAvoidingView>
)}
Expand Down

0 comments on commit 9fa1591

Please sign in to comment.