Skip to content

Commit 0b7c4bd

Browse files
committed
Prepublish to release from github
1 parent 466b8bd commit 0b7c4bd

File tree

7 files changed

+850
-1
lines changed

7 files changed

+850
-1
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
node_modules
2-
lib

lib/Decorator.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
4+
5+
var React = global.React || require('react');
6+
var createReactClass = require('create-react-class');
7+
var Mixin = require('./Mixin.js');
8+
module.exports = function () {
9+
return function (Component) {
10+
return createReactClass({
11+
mixins: [Mixin],
12+
render: function render() {
13+
return React.createElement(Component, _extends({
14+
setValidations: this.setValidations,
15+
setValue: this.setValue,
16+
resetValue: this.resetValue,
17+
getValue: this.getValue,
18+
hasValue: this.hasValue,
19+
getErrorMessage: this.getErrorMessage,
20+
getErrorMessages: this.getErrorMessages,
21+
isFormDisabled: this.isFormDisabled,
22+
isValid: this.isValid,
23+
isPristine: this.isPristine,
24+
isFormSubmitted: this.isFormSubmitted,
25+
isRequired: this.isRequired,
26+
showRequired: this.showRequired,
27+
showError: this.showError,
28+
isValidValue: this.isValidValue
29+
}, this.props));
30+
}
31+
});
32+
};
33+
};

lib/HOC.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
4+
5+
var React = global.React || require('react');
6+
var createReactClass = require('create-react-class');
7+
var Mixin = require('./Mixin.js');
8+
module.exports = function (Component) {
9+
return createReactClass({
10+
displayName: 'Formsy(' + getDisplayName(Component) + ')',
11+
mixins: [Mixin],
12+
13+
render: function render() {
14+
var innerRef = this.props.innerRef;
15+
16+
var propsForElement = _extends({
17+
setValidations: this.setValidations,
18+
setValue: this.setValue,
19+
resetValue: this.resetValue,
20+
getValue: this.getValue,
21+
hasValue: this.hasValue,
22+
getErrorMessage: this.getErrorMessage,
23+
getErrorMessages: this.getErrorMessages,
24+
isFormDisabled: this.isFormDisabled,
25+
isValid: this.isValid,
26+
isPristine: this.isPristine,
27+
isFormSubmitted: this.isFormSubmitted,
28+
isRequired: this.isRequired,
29+
showRequired: this.showRequired,
30+
showError: this.showError,
31+
isValidValue: this.isValidValue
32+
}, this.props);
33+
34+
if (innerRef) {
35+
propsForElement.ref = innerRef;
36+
}
37+
return React.createElement(Component, propsForElement);
38+
}
39+
});
40+
};
41+
42+
function getDisplayName(Component) {
43+
return Component.displayName || Component.name || (typeof Component === 'string' ? Component : 'Component');
44+
}

