From 75d130faf15987ce1903752118e356c4259be5d2 Mon Sep 17 00:00:00 2001 From: Aston Motes Date: Tue, 15 Nov 2016 17:08:15 -0800 Subject: [PATCH] Use FormsyText internal _value to allow external setValue calls Based on the example at https://github.com/christianalfoni/formsy-react/issues/180 --- src/FormsyText.jsx | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/FormsyText.jsx b/src/FormsyText.jsx index a7da0c0..1618fef 100644 --- a/src/FormsyText.jsx +++ b/src/FormsyText.jsx @@ -22,11 +22,6 @@ const FormsyText = React.createClass({ mixins: [Formsy.Mixin], - - getInitialState() { - return { value: this.controlledValue() }; - }, - componentWillMount() { this.setValue(this.controlledValue()); }, @@ -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); } } @@ -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 }); } }, @@ -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); }, @@ -116,7 +110,7 @@ const FormsyText = React.createClass({ onChange={this.handleChange} onKeyDown={this.handleKeyDown} ref={this.setMuiComponentAndMaybeFocus} - value={this.state.value} + value={this.getValue()} /> ); },