diff --git a/src/components/Form.js b/src/components/Form.js index c169113f809..ac9aaf1673f 100644 --- a/src/components/Form.js +++ b/src/components/Form.js @@ -154,7 +154,7 @@ class Form extends React.Component { this.setTouchedInput(inputID); this.validate(this.inputValues); }, - onChange: (value) => { + onInputChange: (value) => { this.inputValues[inputID] = value; if (child.props.shouldSaveDraft) { FormActions.setDraftValues(this.props.formID, {[inputID]: value}); diff --git a/src/components/RoomNameInput.js b/src/components/RoomNameInput.js index 430572844a2..acbdfcdc36d 100644 --- a/src/components/RoomNameInput.js +++ b/src/components/RoomNameInput.js @@ -3,11 +3,10 @@ import PropTypes from 'prop-types'; import {withOnyx} from 'react-native-onyx'; import CONST from '../CONST'; import ONYXKEYS from '../ONYXKEYS'; -import styles from '../styles/styles'; import compose from '../libs/compose'; import withLocalize, {withLocalizePropTypes} from './withLocalize'; import withFullPolicy, {fullPolicyDefaultProps, fullPolicyPropTypes} from '../pages/workspace/withFullPolicy'; -import TextInputWithPrefix from './TextInputWithPrefix'; +import TextInput from './TextInput'; const propTypes = { /** Callback to execute when the text input is modified correctly */ @@ -103,12 +102,11 @@ class RoomNameInput extends Component { render() { return ( - 0; + const activeLabel = props.forceActiveLabel || this.value.length > 0 || props.prefixCharacter; this.state = { isFocused: false, @@ -28,6 +28,7 @@ class BaseTextInput extends Component { labelScale: new Animated.Value(activeLabel ? styleConst.ACTIVE_LABEL_SCALE : styleConst.INACTIVE_LABEL_SCALE), passwordHidden: props.secureTextEntry, textInputWidth: 0, + prefixWidth: 0, }; this.input = null; @@ -38,6 +39,7 @@ class BaseTextInput extends Component { this.setValue = this.setValue.bind(this); this.togglePasswordVisibility = this.togglePasswordVisibility.bind(this); this.dismissKeyboardWhenBackgrounded = this.dismissKeyboardWhenBackgrounded.bind(this); + this.storePrefixLayoutDimensions = this.storePrefixLayoutDimensions.bind(this); } componentDidMount() { @@ -120,8 +122,8 @@ class BaseTextInput extends Component { * @memberof BaseTextInput */ setValue(value) { - if (this.props.onChange) { - this.props.onChange(value); + if (this.props.onInputChange) { + this.props.onInputChange(value); } this.value = value; Str.result(this.props.onChangeText, value); @@ -141,7 +143,7 @@ class BaseTextInput extends Component { } deactivateLabel() { - if (this.props.forceActiveLabel || this.value.length !== 0) { + if (this.props.forceActiveLabel || this.value.length !== 0 || this.props.prefixCharacter) { return; } @@ -176,6 +178,10 @@ class BaseTextInput extends Component { this.setState(prevState => ({passwordHidden: !prevState.passwordHidden})); } + storePrefixLayoutDimensions(event) { + this.setState({prefixWidth: Math.abs(event.nativeEvent.layout.width)}); + } + render() { // eslint-disable-next-line react/forbid-foreign-prop-types const inputProps = _.omit(this.props, _.keys(baseTextInputPropTypes.propTypes)); @@ -215,7 +221,20 @@ class BaseTextInput extends Component { /> ) : null} - + + {Boolean(this.props.prefixCharacter) && ( + + {this.props.prefixCharacter} + + )} { if (typeof this.props.innerRef === 'function') { this.props.innerRef(ref); } @@ -224,7 +243,7 @@ class BaseTextInput extends Component { // eslint-disable-next-line {...inputProps} defaultValue={this.value} - placeholder={(this.state.isFocused || !this.props.label) ? this.props.placeholder : null} + placeholder={(this.props.prefixCharacter || this.state.isFocused || !this.props.label) ? this.props.placeholder : null} placeholderTextColor={themeColors.placeholderText} underlineColorAndroid="transparent" style={[ @@ -232,6 +251,7 @@ class BaseTextInput extends Component { styles.w100, this.props.inputStyle, !hasLabel && styles.pv0, + this.props.prefixCharacter && StyleUtils.getPaddingLeft(this.state.prefixWidth + styles.pl1.paddingLeft), this.props.secureTextEntry && styles.pr2, ]} multiline={this.props.multiline} diff --git a/src/components/TextInput/baseTextInputPropTypes.js b/src/components/TextInput/baseTextInputPropTypes.js index 1454049c495..e6a612d7320 100644 --- a/src/components/TextInput/baseTextInputPropTypes.js +++ b/src/components/TextInput/baseTextInputPropTypes.js @@ -47,6 +47,17 @@ const propTypes = { /** Forward the inner ref */ innerRef: PropTypes.func, + /** Maximum characters allowed */ + maxLength: PropTypes.number, + + /** Hint text to display below the TextInput */ + hint: PropTypes.string, + + /** Prefix character */ + prefixCharacter: PropTypes.string, + + /** Form props */ + /** Indicates that the input is being used with the Form component */ isFormInput: PropTypes.bool, @@ -61,11 +72,8 @@ const propTypes = { /** Saves a draft of the input value when used in a form */ shouldSaveDraft: PropTypes.bool, - /** Maximum characters allowed */ - maxLength: PropTypes.number, - - /** Hint text to display below the TextInput */ - hint: PropTypes.string, + /** Callback to update the value on Form when input is used in the Form component. */ + onInputChange: PropTypes.func, }; const defaultProps = { @@ -94,6 +102,8 @@ const defaultProps = { shouldSaveDraft: false, maxLength: null, hint: '', + prefixCharacter: '', + onInputChange: () => {}, }; export {propTypes, defaultProps}; diff --git a/src/components/TextInputWithPrefix/index.android.js b/src/components/TextInputWithPrefix/index.android.js deleted file mode 100644 index b04f68bd149..00000000000 --- a/src/components/TextInputWithPrefix/index.android.js +++ /dev/null @@ -1,58 +0,0 @@ -import PropTypes from 'prop-types'; -import {View} from 'react-native'; -import _ from 'underscore'; -import React from 'react'; -import RNTextInput from '../RNTextInput'; -import Text from '../Text'; -import styles from '../../styles/styles'; -import InlineErrorText from '../InlineErrorText'; - -const propTypes = { - /** Prefix character */ - prefixCharacter: PropTypes.string.isRequired, - - /** Text to show if there is an error */ - errorText: PropTypes.string, - - /** Whether to disable the field and style */ - disabled: PropTypes.bool, - -}; - -const defaultProps = { - errorText: '', - disabled: false, -}; - -const TextInputWithPrefix = props => ( - <> - - {props.prefixCharacter} - - - {!_.isEmpty(props.errorText) && ( - - {props.errorText} - - )} - -); - -TextInputWithPrefix.propTypes = propTypes; -TextInputWithPrefix.defaultProps = defaultProps; -export default TextInputWithPrefix; diff --git a/src/components/TextInputWithPrefix/index.js b/src/components/TextInputWithPrefix/index.js deleted file mode 100644 index fc64d65a737..00000000000 --- a/src/components/TextInputWithPrefix/index.js +++ /dev/null @@ -1,56 +0,0 @@ -import PropTypes from 'prop-types'; -import {View} from 'react-native'; -import _ from 'underscore'; -import React from 'react'; -import RNTextInput from '../RNTextInput'; -import Text from '../Text'; -import styles from '../../styles/styles'; -import InlineErrorText from '../InlineErrorText'; - -const propTypes = { - /** Prefix character */ - prefixCharacter: PropTypes.string.isRequired, - - /** Text to show if there is an error */ - errorText: PropTypes.string, - - /** Whether to disable the field and style */ - disabled: PropTypes.bool, - -}; - -const defaultProps = { - errorText: '', - disabled: false, -}; - -const TextInputWithPrefix = props => ( - <> - - {props.prefixCharacter} - - - {!_.isEmpty(props.errorText) && ( - - {props.errorText} - - )} - -); - -TextInputWithPrefix.propTypes = propTypes; -TextInputWithPrefix.defaultProps = defaultProps; -export default TextInputWithPrefix; diff --git a/src/pages/ReportSettingsPage.js b/src/pages/ReportSettingsPage.js index ab9b6e7130f..724c1a013dd 100644 --- a/src/pages/ReportSettingsPage.js +++ b/src/pages/ReportSettingsPage.js @@ -188,7 +188,7 @@ class ReportSettingsPage extends Component { {this.props.translate('newRoomPage.roomName')} - +