a-plus-forms
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -54,3 +54,3 @@ 'use strict'; | ||
value: function errorsFor(data) { | ||
return this.schema(data); | ||
return this.schema && this.schema(data); | ||
} | ||
@@ -57,0 +57,0 @@ }]); |
@@ -7,2 +7,4 @@ 'use strict'; | ||
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; }; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
@@ -32,2 +34,4 @@ | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -65,2 +69,3 @@ | ||
APFProps: this.props, | ||
APFDirty: this.props.dirty, | ||
APFState: options.nested && this.stateManager, | ||
@@ -78,5 +83,5 @@ APFError: options.nested && _typeof(this.props.error) === 'object' && this.props.error || undefined | ||
if ('value' in this.props) { | ||
this.value = this.props.value; | ||
this.stateManager.value = this.props.value; | ||
} else if ('defaultValue' in this.props) { | ||
this.value = this.props.defaultValue; | ||
this.stateManager.value = this.props.defaultValue; | ||
} | ||
@@ -95,16 +100,42 @@ } | ||
if ('value' in props) { | ||
this.value = this.props.value; | ||
this.value = props.value; | ||
} | ||
} | ||
}, { | ||
key: 'propagateChange', | ||
value: function propagateChange(value) { | ||
var APFState = this.context.APFState; | ||
var _props = this.props, | ||
name = _props.name, | ||
onChange = _props.onChange; | ||
onChange(value); | ||
if (APFState && name) { | ||
var parentField = APFState.strategy.component; | ||
var parentValue = parentField.value; | ||
parentField.propagateChange(_extends({}, parentValue, _defineProperty({}, name, value))); | ||
} | ||
} | ||
}, { | ||
key: 'render', | ||
value: function render() { | ||
var _props = this.props, | ||
defaultValue = _props.defaultValue, | ||
error = _props.error, | ||
props = _objectWithoutProperties(_props, ['defaultValue', 'error']); // eslint-disable-line | ||
var _props2 = this.props, | ||
defaultValue = _props2.defaultValue, | ||
error = _props2.error, | ||
dirty = _props2.dirty, | ||
props = _objectWithoutProperties(_props2, ['defaultValue', 'error', 'dirty']); // eslint-disable-line | ||
Object.assign(props, { value: this.value, onChange: this.onChange }); | ||
return _react2.default.createElement(_layout2.default, { input: Input, props: props, error: this.error, layout: options.layout }); | ||
return _react2.default.createElement(_layout2.default, { | ||
input: Input, | ||
props: props, | ||
error: this.error, | ||
dirty: this.dirty, | ||
layout: options.layout | ||
}); | ||
} | ||
@@ -124,3 +155,3 @@ }, { | ||
this.stateManager.value = value; | ||
this.props.onChange(value); | ||
this.propagateChange(value); | ||
} | ||
@@ -133,5 +164,5 @@ } | ||
APFError = _context$APFError === undefined ? {} : _context$APFError; | ||
var _props2 = this.props, | ||
propsError = _props2.error, | ||
name = _props2.name; | ||
var _props3 = this.props, | ||
propsError = _props3.error, | ||
name = _props3.name; | ||
@@ -146,2 +177,9 @@ var error = propsError || APFError[name]; | ||
} | ||
}, { | ||
key: 'dirty', | ||
get: function get() { | ||
var dirty = this.props.dirty; | ||
return dirty === undefined ? this.context.APFDirty : dirty; | ||
} | ||
}]); | ||
@@ -154,9 +192,11 @@ | ||
APFState: _propTypes2.default.object, | ||
APFError: _propTypes2.default.object | ||
APFError: _propTypes2.default.object, | ||
APFDirty: _propTypes2.default.bool | ||
}, _class.childContextTypes = { | ||
APFState: _propTypes2.default.object, // nested field anchor | ||
APFProps: _propTypes2.default.object, // original field props, | ||
APFError: _propTypes2.default.object // nested field errors | ||
APFError: _propTypes2.default.object, // nested field errors | ||
APFDirty: _propTypes2.default.bool | ||
}, _temp; | ||
}; | ||
}; |
@@ -63,6 +63,14 @@ 'use strict'; | ||
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = Form.__proto__ || Object.getPrototypeOf(Form)).call.apply(_ref2, [this].concat(args))), _this), _this.state = { errors: null }, _this.onSubmit = function (event) { | ||
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref2 = Form.__proto__ || Object.getPrototypeOf(Form)).call.apply(_ref2, [this].concat(args))), _this), _this.state = { errors: null, dirty: false }, _this.onChange = function (value) { | ||
_this.props.onChange(value); | ||
if (_this.state.dirty) { | ||
_this.validate(value); | ||
} | ||
}, _this.onSubmit = function (event) { | ||
event.preventDefault(); | ||
_this.validate().then(function (errors) { | ||
_this.setState({ dirty: true }); | ||
if (!errors) { | ||
@@ -115,5 +123,5 @@ var result = _this.props.onSubmit(_this.value); | ||
value: function validate() { | ||
var _this2 = this; | ||
var value = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.value; | ||
var errors = this.validator.errorsFor(this.value); | ||
var errors = this.validator.errorsFor(value); | ||
@@ -124,4 +132,6 @@ if (isPromisish(errors)) { | ||
this.handleErrors(errors); | ||
return { then: function then(cb) { | ||
return cb(_this2.handleErrors(errors)); | ||
return cb(errors); | ||
} }; | ||
@@ -134,5 +144,6 @@ } | ||
children = _props.children, | ||
defaultValue = _props.defaultValue, | ||
onChange = _props.onChange; | ||
var errors = this.state.errors; | ||
defaultValue = _props.defaultValue; | ||
var _state = this.state, | ||
errors = _state.errors, | ||
dirty = _state.dirty; | ||
@@ -143,5 +154,6 @@ | ||
{ | ||
dirty: dirty, | ||
error: errors, | ||
defaultValue: defaultValue, | ||
onChange: onChange, | ||
onChange: this.onChange, | ||
ref: this.setStateRef | ||
@@ -173,5 +185,5 @@ }, | ||
onError: function onError() {}, | ||
schema: function schema() {}, | ||
schema: undefined, | ||
defaultValue: {} | ||
}, _temp2); | ||
exports.default = Form; |
@@ -80,6 +80,7 @@ 'use strict'; | ||
rest = _objectWithoutProperties(_props$props2, ['id', 'className']), | ||
error = _props.error; // eslint-disable-line | ||
error = _props.error, | ||
dirty = _props.dirty; // eslint-disable-line | ||
return _extends({}, rest, { error: error }); | ||
return _extends({}, rest, { error: dirty === false ? null : error }); | ||
} | ||
@@ -86,0 +87,0 @@ |
{ | ||
"name": "a-plus-forms", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "A+ forms. Would use again", | ||
@@ -5,0 +5,0 @@ "files": [ |
92895
1570