From 174fc0c10109280cddd0798be3641ce3d9d3fb93 Mon Sep 17 00:00:00 2001 From: vijayrawatsan Date: Mon, 2 Nov 2015 13:58:31 +0530 Subject: [PATCH] FormstText implementation based on https://github.com/christianalfoni/formsy-react/wiki/validate-on-blur --- formsy-material-ui.jsx | 45 ++++++++++++++++++++++++++++++++++++++++-- index.js | 43 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 83 insertions(+), 5 deletions(-) diff --git a/formsy-material-ui.jsx b/formsy-material-ui.jsx index d2941d1..6046cf2 100644 --- a/formsy-material-ui.jsx +++ b/formsy-material-ui.jsx @@ -100,13 +100,54 @@ let FormsySelect = React.createClass({ }); let FormsyText = React.createClass({ - mixins: [ Formsy.Mixin, FormComponentMixin ], + displayName: 'FormsyText', + + propTypes: { + name: React.PropTypes.string.isRequired, + validations: React.PropTypes.string, + validationError: React.PropTypes.string, + hintText: React.PropTypes.string, + floatingLabelText: React.PropTypes.string + }, + + handleChange: function handleChange(event) { + if(this.getErrorMessage() != null){ + this.setValue(event.currentTarget.value); + } + else{ + if (this.isValidValue(event.target.value)) { + this.setValue(event.target.value); + } + else{ + this.setState({ + _value: event.currentTarget.value, + _isPristine: false + }); + } + } + }, + + handleValueChange: function handleValueChange(event, value) { + this.setValue(value); + }, + + handleBlur: function handleBlur(event) { + this.setValue(event.currentTarget.value); + }, + + handleEnterKeyDown: function handleEnterKeyDown(event) { + this.setValue(event.currentTarget.value); + }, + + mixins: [ Formsy.Mixin ], render: function () { return ( ); diff --git a/index.js b/index.js index 9440d5b..56b4744 100644 --- a/index.js +++ b/index.js @@ -115,11 +115,48 @@ var FormsySelect = React.createClass({ var FormsyText = React.createClass({ displayName: 'FormsyText', - mixins: [Formsy.Mixin, FormComponentMixin], + propTypes: { + name: React.PropTypes.string.isRequired, + validations: React.PropTypes.string, + validationError: React.PropTypes.string, + hintText: React.PropTypes.string, + floatingLabelText: React.PropTypes.string + }, + + handleChange: function handleChange(event) { + if (this.getErrorMessage() != null) { + this.setValue(event.currentTarget.value); + } else { + if (this.isValidValue(event.target.value)) { + this.setValue(event.target.value); + } else { + this.setState({ + _value: event.currentTarget.value, + _isPristine: false + }); + } + } + }, + + handleValueChange: function handleValueChange(event, value) { + this.setValue(value); + }, + + handleBlur: function handleBlur(event) { + this.setValue(event.currentTarget.value); + }, + + handleEnterKeyDown: function handleEnterKeyDown(event) { + this.setValue(event.currentTarget.value); + }, + + mixins: [Formsy.Mixin], render: function render() { return React.createElement(TextField, _extends({}, this.props, { - onBlur: this.handleChange, + onChange: this.handleChange, + onBlur: this.handleBlur, + onEnterKeyDown: this.handleEnterKeyDown, errorText: this.getErrorMessage(), value: this.getValue() })); } @@ -165,4 +202,4 @@ module.exports = { FormsyText: FormsyText, FormsyTime: FormsyTime, FormsyToggle: FormsyToggle -}; +}; \ No newline at end of file