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

Use FormsyText internal _value to allow external setValue calls #174

Merged
merged 2 commits into from
Nov 24, 2016
Merged
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
15 changes: 6 additions & 9 deletions src/FormsyText.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ const FormsyText = React.createClass({

mixins: [Formsy.Mixin],

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

componentWillMount() {
this.setValue(this.controlledValue());
Expand All @@ -38,7 +34,7 @@ const FormsyText = React.createClass({
const isValid = this.isValidValue(value);

if (isValueChanging || this.props.defaultValue === this.getValue()) {
this.setState({ value, isValid });
this.setState({ isValid });
this.setValue(value);
}
}
Expand All @@ -51,7 +47,7 @@ const FormsyText = React.createClass({
const value = this.controlledValue(nextProps);
const isValid = this.isValidValue(value);
this.setValue(value);
this.setState({ value, isValid });
this.setState({ isValid });
}
},

Expand Down Expand Up @@ -85,12 +81,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, isValid: this.isValidValue(event.currentTarget.value) });
this.setState({ isValid: this.isValidValue(event.currentTarget.value) });
if (this.props.onChange) this.props.onChange(event, event.currentTarget.value);
},

Expand Down Expand Up @@ -120,7 +117,7 @@ const FormsyText = React.createClass({
onChange={this.handleChange}
onKeyDown={this.handleKeyDown}
ref={this.setMuiComponentAndMaybeFocus}
value={this.state.value}
value={this.getValue()}
underlineStyle={this.state.isValid ? { color: this.validationColor() } : {}}
underlineFocusStyle={this.state.isValid ? { color: this.validationColor() } : {}}
/>
Expand Down