Comparing version 0.16.0 to 0.17.0
# History | ||
---- | ||
## 0.17.0 / 2016-06-12 | ||
- support checkbox radio https://github.com/react-component/form/pull/21 | ||
- add exclusive config | ||
## 0.16.0 / 2016-05-19 | ||
@@ -5,0 +10,0 @@ |
@@ -7,2 +7,4 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
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; }; | ||
@@ -63,3 +65,3 @@ | ||
if (mapPropsToFields) { | ||
this.fields = mapPropsToFields(nextProps); | ||
this.fields = (0, _utils.flatFields)(mapPropsToFields(nextProps)); | ||
} | ||
@@ -79,7 +81,13 @@ }, | ||
var value = fieldMeta.getValueFromEvent ? fieldMeta.getValueFromEvent.apply(fieldMeta, args) : _utils.getValueFromEvent.apply(undefined, args); | ||
var field = this.getField(name, true); | ||
this.setFields(_defineProperty({}, name, _extends({}, field, { | ||
var field = this.getField(name); | ||
var fieldContent = _extends({}, field, { | ||
value: value, | ||
dirty: (0, _utils.hasRules)(validate) | ||
}))); | ||
}); | ||
if (fieldMeta.exclusive) { | ||
var nameKeyObj = (0, _utils.getNameKeyObj)(name); | ||
name = nameKeyObj.name; | ||
fieldContent = _defineProperty({}, nameKeyObj.key, fieldContent); | ||
} | ||
this.setFields(_defineProperty({}, name, fieldContent)); | ||
}, | ||
@@ -97,3 +105,3 @@ onChangeValidate: function onChangeValidate(name, action) { | ||
var value = fieldMeta.getValueFromEvent ? fieldMeta.getValueFromEvent.apply(fieldMeta, args) : _utils.getValueFromEvent.apply(undefined, args); | ||
var field = this.getField(name, true); | ||
var field = this.getField(name); | ||
field.value = value; | ||
@@ -104,2 +112,3 @@ field.dirty = true; | ||
options: { | ||
exclusive: fieldMeta.exclusive, | ||
firstFields: !!fieldMeta.validateFirst | ||
@@ -119,18 +128,26 @@ } | ||
}, | ||
getField: function getField(name, copy) { | ||
var ret = this.fields[name]; | ||
if (ret) { | ||
ret.name = name; | ||
getField: function getField(name) { | ||
var fields = this.fields; | ||
if (this.fieldsMeta[name] && this.fieldsMeta[name].hasKey) { | ||
var _ret = function () { | ||
var ret2 = _defineProperty({}, name, {}); | ||
Object.keys(fields).forEach(function (k) { | ||
var nameKeyObj = (0, _utils.getNameKeyObj)(k); | ||
if (nameKeyObj.name === name) { | ||
ret2[name][nameKeyObj.key] = fields[k]; | ||
} | ||
}); | ||
return { | ||
v: ret2 | ||
}; | ||
}(); | ||
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v; | ||
} | ||
if (copy) { | ||
if (ret) { | ||
return _extends({}, ret); | ||
} | ||
return { | ||
name: name | ||
}; | ||
} | ||
return ret; | ||
return _extends({}, fields[name], { | ||
name: name | ||
}); | ||
}, | ||
getFieldProps: function getFieldProps(name) { | ||
getFieldProps: function getFieldProps(name_) { | ||
var _this = this; | ||
@@ -140,2 +157,4 @@ | ||
var name = name_; | ||
if (!name) { | ||
@@ -151,2 +170,3 @@ throw new Error('must call getFieldProps with valid name string!'); | ||
var getValueProps = fieldOption.getValueProps; | ||
var exclusive = fieldOption.exclusive; | ||
var _fieldOption$validate = fieldOption.validateTrigger; | ||
@@ -157,4 +177,14 @@ var validateTrigger = _fieldOption$validate === undefined ? defaultValidateTrigger : _fieldOption$validate; | ||
var nameKeyObj = (0, _utils.getNameKeyObj)(name); | ||
name = nameKeyObj.name; | ||
var key = nameKeyObj.key; | ||
var fieldMeta = this.fieldsMeta[name] = this.fieldsMeta[name] || {}; | ||
var fieldMeta = this.fieldsMeta[name] || {}; | ||
if (key) { | ||
fieldMeta.hasKey = 1; | ||
fieldMeta.exclusive = exclusive; | ||
fieldMeta.hidden = 1; | ||
name = (0, _utils.getNameKeyStr)(name, key); | ||
fieldMeta = this.fieldsMeta[name] = this.fieldsMeta[name] || {}; | ||
} | ||
@@ -167,2 +197,6 @@ if ('initialValue' in fieldOption) { | ||
if (key) { | ||
inputProps.key = key; | ||
} | ||
if (fieldNameProp) { | ||
@@ -247,3 +281,3 @@ inputProps[fieldNameProp] = name; | ||
var fields = names || this.getValidFieldsName(); | ||
var fields = names || (0, _utils.flatFieldNames)(this.getValidFieldsName()); | ||
var allValues = {}; | ||
@@ -263,5 +297,3 @@ fields.forEach(function (f) { | ||
}, | ||
getValueFromFields: function getValueFromFields(name, fields) { | ||
var fieldsMeta = this.fieldsMeta; | ||
getValueFromFieldsInternal: function getValueFromFieldsInternal(name, fields) { | ||
var field = fields[name]; | ||
@@ -271,5 +303,26 @@ if (field && 'value' in field) { | ||
} | ||
var fieldMeta = fieldsMeta[name]; | ||
var fieldMeta = this.fieldsMeta[name]; | ||
return fieldMeta && fieldMeta.initialValue; | ||
}, | ||
getValueFromFields: function getValueFromFields(name, fields) { | ||
var derefKey = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2]; | ||
var fieldsMeta = this.fieldsMeta; | ||
if (fieldsMeta[name] && fieldsMeta[name].hasKey) { | ||
var ret = fieldsMeta[name].exclusive ? undefined : {}; | ||
for (var fieldKey in fields) { | ||
if (fields.hasOwnProperty(fieldKey)) { | ||
var nameKeyObj = derefKey && (0, _utils.getNameKeyObj)(fieldKey); | ||
if (nameKeyObj.name === name && nameKeyObj.key) { | ||
if (fieldsMeta[name].exclusive) { | ||
return this.getValueFromFieldsInternal(fieldKey, fields); | ||
} | ||
ret[nameKeyObj.key] = this.getValueFromFieldsInternal(fieldKey, fields); | ||
} | ||
} | ||
} | ||
return ret; | ||
} | ||
return this.getValueFromFieldsInternal(name, fields); | ||
}, | ||
getRules: function getRules(fieldMeta, action) { | ||
@@ -283,11 +336,27 @@ var actionRules = fieldMeta.validate.filter(function (item) { | ||
}, | ||
setFields: function setFields(fields) { | ||
setFields: function setFields(fields_) { | ||
var _this3 = this; | ||
var originalFields = this.fields; | ||
var fieldsMeta = this.fieldsMeta; | ||
var fields = (0, _utils.flatFields)(fields_, fieldsMeta); | ||
var originalFields = _extends({}, this.fields); | ||
var _loop = function _loop(k) { | ||
if (fieldsMeta[k] && fieldsMeta[k].hasKey) { | ||
/* eslint no-loop-func:0 */ | ||
Object.keys(originalFields).forEach(function (ok) { | ||
if ((0, _utils.getNameKeyObj)(ok).name === k) { | ||
delete originalFields[ok]; | ||
} | ||
}); | ||
} | ||
}; | ||
for (var k in fields_) { | ||
_loop(k); | ||
} | ||
var nowFields = _extends({}, originalFields, fields); | ||
var fieldsMeta = this.fieldsMeta; | ||
var nowValues = {}; | ||
Object.keys(fieldsMeta).forEach(function (f) { | ||
nowValues[f] = _this3.getValueFromFields(f, nowFields); | ||
nowValues[f] = _this3.getValueFromFields(f, nowFields, false); | ||
}); | ||
@@ -299,3 +368,3 @@ var changedFieldsName = Object.keys(fields); | ||
if (fieldMeta && fieldMeta.normalize) { | ||
var nowValue = fieldMeta.normalize(value, _this3.getValueFromFields(f, originalFields), nowValues); | ||
var nowValue = fieldMeta.normalize(value, _this3.getValueFromFields(f, originalFields), nowValues, false); | ||
if (nowValue !== value) { | ||
@@ -305,4 +374,5 @@ nowFields[f] = _extends({}, nowFields[f], { | ||
}); | ||
if (changedFieldsName.indexOf(f) === -1) { | ||
changedFieldsName.push(f); | ||
var fieldName = (0, _utils.getNameKeyObj)(f).name; | ||
if (changedFieldsName.indexOf(fieldName) === -1) { | ||
changedFieldsName.push(fieldName); | ||
} | ||
@@ -317,3 +387,3 @@ } | ||
changedFieldsName.forEach(function (f) { | ||
changedFields[f] = nowFields[f]; | ||
changedFields[f] = _this3.getField(f); | ||
}); | ||
@@ -354,2 +424,3 @@ onFieldsChange(_this3.props, changedFields); | ||
delete this.instances[name]; | ||
delete this.cachedBind[name]; | ||
return; | ||
@@ -395,3 +466,8 @@ } | ||
allValues[name] = newField.value; | ||
allFields[name] = newField; | ||
if (options.exclusive) { | ||
var nameKeyObj = (0, _utils.getNameKeyObj)(name); | ||
allFields[nameKeyObj.name] = _defineProperty({}, nameKeyObj.key, newField); | ||
} else { | ||
allFields[name] = newField; | ||
} | ||
}); | ||
@@ -405,3 +481,3 @@ this.setFields(allFields); | ||
if (callback && (0, _utils.isEmptyObject)(allFields)) { | ||
callback((0, _utils.isEmptyObject)(alreadyErrors) ? null : alreadyErrors, this.getFieldsValue(fieldNames)); | ||
callback((0, _utils.isEmptyObject)(alreadyErrors) ? null : alreadyErrors, this.getFieldsValue((0, _utils.flatFieldNames)(fieldNames))); | ||
return; | ||
@@ -431,3 +507,3 @@ } | ||
var fieldErrors = errorsGroup[name]; | ||
var nowField = _this4.getField(name, true); | ||
var nowField = _this4.getField(name); | ||
// avoid concurrency problems | ||
@@ -462,3 +538,3 @@ if (nowField.value !== allValues[name]) { | ||
} | ||
callback((0, _utils.isEmptyObject)(errorsGroup) ? null : errorsGroup, _this4.getFieldsValue(fieldNames)); | ||
callback((0, _utils.isEmptyObject)(errorsGroup) ? null : errorsGroup, _this4.getFieldsValue((0, _utils.flatFieldNames)(fieldNames))); | ||
} | ||
@@ -482,3 +558,3 @@ }); | ||
} | ||
var field = _this5.getField(name, true); | ||
var field = _this5.getField(name); | ||
field.value = _this5.getFieldValue(name); | ||
@@ -491,3 +567,3 @@ return field; | ||
if (callback) { | ||
callback(null, this.getFieldsValue(fieldNames)); | ||
callback(null, this.getFieldsValue((0, _utils.flatFieldNames)(fieldNames))); | ||
} | ||
@@ -511,4 +587,8 @@ return; | ||
isFieldsValidating: function isFieldsValidating(ns) { | ||
var _this6 = this; | ||
var names = ns || this.getValidFieldsName(); | ||
return names.some(this.isFieldValidating); | ||
return names.some(function (n) { | ||
return _this6.isFieldValidating(n); | ||
}); | ||
}, | ||
@@ -519,6 +599,6 @@ isSubmitting: function isSubmitting() { | ||
submit: function submit(callback) { | ||
var _this6 = this; | ||
var _this7 = this; | ||
var fn = function fn() { | ||
_this6.setState({ | ||
_this7.setState({ | ||
submitting: false | ||
@@ -525,0 +605,0 @@ }); |
@@ -6,2 +6,5 @@ '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; }; | ||
exports.argumentContainer = argumentContainer; | ||
@@ -14,3 +17,8 @@ exports.getValueFromEvent = getValueFromEvent; | ||
exports.hasRules = hasRules; | ||
exports.startsWith = startsWith; | ||
exports.getParams = getParams; | ||
exports.getNameKeyStr = getNameKeyStr; | ||
exports.getNameKeyObj = getNameKeyObj; | ||
exports.flatFields = flatFields; | ||
exports.flatFieldNames = flatFieldNames; | ||
@@ -77,2 +85,6 @@ var _hoistNonReactStatics = require('hoist-non-react-statics'); | ||
function startsWith(str, prefix) { | ||
return str.lastIndexOf(prefix, 0) === 0; | ||
} | ||
function getParams(ns, opt, cb) { | ||
@@ -105,2 +117,51 @@ var names = ns; | ||
}; | ||
} | ||
var NAME_KEY_SEP = '.'; | ||
function getNameKeyStr(name, key) { | ||
if (key) { | ||
return '' + name + NAME_KEY_SEP + key; | ||
} | ||
return name; | ||
} | ||
function getNameKeyObj(str) { | ||
var index = str.indexOf(NAME_KEY_SEP); | ||
if (str.indexOf(NAME_KEY_SEP) !== -1) { | ||
var name = str.slice(0, index); | ||
var key = str.slice(index + NAME_KEY_SEP.length); | ||
return { | ||
name: name, | ||
key: key | ||
}; | ||
} | ||
return { | ||
name: str | ||
}; | ||
} | ||
function flatFields(fields_, fieldsMeta) { | ||
var fields = _extends({}, fields_); | ||
Object.keys(fields).forEach(function (k) { | ||
if (fieldsMeta[k] && fieldsMeta[k].hasKey) { | ||
var value = fields[k]; | ||
// flatten | ||
for (var k2 in value) { | ||
if (value.hasOwnProperty(k2)) { | ||
fields[getNameKeyStr(k, k2)] = value[k2]; | ||
} | ||
} | ||
delete fields[k]; | ||
} | ||
}); | ||
return fields; | ||
} | ||
function flatFieldNames(names) { | ||
var ret = {}; | ||
names.forEach(function (n) { | ||
ret[getNameKeyObj(n).name] = 1; | ||
}); | ||
return Object.keys(ret); | ||
} |
{ | ||
"name": "rc-form", | ||
"version": "0.16.0", | ||
"version": "0.17.0", | ||
"description": "React High Order Form Component", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -112,2 +112,6 @@ # rc-form | ||
#### option.exclusive: boolean | ||
whether set value exclusively. used with radio. | ||
#### option.valuePropName: String | ||
@@ -114,0 +118,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
302
0
0
41233
8
834