added HOC and decorators

This commit is contained in:
christianalfoni 2015-07-26 15:06:46 +02:00
parent 818b1defbf
commit 82edabde15
4 changed files with 63 additions and 5 deletions

View File

@ -3,21 +3,21 @@ var ReactDOM = require('react-dom');
var Formsy = require('./../src/main.js');
var Input = React.createClass({
mixins: [Formsy.Mixin],
onChange: function (event) {
this.setValue(event.currentTarget.value);
this.props.setValue(event.currentTarget.value);
},
render: function () {
return (
<div>
{this.showRequired() ? 'required' : ''}
<input disabled={this.isFormDisabled()} value={this.getValue()} onChange={this.onChange}/>
{this.props.showRequired() ? 'required' : ''}
<input disabled={this.props.isFormDisabled()} value={this.props.getValue()} onChange={this.onChange}/>
</div>
);
}
});
Input = Formsy.HOC(Input);
var SomeComp = React.createClass({
getInitialState: function () {
return {

28
src/Decorator.js Normal file
View File

@ -0,0 +1,28 @@
var React = global.React || require('react');
var Mixin = require('./Mixin.js');
module.exports = function () {
return function (Component) {
return React.createClass({
mixins: [Mixin],
render: function () {
return React.createElement(Component, {
setValidations: this.setValidations,
setValue: this.setValue,
resetValue: this.resetValue,
getValue: this.getValue,
hasValue: this.hasValue,
getErrorMessage: this.getErrorMessage,
getErrorMessages: this.getErrorMessages,
isFormDisabled: this.isFormDisabled,
isValid: this.isValid,
isPristine: this.isPristine,
isFormSubmitted: this.isFormSubmitted,
isRequired: this.isRequired,
showRequired: this.showRequired,
showError: this.showError,
isValidValue: this.isValidValue
});
}
});
};
};

26
src/HOC.js Normal file
View File

@ -0,0 +1,26 @@
var React = global.React || require('react');
var Mixin = require('./Mixin.js');
module.exports = function (Component) {
return React.createClass({
mixins: [Mixin],
render: function () {
return React.createElement(Component, {
setValidations: this.setValidations,
setValue: this.setValue,
resetValue: this.resetValue,
getValue: this.getValue,
hasValue: this.hasValue,
getErrorMessage: this.getErrorMessage,
getErrorMessages: this.getErrorMessages,
isFormDisabled: this.isFormDisabled,
isValid: this.isValid,
isPristine: this.isPristine,
isFormSubmitted: this.isFormSubmitted,
isRequired: this.isRequired,
showRequired: this.showRequired,
showError: this.showError,
isValidValue: this.isValidValue
});
}
});
};

View File

@ -4,9 +4,13 @@ var validationRules = require('./validationRules.js');
var formDataToObject = require('form-data-to-object');
var utils = require('./utils.js');
var Mixin = require('./Mixin.js');
var HOC = require('./HOC.js');
var Decorator = require('./Decorator.js');
var options = {};
Formsy.Mixin = Mixin;
Formsy.HOC = HOC;
Formsy.Decorator = Decorator;
Formsy.defaults = function (passedOptions) {
options = passedOptions;