Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Form inputs to manipulate related field values #9207

Merged
merged 2 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion FORMS.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,4 @@ Form.js will automatically provide the following props to any input with the inp
- defaultValue: The input default value.
- errorText: The translated error text that is returned by validate for that specific input.
- onBlur: An onBlur handler that calls validate.
- onChange: An onChange handler that saves draft values and calls validate.
- onInputChange: An onChange handler that saves draft values and calls validate for that input (inputA). Passing an inputID as a second param allows inputA to manipulate the input value of the provided inputID (inputB).
8 changes: 6 additions & 2 deletions src/components/AddressSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ const AddressSearch = (props) => {
if (_.size(values) === 0) {
return;
}
props.onInputChange(values);
if (props.inputID) {
_.each(values, (value, key) => props.onInputChange(value, key));
} else {
props.onInputChange(values);
}
};

return (
Expand Down Expand Up @@ -161,7 +165,7 @@ const AddressSearch = (props) => {
label: props.label,
containerStyles: props.containerStyles,
errorText: props.errorText,
hint: props.hint,
hint: displayListViewBorder ? undefined : props.hint,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this because I noticed that the dropdown was showing below the hint, which looked odd.

value: props.value,
defaultValue: props.defaultValue,
inputID: props.inputID,
Expand Down
12 changes: 9 additions & 3 deletions src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,16 @@ class Form extends React.Component {
this.setTouchedInput(inputID);
this.validate(this.inputValues);
},
onInputChange: (value) => {
this.inputValues[inputID] = value;
onInputChange: (value, key) => {
const inputKey = key || inputID;
this.inputValues[inputKey] = value;
const inputRef = this.inputRefs[inputKey];

if (key && inputRef && _.isFunction(inputRef.setNativeProps)) {
inputRef.setNativeProps({value});
}
if (child.props.shouldSaveDraft) {
FormActions.setDraftValues(this.props.formID, {[inputID]: value});
FormActions.setDraftValues(this.props.formID, {[inputKey]: value});
}
this.validate(this.inputValues);
},
Expand Down
10 changes: 5 additions & 5 deletions src/stories/Form.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const Template = (args) => {
/>
<View style={styles.mt4}>
<StatePicker
inputID="pickState"
inputID="state"
shouldSaveDraft
/>
</View>
Expand Down Expand Up @@ -179,8 +179,8 @@ const defaultArgs = {
if (!values.pickAnotherFruit) {
errors.pickAnotherFruit = 'Please select a fruit';
}
if (!values.pickState) {
errors.pickState = 'Please select a state';
if (!values.state) {
errors.state = 'Please select a state';
}
if (!values.checkbox) {
errors.checkbox = 'You must accept the Terms of Service to continue';
Expand All @@ -204,7 +204,7 @@ const defaultArgs = {
dob: '1990-01-01',
pickFruit: 'orange',
pickAnotherFruit: 'apple',
pickState: 'AL',
state: 'AL',
checkbox: false,
},
};
Expand All @@ -221,7 +221,7 @@ InputError.args = {
pickFruit: '',
dob: '',
pickAnotherFruit: '',
pickState: '',
state: '',
checkbox: false,
},
};
Expand Down