diff --git a/src/components/StatePicker.js b/src/components/StatePicker.js index 11d4cad09f55..96b1a01d596c 100644 --- a/src/components/StatePicker.js +++ b/src/components/StatePicker.js @@ -1,9 +1,10 @@ import _ from 'underscore'; -import React from 'react'; +import React, {forwardRef} from 'react'; import PropTypes from 'prop-types'; import {CONST} from 'expensify-common/lib/CONST'; import Picker from './Picker'; import withLocalize, {withLocalizePropTypes} from './withLocalize'; +import * as FormUtils from '../libs/FormUtils'; const STATES = _.map(CONST.STATES, ({stateISO}) => ({ value: stateISO, @@ -14,31 +15,64 @@ const propTypes = { /** The label for the field */ label: PropTypes.string, - /** A callback method that is called when the value changes and it received the selected value as an argument */ - onChange: PropTypes.func.isRequired, + /** A callback method that is called when the value changes and it receives the selected value as an argument. */ + onInputChange: PropTypes.func.isRequired, /** The value that needs to be selected */ value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), + /** Indicates that the input is being used with the Form component */ + isFormInput: PropTypes.bool, + + /** + * The ID used to uniquely identify the input + * + * @param {Object} props - props passed to the input + * @returns {Object} - returns an Error object if isFormInput is supplied but inputID is falsey or not a string + */ + inputID: props => FormUtils.validateInputIDProps(props), + + /** Saves a draft of the input value when used in a form */ + shouldSaveDraft: PropTypes.bool, + + /** Callback that is called when the text input is blurred */ + onBlur: PropTypes.func, + + /** Error text to display */ + errorText: PropTypes.string, + + /** The default value of the state picker */ + defaultValue: PropTypes.string, + ...withLocalizePropTypes, }; const defaultProps = { label: '', - value: '', + value: undefined, + defaultValue: undefined, + errorText: '', + shouldSaveDraft: false, + isFormInput: false, + inputID: undefined, + onBlur: () => {}, }; -const StatePicker = props => ( +const StatePicker = forwardRef((props, ref) => ( -); +)); StatePicker.propTypes = propTypes; StatePicker.defaultProps = defaultProps; diff --git a/src/pages/ReimbursementAccount/AddressForm.js b/src/pages/ReimbursementAccount/AddressForm.js index fdf0150f92d2..7f19e76516aa 100644 --- a/src/pages/ReimbursementAccount/AddressForm.js +++ b/src/pages/ReimbursementAccount/AddressForm.js @@ -70,9 +70,8 @@ const AddressForm = props => ( props.onFieldChange({state: value})} + onInputChange={value => props.onFieldChange({state: value})} errorText={props.errors.state ? props.translate('bankAccount.error.addressState') : ''} - hasError={Boolean(props.errors.state)} /> diff --git a/src/pages/ReimbursementAccount/CompanyStep.js b/src/pages/ReimbursementAccount/CompanyStep.js index 59dd4149abcb..003be4e821e6 100644 --- a/src/pages/ReimbursementAccount/CompanyStep.js +++ b/src/pages/ReimbursementAccount/CompanyStep.js @@ -85,6 +85,7 @@ class CompanyStep extends React.Component { incorporationDateFuture: 'bankAccount.error.incorporationDateFuture', incorporationType: 'bankAccount.error.companyType', hasNoConnectionToCannabis: 'bankAccount.error.restrictedBusiness', + incorporationState: 'bankAccount.error.incorporationState', }; this.getErrorText = inputKey => ReimbursementAccountUtils.getErrorText(this.props, this.errorTranslationKeys, inputKey); @@ -286,9 +287,9 @@ class CompanyStep extends React.Component { this.clearErrorAndSetValue('incorporationState', value)} + onInputChange={value => this.clearErrorAndSetValue('incorporationState', value)} value={this.state.incorporationState} - hasError={this.getErrors().incorporationState} + errorText={this.getErrorText('incorporationState')} /> this.clearErrorAndSetValue('addressState', value)} + onInputChange={value => this.clearErrorAndSetValue('addressState', value)} value={this.state.addressState} - hasError={lodashGet(this.state.errors, 'addressState', false)} + errorText={this.getErrorText('addressState')} /> diff --git a/src/stories/Form.stories.js b/src/stories/Form.stories.js index 5e45076a8c7a..8aaec3606950 100644 --- a/src/stories/Form.stories.js +++ b/src/stories/Form.stories.js @@ -2,6 +2,7 @@ import React, {useState} from 'react'; import {View} from 'react-native'; import TextInput from '../components/TextInput'; import Picker from '../components/Picker'; +import StatePicker from '../components/StatePicker'; import AddressSearch from '../components/AddressSearch'; import DatePicker from '../components/DatePicker'; import Form from '../components/Form'; @@ -23,6 +24,7 @@ const story = { AddressSearch, CheckboxWithLabel, Picker, + StatePicker, DatePicker, }, }; @@ -105,6 +107,13 @@ const Template = (args) => { ]} isFormInput /> + + +