lib/Mixin.js

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
'use strict';
2+
3+
var PropTypes = require('prop-types');
4+
var utils = require('./utils.js');
5+
var React = global.React || require('react');
6+
7+
var convertValidationsToObject = function convertValidationsToObject(validations) {
8+
9+
if (typeof validations === 'string') {
10+
11+
return validations.split(/\,(?![^{\[]*[}\]])/g).reduce(function (validations, validation) {
12+
var args = validation.split(':');
13+
var validateMethod = args.shift();
14+
15+
args = args.map(function (arg) {
16+
try {
17+
return JSON.parse(arg);
18+
} catch (e) {
19+
return arg; // It is a string if it can not parse it
20+
}
21+
});
22+
23+
if (args.length > 1) {
24+
throw new Error('Formsy does not support multiple args on string validations. Use object format of validations instead.');
25+
}
26+
27+
validations[validateMethod] = args.length ? args[0] : true;
28+
return validations;
29+
}, {});
30+
}
31+
32+
return validations || {};
33+
};
34+
35+
module.exports = {
36+
getInitialState: function getInitialState() {
37+
return {
38+
_value: this.props.value,
39+
_isRequired: false,
40+
_isValid: true,
41+
_isPristine: true,
42+
_pristineValue: this.props.value,
43+
_validationError: [],
44+
_externalError: null,
45+
_formSubmitted: false
46+
};
47+
},
48+
contextTypes: {
49+
formsy: PropTypes.object // What about required?
50+
},
51+
getDefaultProps: function getDefaultProps() {
52+
return {
53+
validationError: '',
54+
validationErrors: {}
55+
};
56+
},
57+
58+
componentWillMount: function componentWillMount() {
59+
var configure = function () {
60+
this.setValidations(this.props.validations, this.props.required);
61+
62+
// Pass a function instead?
63+
this.context.formsy.attachToForm(this);
64+
//this.props._attachToForm(this);
65+
}.bind(this);
66+
67+
if (!this.props.name) {
68+
throw new Error('Form Input requires a name property when used');
69+
}
70+
71+
/*
72+
if (!this.props._attachToForm) {
73+
return setTimeout(function () {
74+
if (!this.isMounted()) return;
75+
if (!this.props._attachToForm) {
76+
throw new Error('Form Mixin requires component to be nested in a Form');
77+
}
78+
configure();
79+
}.bind(this), 0);
80+
}
81+
*/
82+
configure();
83+
},
84+
85+
// We have to make the validate method is kept when new props are added
86+
componentWillReceiveProps: function componentWillReceiveProps(nextProps) {
87+
this.setValidations(nextProps.validations, nextProps.required);
88+
},
89+
90+
componentDidUpdate: function componentDidUpdate(prevProps) {
91+
92+
// If the value passed has changed, set it. If value is not passed it will
93+
// internally update, and this will never run
94+
if (!utils.isSame(this.props.value, prevProps.value)) {
95+
this.setValue(this.props.value);
96+
}
97+
98+
// If validations or required is changed, run a new validation
99+
if (!utils.isSame(this.props.validations, prevProps.validations) || !utils.isSame(this.props.required, prevProps.required)) {
100+
this.context.formsy.validate(this);
101+
}
102+
},
103+
104+
// Detach it when component unmounts
105+
componentWillUnmount: function componentWillUnmount() {
106+
this.context.formsy.detachFromForm(this);
107+
//this.props._detachFromForm(this);
108+
},
109+
110+
setValidations: function setValidations(validations, required) {
111+
112+
// Add validations to the store itself as the props object can not be modified
113+
this._validations = convertValidationsToObject(validations) || {};
114+
this._requiredValidations = required === true ? { isDefaultRequiredValue: true } : convertValidationsToObject(required);
115+
},
116+
117+
// We validate after the value has been set
118+
setValue: function setValue(value) {
119+
this.setState({
120+
_value: value,
121+
_isPristine: false
122+
}, function () {
123+
this.context.formsy.validate(this);
124+
//this.props._validate(this);
125+
}.bind(this));
126+
},
127+
resetValue: function resetValue() {
128+
this.setState({
129+
_value: this.state._pristineValue,
130+
_isPristine: true
131+
}, function () {
132+
this.context.formsy.validate(this);
133+
//this.props._validate(this);
134+
});
135+
},
136+
getValue: function getValue() {
137+
return this.state._value;
138+
},
139+
hasValue: function hasValue() {
140+
return this.state._value !== '';
141+
},
142+
getErrorMessage: function getErrorMessage() {
143+
var messages = this.getErrorMessages();
144+
return messages.length ? messages[0] : null;
145+
},
146+
getErrorMessages: function getErrorMessages() {
147+
return !this.isValid() || this.showRequired() ? this.state._externalError || this.state._validationError || [] : [];
148+
},
149+
isFormDisabled: function isFormDisabled() {
150+
return this.context.formsy.isFormDisabled();
151+
//return this.props._isFormDisabled();
152+
},
153+
isValid: function isValid() {
154+
return this.state._isValid;
155+
},
156+
isPristine: function isPristine() {
157+
return this.state._isPristine;
158+
},
159+
isFormSubmitted: function isFormSubmitted() {
160+
return this.state._formSubmitted;
161+
},
162+
isRequired: function isRequired() {
163+
return !!this.props.required;
164+
},
165+
showRequired: function showRequired() {
166+
return this.state._isRequired;
167+
},
168+
showError: function showError() {
169+
return !this.showRequired() && !this.isValid();
170+
},
171+
isValidValue: function isValidValue(value) {
172+
return this.context.formsy.isValidValue.call(null, this, value);
173+
//return this.props._isValidValue.call(null, this, value);
174+
}
175+
};

0 commit comments

Comments
 (0)