Last active
May 30, 2016 06:34
-
-
Save krizka/6be5cd3fd1a200f1dba9de6363dbe63d to your computer and use it in GitHub Desktop.
Fixed formsy text validation on input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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