Skip to content

Commit

Permalink
Use FormsyText internal _value to allow external setValue calls
Browse files Browse the repository at this point in the history
Based on the example at christianalfoni/formsy-react#180
  • Loading branch information
astonm committed Nov 16, 2016
1 parent b3dbbab commit 75d130f
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions src/FormsyText.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ const FormsyText = React.createClass({

mixins: [Formsy.Mixin],


getInitialState() {
return { value: this.controlledValue() };
},

componentWillMount() {
this.setValue(this.controlledValue());
},
Expand All @@ -36,7 +31,6 @@ const FormsyText = React.createClass({
if (isValueChanging || nextProps.defaultValue !== this.props.defaultValue) {
const value = this.controlledValue(nextProps);
if (isValueChanging || this.props.defaultValue === this.getValue()) {
this.setState({ value });
this.setValue(value);
}
}
Expand All @@ -48,7 +42,6 @@ const FormsyText = React.createClass({
// Calling state here is valid, as it cannot cause infinite recursion.
const value = this.controlledValue(nextProps);
this.setValue(value);
this.setState({ value });
}
},

Expand Down Expand Up @@ -78,12 +71,13 @@ const FormsyText = React.createClass({
if (this.isValidValue(event.target.value)) {
this.setValue(event.currentTarget.value);
// If it becomes invalid, and there isn't an error message, invalidate without error.
} else {
this.setState({_value: event.currentTarget.value, _isPristine: false});
}
}
}

// Controlled component
this.setState({ value: event.currentTarget.value });
if (this.props.onChange) this.props.onChange(event, event.currentTarget.value);
},

Expand Down Expand Up @@ -116,7 +110,7 @@ const FormsyText = React.createClass({
onChange={this.handleChange}
onKeyDown={this.handleKeyDown}
ref={this.setMuiComponentAndMaybeFocus}
value={this.state.value}
value={this.getValue()}
/>
);
},
Expand Down

0 comments on commit 75d130f

Please sign in to comment.