Skip to content

Commit

Permalink
Merge pull request #16 from FoxxMD/master
Browse files Browse the repository at this point in the history
Add method for checking if input has had interaction
  • Loading branch information
christianalfoni committed Jan 21, 2015
2 parents 1ec5868 + 418c833 commit a008b1c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 14 deletions.
50 changes: 40 additions & 10 deletions releases/formsy-react.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,15 @@ var request = function (method, url, data, contentType, headers) {
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {

if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.responseText ? JSON.parse(xhr.responseText) : null);
} else {
reject(xhr.responseText ? JSON.parse(xhr.responseText) : null);
try {
var response = xhr.responseText ? JSON.parse(xhr.responseText) : null;
if (xhr.status >= 200 && xhr.status < 300) {
resolve(response);
} else {
reject(response);
}
} catch (e) {
reject(e);
}

}
Expand All @@ -95,7 +100,8 @@ Formsy.Mixin = {
getInitialState: function () {
return {
_value: this.props.value ? this.props.value : '',
_isValid: true
_isValid: true,
_isPristine: true
};
},
componentWillMount: function () {
Expand Down Expand Up @@ -133,14 +139,16 @@ Formsy.Mixin = {
// We validate after the value has been set
setValue: function (value) {
this.setState({
_value: value
_value: value,
_isPristine: false
}, function () {
this.props._validate(this);
}.bind(this));
},
resetValue: function () {
this.setState({
_value: ''
_value: '',
_isPristine: true
}, function () {
this.props._validate(this);
});
Expand All @@ -157,6 +165,9 @@ Formsy.Mixin = {
isValid: function () {
return this.state._isValid;
},
isPristine: function () {
return this.state._isPristine;
},
isRequired: function () {
return this.props.required;
},
Expand Down Expand Up @@ -207,6 +218,11 @@ Formsy.Form = React.createClass({
submit: function (event) {
event.preventDefault();

// Trigger form as not pristine.
// If any inputs have not been touched yet this will make them dirty
// so validation becomes visible (if based on isPristine)
this.setFormPristine(false);

// To support use cases where no async or request operation is needed.
// The "onSubmit" callback is called with the model e.g. {fieldName: "myValue"},
// if wanting to reset the entire form to original state, the second param is a callback for this.
Expand All @@ -223,12 +239,12 @@ Formsy.Form = React.createClass({

this.props.onSubmit();

var headers = (Object.keys(this.props.headers).length && this.props.headers) || options.headers;
var headers = (Object.keys(this.props.headers).length && this.props.headers) || options.headers || {};

ajax[this.props.method || 'post'](this.props.url, this.model, this.props.contentType || options.contentType || 'json', headers)
.then(function (response) {
this.onSuccess(response);
this.onSubmitted();
this.props.onSuccess(response);
this.props.onSubmitted();
}.bind(this))
.catch(this.failSubmit);
},
Expand Down Expand Up @@ -305,6 +321,20 @@ Formsy.Form = React.createClass({
}.bind(this), {});
},

setFormPristine: function(isPristine) {
var inputs = this.inputs;
var inputKeys = Object.keys(inputs);

// Iterate through each component and set it as pristine
// or "dirty".
inputKeys.forEach(function (name, index) {
var component = inputs[name];
component.setState({
_isPristine: isPristine
});
}.bind(this));
},

// Use the binded values and the actual input value to
// validate the input and set its state. Then check the
// state of the form itself
Expand Down
2 changes: 1 addition & 1 deletion releases/formsy-react.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a008b1c

Please sign in to comment.