Skip to content

Instantly share code, notes, and snippets.

@krizka
Last active May 30, 2016 06:34
Show Gist options
  • Save krizka/6be5cd3fd1a200f1dba9de6363dbe63d to your computer and use it in GitHub Desktop.
Save krizka/6be5cd3fd1a200f1dba9de6363dbe63d to your computer and use it in GitHub Desktop.
Fixed formsy text validation on input
/**
* Created by kriz on 13/05/16.
*/
import React from 'react';
import FormsyText from 'formsy-material-ui/lib/FormsyText';
function debounceFunc(fn, delay) {
var timer = null;
return function () {
const args = arguments;
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, args);
}, delay);
};
}
// fix formsy text for form update
export default FixedFormsyText = React.createClass({
componentDidMount() {
const { debounce = 200 } = this.props;
this.setValidate = debounceFunc(this.refs.input.setValue, debounce);
},
componentWillReceiveProps() {
const input = this.refs.input;
input.setState({ value: input.getValue() || '' });
},
onChange(event) {
if (this.props.onChange)
this.props.onChange(event);
this.setValidate(event.currentTarget.value);
},
render() {
return <FormsyText ref="input" {...this.props} onChange={this.onChange} />
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment