react-loose-forms
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -1,2 +0,2 @@ | ||
var _ = require('lodash'); | ||
var is = require("is"); | ||
@@ -6,3 +6,3 @@ module.exports = { | ||
var onChange = this.props.onChange; | ||
if(_.isFunction(onChange)){ | ||
if(is.fn(onChange)){ | ||
onChange(this.props.field_path, new_value); | ||
@@ -9,0 +9,0 @@ } |
125
FormMixin.js
@@ -1,2 +0,3 @@ | ||
var _ = require('lodash'); | ||
var is = require('is'); | ||
var clone = require('clone'); | ||
var InputTypes = require('./InputTypes'); | ||
@@ -7,3 +8,3 @@ | ||
return { | ||
initial_values_source_version: _.isFunction(this.getInitialValuesSourceVersion) ? this.getInitialValuesSourceVersion(this.props) : null, | ||
initial_values_source_version: this.____getInitialValuesSourceVersion(this.props), | ||
data: this.____getInitialValues(this.props), | ||
@@ -15,18 +16,16 @@ errors: {}, | ||
componentWillReceiveProps: function(new_props){ | ||
if(_.isFunction(this.getInitialValuesSourceVersion)){ | ||
var initial_values_source_version = this.getInitialValuesSourceVersion(new_props); | ||
if(this.state.initial_values_source_version !== initial_values_source_version){ | ||
this.setState({initial_values_source_version: initial_values_source_version, data: this.____getInitialValues(new_props), errors: {}, submit_attempts: 0}); | ||
} | ||
var initial_values_source_version = this.____getInitialValuesSourceVersion(new_props); | ||
if(this.state.initial_values_source_version !== initial_values_source_version){ | ||
this.setState({ | ||
initial_values_source_version: initial_values_source_version, | ||
data: this.____getInitialValues(new_props), | ||
errors: {}, | ||
submit_attempts: 0 | ||
}); | ||
} | ||
}, | ||
Form_validate: function(callback){ | ||
var fields = this.buildFields(); | ||
Form_validate: function(){ | ||
var fields = this.____buildSchema(); | ||
var data = this.state.data; | ||
this.setState({errors: validateFields(fields, data)}, function(){ | ||
if(_.isFunction(callback)){ | ||
callback(); | ||
} | ||
} | ||
); | ||
return validateFields(fields, data); | ||
}, | ||
@@ -38,27 +37,16 @@ Form_onSubmit: function(e){ | ||
var self = this; | ||
var fields = this.buildFields(); | ||
var data = this.state.data; | ||
this.setState({ | ||
submit_attempts: this.state.submit_attempts + 1, | ||
errors: validateFields(fields, data) | ||
}, | ||
function(){ | ||
var has_error = _.filter(_.values(self.state.errors)).length > 0; | ||
if(has_error){ | ||
if(_.isFunction(self.props.onSubmitFail)){ | ||
self.props.onSubmitFail(self.state.errors); | ||
} | ||
}else{ | ||
if(_.isFunction(self.props.onSubmit)){ | ||
self.props.onSubmit(self.state.data); | ||
} | ||
} | ||
submit_attempts: this.state.submit_attempts + 1, | ||
errors: this.Form_validate() | ||
}, function(){ | ||
if(is.empty(self.state.errors)){ | ||
self.____onSubmit(self.state.data); | ||
}else{ | ||
self.____onSubmitFail(self.state.errors); | ||
} | ||
); | ||
return false; | ||
}); | ||
}, | ||
Form_onChange: function(field_path, new_value){ | ||
var self = this; | ||
var fields = this.buildFields(); | ||
var fields = this.____buildSchema(); | ||
var should_validate = this.state.submit_attempts > 0; | ||
@@ -73,5 +61,3 @@ var data = this.state.data; | ||
}, function(){ | ||
if(_.isFunction(self.onFormChanged)){ | ||
self.onFormChanged(field_path, new_value); | ||
} | ||
self.____onFormChanged(field_path, new_value); | ||
}); | ||
@@ -92,22 +78,53 @@ }, | ||
Form_areChangesMade: function(props){ | ||
return !_.isEqual(this.state.data, this.____getInitialValues(props || this.props)); | ||
return !is.equal(this.state.data, this.____getInitialValues(props || this.props)); | ||
}, | ||
//Below are functions that the child can implement | ||
____buildSchema: function(){ | ||
if(is.fn(this.buildSchema)){ | ||
return this.buildSchema(); | ||
}else{ | ||
console.warn("react-loose-forms FormMixin expects there to be a funciton on the compoent called buildSchema that returns the schema for the form."); | ||
return {}; | ||
} | ||
}, | ||
____getInitialValuesSourceVersion: function(props){ | ||
if(is.fn(this.getInitialValuesSourceVersion)){ | ||
return this.getInitialValuesSourceVersion(props); | ||
} | ||
return null; | ||
}, | ||
____getInitialValues: function(props){ | ||
if(_.isFunction(this.getInitialValues)){ | ||
return _.cloneDeep(this.getInitialValues(props)) || {}; | ||
if(is.fn(this.getInitialValues)){ | ||
return clone(this.getInitialValues(props) || {}); | ||
} | ||
return {}; | ||
}, | ||
____onFormChanged: function(field_path, new_value){ | ||
if(is.fn(this.onFormChanged)){ | ||
this.onFormChanged(field_path, new_value); | ||
} | ||
}, | ||
____onSubmit: function(data){ | ||
if(is.fn(this.props.onSubmit)){ | ||
this.props.onSubmit(data); | ||
} | ||
}, | ||
____onSubmitFail: function(errors){ | ||
if(is.fn(this.props.onSubmitFail)){ | ||
this.props.onSubmitFail(errors); | ||
} | ||
} | ||
}; | ||
var default_validation_fn = function(value, field){ | ||
var defaultValidationFn = function(value, field){ | ||
var valid = false; | ||
if(_.isBoolean(value)){ | ||
if(is.boolean(value)){ | ||
valid = value === true; | ||
}else if(_.isNumber(value)){ | ||
}else if(is.number(value)){ | ||
valid = value !== 0; | ||
}else if(_.isDate(value)){ | ||
}else if(is.date(value)){ | ||
valid = true; | ||
}else{ | ||
valid = !_.isEmpty(value); | ||
valid = !is.empty(value); | ||
} | ||
@@ -118,9 +135,11 @@ return valid || field.label + ' is required'; | ||
var validateFields = function(fields, data){ | ||
return _.mapValues(fields, function(field, field_path){ | ||
var validation_fn = default_validation_fn; | ||
if(_.isFunction(field.validate)){ | ||
var errors = {}; | ||
Object.keys(fields).forEach(function(field_path){ | ||
var field = fields[field_path]; | ||
var validation_fn = defaultValidationFn; | ||
if(is.fn(field.validate)){ | ||
validation_fn = field.validate; | ||
}else{ | ||
var input = InputTypes.getInputByType(field.type); | ||
if(_.isFunction(input.validate)){ | ||
if(is.fn(input && input.validate)){ | ||
validation_fn = input.validate; | ||
@@ -130,9 +149,9 @@ } | ||
var resp = validation_fn(data[field_path], field); | ||
if(resp === true){ | ||
return false; | ||
if(resp !== true){ | ||
errors[field_path] = is.string(resp) ? resp : 'Please check your input.'; | ||
} | ||
return _.isString(resp) ? resp : 'Please check your input.'; | ||
}); | ||
return errors; | ||
}; | ||
module.exports = FormMixin; |
@@ -1,7 +0,5 @@ | ||
var _ = require('lodash'); | ||
var input_types = {}; | ||
var getInputByType = function(type){ | ||
if(!_.has(input_types, type)){ | ||
if(!input_types.hasOwnProperty(type)){ | ||
type = 'text'; | ||
@@ -8,0 +6,0 @@ } |
{ | ||
"name": "react-loose-forms", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "form library for React that is very flexible yet powerful", | ||
@@ -24,4 +24,5 @@ "main": "index.js", | ||
"dependencies": { | ||
"lodash": "~2.4.1" | ||
"clone": "^1.0.2", | ||
"is": "^3.0.1" | ||
} | ||
} |
7643
172
2
9
+ Addedclone@^1.0.2
+ Addedis@^3.0.1
+ Addedclone@1.0.4(transitive)
+ Addedis@3.3.0(transitive)
- Removedlodash@~2.4.1
- Removedlodash@2.4.2(transitive)