Skip to content

Commit 782b4e8

Browse files
authored
Merge pull request #16 from formsy/v1.0.1release
Bump version number to v1.0.1
2 parents adc6fff + a18c220 commit 782b4e8

File tree

8 files changed

+1066
-4
lines changed

8 files changed

+1066
-4
lines changed

.gitignore

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

lib/Wrapper.js

Lines changed: 311 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.propTypes = undefined;
7+
8+
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; };
9+
10+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
11+
12+
var _propTypes = require('prop-types');
13+
14+
var _propTypes2 = _interopRequireDefault(_propTypes);
15+
16+
var _react = require('react');
17+
18+
var _react2 = _interopRequireDefault(_react);
19+
20+
var _utils = require('./utils');
21+
22+
var _utils2 = _interopRequireDefault(_utils);
23+
24+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25+
26+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
27+
28+
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
29+
30+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
31+
32+
var convertValidationsToObject = function convertValidationsToObject(validations) {
33+
if (typeof validations === 'string') {
34+
return validations.split(/,(?![^{[]*[}\]])/g).reduce(function (validationsAccumulator, validation) {
35+
var args = validation.split(':');
36+
var validateMethod = args.shift();
37+
38+
args = args.map(function (arg) {
39+
try {
40+
return JSON.parse(arg);
41+
} catch (e) {
42+
return arg; // It is a string if it can not parse it
43+
}
44+
});
45+
46+
if (args.length > 1) {
47+
throw new Error('Formsy does not support multiple args on string validations. Use object format of validations instead.');
48+
}
49+
50+
// Avoid parameter reassignment
51+
var validationsAccumulatorCopy = Object.assign({}, validationsAccumulator);
52+
validationsAccumulatorCopy[validateMethod] = args.length ? args[0] : true;
53+
return validationsAccumulatorCopy;
54+
}, {});
55+
}
56+
57+
return validations || {};
58+
};
59+
60+
var propTypes = {
61+
innerRef: _propTypes2.default.func,
62+
name: _propTypes2.default.string.isRequired,
63+
required: _propTypes2.default.bool,
64+
validations: _propTypes2.default.oneOfType([_propTypes2.default.object, _propTypes2.default.string]),
65+
value: _propTypes2.default.any
66+
};
67+
68+
exports.propTypes = propTypes;
69+
70+
exports.default = function (Component) {
71+
var WrappedComponent = function (_React$Component) {
72+
_inherits(WrappedComponent, _React$Component);
73+
74+
function WrappedComponent(props) {
75+
_classCallCheck(this, WrappedComponent);
76+
77+
var _this = _possibleConstructorReturn(this, (WrappedComponent.__proto__ || Object.getPrototypeOf(WrappedComponent)).call(this, props));
78+
79+
_this.state = {
80+
value: props.value,
81+
isRequired: false,
82+
isValid: true,
83+
isPristine: true,
84+
pristineValue: props.value,
85+
validationError: [],
86+
externalError: null,
87+
formSubmitted: false
88+
};
89+
_this.getErrorMessage = _this.getErrorMessage.bind(_this);
90+
_this.getErrorMessages = _this.getErrorMessages.bind(_this);
91+
_this.getValue = _this.getValue.bind(_this);
92+
_this.isFormDisabled = _this.isFormDisabled.bind(_this);
93+
_this.isPristine = _this.isPristine.bind(_this);
94+
_this.isRequired = _this.isRequired.bind(_this);
95+
_this.isValid = _this.isValid.bind(_this);
96+
_this.resetValue = _this.resetValue.bind(_this);
97+
_this.setValue = _this.setValue.bind(_this);
98+
_this.showRequired = _this.showRequired.bind(_this);
99+
return _this;
100+
}
101+
102+
_createClass(WrappedComponent, [{
103+
key: 'componentWillMount',
104+
value: function componentWillMount() {
105+
var _this2 = this;
106+
107+
var configure = function configure() {
108+
_this2.setValidations(_this2.props.validations, _this2.props.required);
109+
110+
// Pass a function instead?
111+
_this2.context.formsy.attachToForm(_this2);
112+
};
113+
114+
if (!this.props.name) {
115+
throw new Error('Form Input requires a name property when used');
116+
}
117+
118+
configure();
119+
}
120+
121+
// We have to make sure the validate method is kept when new props are added
122+
123+
}, {
124+
key: 'componentWillReceiveProps',
125+
value: function componentWillReceiveProps(nextProps) {
126+
this.setValidations(nextProps.validations, nextProps.required);
127+
}
128+
}, {
129+
key: 'componentDidUpdate',
130+
value: function componentDidUpdate(prevProps) {
131+
// If the value passed has changed, set it. If value is not passed it will
132+
// internally update, and this will never run
133+
if (!_utils2.default.isSame(this.props.value, prevProps.value)) {
134+
this.setValue(this.props.value);
135+
}
136+
137+
// If validations or required is changed, run a new validation
138+
if (!_utils2.default.isSame(this.props.validations, prevProps.validations) || !_utils2.default.isSame(this.props.required, prevProps.required)) {
139+
this.context.formsy.validate(this);
140+
}
141+
}
142+
143+
// Detach it when component unmounts
144+
145+
}, {
146+
key: 'componentWillUnmount',
147+
value: function componentWillUnmount() {
148+
this.context.formsy.detachFromForm(this);
149+
}
150+
}, {
151+
key: 'getErrorMessage',
152+
value: function getErrorMessage() {
153+
var messages = this.getErrorMessages();
154+
return messages.length ? messages[0] : null;
155+
}
156+
}, {
157+
key: 'getErrorMessages',
158+
value: function getErrorMessages() {
159+
return !this.isValid() || this.showRequired() ? this.state.externalError || this.state.validationError || [] : [];
160+
}
161+
}, {
162+
key: 'getValue',
163+
value: function getValue() {
164+
return this.state.value;
165+
}
166+
}, {
167+
key: 'setValidations',
168+
value: function setValidations(validations, required) {
169+
// Add validations to the store itself as the props object can not be modified
170+
this.validations = convertValidationsToObject(validations) || {};
171+
this.requiredValidations = required === true ? { isDefaultRequiredValue: true } : convertValidationsToObject(required);
172+
}
173+
174+
// By default, we validate after the value has been set.
175+
// A user can override this and pass a second parameter of `false` to skip validation.
176+
177+
}, {
178+
key: 'setValue',
179+
value: function setValue(value) {
180+
var _this3 = this;
181+
182+
var validate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
183+
184+
if (!validate) {
185+
this.setState({
186+
value: value
187+
});
188+
} else {
189+
this.setState({
190+
value: value,
191+
isPristine: false
192+
}, function () {
193+
_this3.context.formsy.validate(_this3);
194+
});
195+
}
196+
}
197+
}, {
198+
key: 'hasValue',
199+
value: function hasValue() {
200+
return this.state.value !== '';
201+
}
202+
}, {
203+
key: 'isFormDisabled',
204+
value: function isFormDisabled() {
205+
return this.context.formsy.isFormDisabled();
206+
}
207+
}, {
208+
key: 'isFormSubmitted',
209+
value: function isFormSubmitted() {
210+
return this.state.formSubmitted;
211+
}
212+
}, {
213+
key: 'isPristine',
214+
value: function isPristine() {
215+
return this.state.isPristine;
216+
}
217+
}, {
218+
key: 'isRequired',
219+
value: function isRequired() {
220+
return !!this.props.required;
221+
}
222+
}, {
223+
key: 'isValid',
224+
value: function isValid() {
225+
return this.state.isValid;
226+
}
227+
}, {
228+
key: 'isValidValue',
229+
value: function isValidValue(value) {
230+
return this.context.formsy.isValidValue.call(null, this, value);
231+
// return this.props.isValidValue.call(null, this, value);
232+
}
233+
}, {
234+
key: 'resetValue',
235+
value: function resetValue() {
236+
var _this4 = this;
237+
238+
this.setState({
239+
value: this.state.pristineValue,
240+
isPristine: true
241+
}, function () {
242+
_this4.context.formsy.validate(_this4);
243+
});
244+
}
245+
}, {
246+
key: 'showError',
247+
value: function showError() {
248+
return !this.showRequired() && !this.isValid();
249+
}
250+
}, {
251+
key: 'showRequired',
252+
value: function showRequired() {
253+
return this.state.isRequired;
254+
}
255+
}, {
256+
key: 'render',
257+
value: function render() {
258+
var innerRef = this.props.innerRef;
259+
260+
var propsForElement = _extends({
261+
getErrorMessage: this.getErrorMessage,
262+
getErrorMessages: this.getErrorMessages,
263+
getValue: this.getValue,
264+
hasValue: this.hasValue,
265+
isFormDisabled: this.isFormDisabled,
266+
isValid: this.isValid,
267+
isPristine: this.isPristine,
268+
isFormSubmitted: this.isFormSubmitted,
269+
isRequired: this.isRequired,
270+
isValidValue: this.isValidValue,
271+
resetValue: this.resetValue,
272+
setValidations: this.setValidations,
273+
setValue: this.setValue,
274+
showRequired: this.showRequired,
275+
showError: this.showError
276+
}, this.props);
277+
278+
if (innerRef) {
279+
propsForElement.ref = innerRef;
280+
}
281+
282+
return _react2.default.createElement(Component, propsForElement);
283+
}
284+
}]);
285+
286+
return WrappedComponent;
287+
}(_react2.default.Component);
288+
289+
function getDisplayName(component) {
290+
return component.displayName || component.name || (typeof component === 'string' ? component : 'Component');
291+
}
292+
293+
WrappedComponent.displayName = 'Formsy(' + getDisplayName(Component) + ')';
294+
295+
WrappedComponent.contextTypes = {
296+
formsy: _propTypes2.default.object // What about required?
297+
};
298+
299+
WrappedComponent.defaultProps = {
300+
innerRef: function innerRef() {},
301+
required: false,
302+
validationError: '',
303+
validationErrors: {},
304+
validations: null,
305+
value: Component.defaultValue
306+
};
307+
308+
WrappedComponent.propTypes = propTypes;
309+
310+
return WrappedComponent;
311+
};

0 commit comments

Comments
 (0)