Skip to content

Commit

Permalink
add js docs, rm onBlur handler, rm touched input check in onChange, a…
Browse files Browse the repository at this point in the history
…dd value decorators
  • Loading branch information
luacmartins committed Feb 1, 2022
1 parent fd38d13 commit 25fed0e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
15 changes: 8 additions & 7 deletions src/components/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ class Form extends React.Component {
return errors;
}

/**
* Loops over Form's children and automatically supplies Form props to them
*
* @param {Array} children - An array containing all Form children
* @returns {React.Component}
*/
childrenWrapperWithProps(children) {
return React.Children.map(children, (child) => {
// Just render the child if it is not a valid React element, e.g. text within a <Text> component
Expand All @@ -153,20 +159,15 @@ class Form extends React.Component {
ref: node => this.inputRefs[inputID] = node,
defaultValue: this.props.draftValues[inputID] || child.props.defaultValue,
errorText: this.state.errors[inputID] || '',
onBlur: (event) => {
onBlur: () => {
this.setTouchedInput(inputID);
this.validate(this.getValues());
if (child.props.onBlur) {
child.props.onBlur(event);
}
},
onChange: (value) => {
if (child.props.shouldSaveDraft) {
FormActions.setDraftValues(this.props.formID, {[inputID]: value});
}
if (this.touchedInputs[inputID]) {
this.validate(this.getValues());
}
this.validate(this.getValues());
},
});
});
Expand Down
6 changes: 5 additions & 1 deletion src/components/TextInput/BaseTextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ class BaseTextInput extends Component {
* @memberof BaseTextInput
*/
setValue(value) {
this.value = value;
this.input.value = this.value;
if (this.props.onChange) {
this.props.onChange(value);
}
this.value = value;
Str.result(this.props.onChangeText, value);
this.activateLabel();
}
Expand Down Expand Up @@ -146,6 +147,9 @@ class BaseTextInput extends Component {
// eslint-disable-next-line react/forbid-foreign-prop-types
const inputProps = _.omit(this.props, _.keys(baseTextInputPropTypes.propTypes));
const hasLabel = Boolean(this.props.label.length);
if (this.input) {
this.input.value = this.value;
}
return (
<View>
<View
Expand Down

0 comments on commit 25fed0e

Please sign in to comment.