@uform/core
Advanced tools
Comparing version 0.3.8 to 0.3.9-beta.0
154
lib/field.js
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
@@ -12,3 +13,4 @@ __assign = Object.assign || function(t) { | ||
}; | ||
import { Broadcast, publishFieldState, isEqual, clone, isFn, isBool, toArr, isStr, hasRequired, resolveFieldPath, isEmpty } from './utils'; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var utils_1 = require("./utils"); | ||
var filterSchema = function (value, key) { | ||
@@ -19,3 +21,3 @@ return key !== 'properties' && key !== 'items'; | ||
function Field(context, options) { | ||
this.fieldbrd = new Broadcast(); | ||
this.fieldbrd = new utils_1.Broadcast(); | ||
this.context = context; | ||
@@ -40,12 +42,12 @@ this.dirty = false; | ||
var rules = this.getRulesFromProps(options.props); | ||
this.value = !isEqual(this.value, options.value) | ||
? clone(options.value) | ||
this.value = !utils_1.isEqual(this.value, options.value) | ||
? utils_1.clone(options.value) | ||
: this.value; | ||
this.name = !isEmpty(options.name) ? options.name : this.name || ''; | ||
this.namePath = resolveFieldPath(this.name); | ||
this.path = resolveFieldPath(!isEmpty(options.path) ? options.path : this.path || []); | ||
this.rules = !isEmpty(rules) ? rules : this.rules; | ||
this.required = hasRequired(this.rules); | ||
if (isEmpty(options.props)) { | ||
this.initialValue = !isEmpty(options.initialValue) | ||
this.name = !utils_1.isEmpty(options.name) ? options.name : this.name || ''; | ||
this.namePath = utils_1.resolveFieldPath(this.name); | ||
this.path = utils_1.resolveFieldPath(!utils_1.isEmpty(options.path) ? options.path : this.path || []); | ||
this.rules = !utils_1.isEmpty(rules) ? rules : this.rules; | ||
this.required = utils_1.hasRequired(this.rules); | ||
if (utils_1.isEmpty(options.props)) { | ||
this.initialValue = !utils_1.isEmpty(options.initialValue) | ||
? options.initialValue | ||
@@ -55,21 +57,21 @@ : this.initialValue; | ||
else { | ||
this.initialValue = !isEqual(this.initialValue, options.initialValue) | ||
this.initialValue = !utils_1.isEqual(this.initialValue, options.initialValue) | ||
? options.initialValue | ||
: !isEmpty(this.initialValue) | ||
: !utils_1.isEmpty(this.initialValue) | ||
? this.initialValue | ||
: this.getInitialValueFromProps(options.props); | ||
this.props = !isEmpty(this.props) | ||
? __assign({}, this.props, clone(options.props)) : clone(options.props); | ||
this.props = !utils_1.isEmpty(this.props) | ||
? __assign({}, this.props, utils_1.clone(options.props)) : utils_1.clone(options.props); | ||
var editable = this.getEditableFromProps(options.props); | ||
this.editable = !isEmpty(editable) ? editable : this.getContextEditable(); | ||
this.editable = !utils_1.isEmpty(editable) ? editable : this.getContextEditable(); | ||
} | ||
if (this.pristine && | ||
!isEmpty(this.initialValue) && | ||
((isEmpty(this.value) && this.visible) || | ||
!utils_1.isEmpty(this.initialValue) && | ||
((utils_1.isEmpty(this.value) && this.visible) || | ||
(this.removed && !this.shownFromParent))) { | ||
this.value = clone(this.initialValue); | ||
this.value = utils_1.clone(this.initialValue); | ||
this.context.setIn(this.name, this.value); | ||
} | ||
this.mount(); | ||
if (isFn(options.onChange)) { | ||
if (utils_1.isFn(options.onChange)) { | ||
this.onChange(options.onChange); | ||
@@ -83,3 +85,3 @@ } | ||
if (props) { | ||
if (!isEqual(this.initialValue, props.default)) { | ||
if (!utils_1.isEqual(this.initialValue, props.default)) { | ||
return props.default; | ||
@@ -94,7 +96,7 @@ } | ||
if (props) { | ||
if (!isEmpty(props.editable)) { | ||
if (!utils_1.isEmpty(props.editable)) { | ||
return this.getEditable(props.editable); | ||
} | ||
else { | ||
if (props['x-props'] && !isEmpty(props['x-props'].editable)) { | ||
if (props['x-props'] && !utils_1.isEmpty(props['x-props'].editable)) { | ||
return this.getEditable(props['x-props'].editable); | ||
@@ -107,7 +109,7 @@ } | ||
if (props) { | ||
var rules = toArr(props['x-rules']); | ||
var rules = utils_1.toArr(props['x-rules']); | ||
if (props.required && !rules.some(function (rule) { return rule.required; })) { | ||
rules.push({ required: true }); | ||
} | ||
return clone(rules); | ||
return utils_1.clone(rules); | ||
} | ||
@@ -117,3 +119,3 @@ return this.rules; | ||
Field.prototype.getRequiredFromProps = function (props) { | ||
if (!isEmpty(props.required)) { | ||
if (!utils_1.isEmpty(props.required)) { | ||
return props.required; | ||
@@ -123,6 +125,6 @@ } | ||
Field.prototype.getEditable = function (editable) { | ||
if (isFn(editable)) { | ||
if (utils_1.isFn(editable)) { | ||
return editable(this.name); | ||
} | ||
if (isBool(editable)) { | ||
if (utils_1.isBool(editable)) { | ||
return editable; | ||
@@ -133,3 +135,3 @@ } | ||
Field.prototype.onChange = function (fn) { | ||
if (isFn(fn)) { | ||
if (utils_1.isFn(fn)) { | ||
if (this.unSubscribeOnChange) { | ||
@@ -143,3 +145,3 @@ this.unSubscribeOnChange(); | ||
Field.prototype.pathEqual = function (path) { | ||
if (isStr(path)) { | ||
if (utils_1.isStr(path)) { | ||
if (path === this.name) { | ||
@@ -149,3 +151,3 @@ return true; | ||
} | ||
path = resolveFieldPath(path); | ||
path = utils_1.resolveFieldPath(path); | ||
if (path.length === this.path.length) { | ||
@@ -170,6 +172,6 @@ for (var i = 0; i < path.length; i++) { | ||
Field.prototype.match = function (path) { | ||
if (isFn(path)) { | ||
if (utils_1.isFn(path)) { | ||
return path(this); | ||
} | ||
if (isStr(path)) { | ||
if (utils_1.isStr(path)) { | ||
if (path === this.name) { | ||
@@ -179,3 +181,3 @@ return true; | ||
} | ||
path = resolveFieldPath(path); | ||
path = utils_1.resolveFieldPath(path); | ||
if (path.length === this.path.length) { | ||
@@ -200,3 +202,3 @@ for (var i = 0; i < path.length; i++) { | ||
Field.prototype.publishState = function () { | ||
return publishFieldState(this); | ||
return utils_1.publishFieldState(this); | ||
}; | ||
@@ -207,6 +209,6 @@ Field.prototype.syncContextValue = function () { | ||
var contextInitialValue = this.context.getInitialValue(this.name, this.path); | ||
if (!isEqual(this.value, contextValue)) { | ||
if (!utils_1.isEqual(this.value, contextValue)) { | ||
this.value = contextValue; | ||
} | ||
if (!isEqual(this.initialValue, contextInitialValue)) { | ||
if (!utils_1.isEqual(this.initialValue, contextInitialValue)) { | ||
this.initialValue = contextInitialValue; | ||
@@ -232,7 +234,7 @@ } | ||
var lastProps = this.props; | ||
if (isEmpty(props)) { | ||
if (utils_1.isEmpty(props)) { | ||
return; | ||
} | ||
if (force || !isEqual(lastProps, props, filterSchema)) { | ||
this.props = clone(props, filterSchema); | ||
if (force || !utils_1.isEqual(lastProps, props, filterSchema)) { | ||
this.props = utils_1.clone(props, filterSchema); | ||
this.editable = this.getEditableFromProps(this.props); | ||
@@ -245,6 +247,6 @@ this.rules = this.getRulesFromProps(this.props); | ||
Field.prototype.changeEditable = function (editable) { | ||
if (!this.props || !isEmpty(this.props.editable)) { | ||
if (!this.props || !utils_1.isEmpty(this.props.editable)) { | ||
return; | ||
} | ||
if (this.props['x-props'] && !isEmpty(this.props['x-props'].editable)) { | ||
if (this.props['x-props'] && !utils_1.isEmpty(this.props['x-props'].editable)) { | ||
return; | ||
@@ -283,3 +285,3 @@ } | ||
if (published === void 0) { published = this.publishState(); } | ||
if (!isEqual(published.value, this.value)) { | ||
if (!utils_1.isEqual(published.value, this.value)) { | ||
this.value = published.value; | ||
@@ -292,3 +294,3 @@ this.pristine = false; | ||
} | ||
if (!isEqual(published.initialValue, this.initialValue)) { | ||
if (!utils_1.isEqual(published.initialValue, this.initialValue)) { | ||
this.initialValue = published.initialValue; | ||
@@ -301,3 +303,3 @@ this.context.setInitialValueIn(this.name, this.value); | ||
var editable = this.getEditable(published.editable); | ||
if (!isEqual(editable, this.editable)) { | ||
if (!utils_1.isEqual(editable, this.editable)) { | ||
this.editable = editable; | ||
@@ -310,5 +312,5 @@ this.dirtyType = 'editable'; | ||
var propsEditable = this.getEditableFromProps(published.props); | ||
if (!isEmpty(propsEditable) && | ||
!isEqual(propsEditable, this.editable) && | ||
!isEqual(prevEditable, propsEditable)) { | ||
if (!utils_1.isEmpty(propsEditable) && | ||
!utils_1.isEqual(propsEditable, this.editable) && | ||
!utils_1.isEqual(prevEditable, propsEditable)) { | ||
this.editable = propsEditable; | ||
@@ -319,4 +321,4 @@ this.dirtyType = 'editable'; | ||
} | ||
published.errors = toArr(published.errors).filter(function (v) { return !!v; }); | ||
if (!isEqual(published.errors, this.effectErrors)) { | ||
published.errors = utils_1.toArr(published.errors).filter(function (v) { return !!v; }); | ||
if (!utils_1.isEqual(published.errors, this.effectErrors)) { | ||
this.effectErrors = published.errors; | ||
@@ -328,7 +330,7 @@ this.valid = this.effectErrors.length > 0 && this.errors.length > 0; | ||
} | ||
if (!isEqual(published.rules, this.rules)) { | ||
if (!utils_1.isEqual(published.rules, this.rules)) { | ||
this.rules = published.rules; | ||
this.errors = []; | ||
this.valid = true; | ||
if (hasRequired(this.rules)) { | ||
if (utils_1.hasRequired(this.rules)) { | ||
this.required = true; | ||
@@ -344,8 +346,8 @@ published.required = true; | ||
var propsRules = this.getRulesFromProps(published.props); | ||
if (!isEmpty(propsRules) && | ||
!isEqual(prePropsRules, propsRules) && | ||
!isEqual(propsRules, this.rules)) { | ||
if (!utils_1.isEmpty(propsRules) && | ||
!utils_1.isEqual(prePropsRules, propsRules) && | ||
!utils_1.isEqual(propsRules, this.rules)) { | ||
this.rules = propsRules; | ||
this.errors = []; | ||
if (hasRequired(this.rules)) { | ||
if (utils_1.hasRequired(this.rules)) { | ||
this.required = true; | ||
@@ -360,7 +362,7 @@ published.required = true; | ||
} | ||
if (!isEqual(published.required, this.required)) { | ||
if (!utils_1.isEqual(published.required, this.required)) { | ||
this.required = !!published.required; | ||
if (this.required) { | ||
if (!hasRequired(this.rules)) { | ||
this.rules = toArr(this.rules).concat({ | ||
if (!utils_1.hasRequired(this.rules)) { | ||
this.rules = utils_1.toArr(this.rules).concat({ | ||
required: true | ||
@@ -374,3 +376,3 @@ }); | ||
else { | ||
this.rules = toArr(this.rules).filter(function (rule) { | ||
this.rules = utils_1.toArr(this.rules).filter(function (rule) { | ||
if (rule && rule.required) { | ||
@@ -389,8 +391,8 @@ return false; | ||
var propsRequired = this.getRequiredFromProps(published.props); | ||
if (!isEmpty(propsRequired) && !isEqual(propsRequired, this.required)) { | ||
if (!utils_1.isEmpty(propsRequired) && !utils_1.isEqual(propsRequired, this.required)) { | ||
this.required = !!propsRequired; | ||
this.errors = []; | ||
if (this.required) { | ||
if (!hasRequired(this.rules)) { | ||
this.rules = toArr(this.rules).concat({ | ||
if (!utils_1.hasRequired(this.rules)) { | ||
this.rules = utils_1.toArr(this.rules).concat({ | ||
required: true | ||
@@ -404,3 +406,3 @@ }); | ||
else { | ||
this.rules = toArr(this.rules).filter(function (rule) { | ||
this.rules = utils_1.toArr(this.rules).filter(function (rule) { | ||
if (rule && rule.required) { | ||
@@ -423,7 +425,7 @@ return false; | ||
} | ||
if (!isEqual(published.visible, this.visible)) { | ||
if (!utils_1.isEqual(published.visible, this.visible)) { | ||
this.visible = !!published.visible; | ||
if (this.visible) { | ||
this.value = | ||
this.value !== undefined ? this.value : clone(this.initialValue); | ||
this.value !== undefined ? this.value : utils_1.clone(this.initialValue); | ||
if (this.value !== undefined) { | ||
@@ -441,3 +443,3 @@ this.context.setIn(this.name, this.value); | ||
} | ||
if (!isEqual(published.display, this.display)) { | ||
if (!utils_1.isEqual(published.display, this.display)) { | ||
this.display = !!published.display; | ||
@@ -448,4 +450,4 @@ this.context.updateChildrenDisplay(this, this.display); | ||
} | ||
if (!isEqual(published.props, this.props, filterSchema)) { | ||
this.props = clone(published.props, filterSchema); | ||
if (!utils_1.isEqual(published.props, this.props, filterSchema)) { | ||
this.props = utils_1.clone(published.props, filterSchema); | ||
this.dirtyType = 'props'; | ||
@@ -456,3 +458,3 @@ this.dirty = true; | ||
Field.prototype.updateState = function (reducer) { | ||
if (!isFn(reducer)) { | ||
if (!utils_1.isFn(reducer)) { | ||
return; | ||
@@ -466,5 +468,5 @@ } | ||
path: this.path, | ||
props: clone(this.props, filterSchema), | ||
value: clone(this.value), | ||
initialValue: clone(this.initialValue), | ||
props: utils_1.clone(this.props, filterSchema), | ||
value: utils_1.clone(this.value), | ||
initialValue: utils_1.clone(this.initialValue), | ||
valid: this.valid, | ||
@@ -475,4 +477,4 @@ loading: this.loading, | ||
pristine: this.pristine, | ||
rules: clone(this.rules), | ||
errors: clone(this.effectErrors), | ||
rules: utils_1.clone(this.rules), | ||
errors: utils_1.clone(this.effectErrors), | ||
visible: this.visible, | ||
@@ -501,2 +503,2 @@ display: this.display, | ||
}()); | ||
export { Field }; | ||
exports.Field = Field; |
194
lib/form.js
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
@@ -12,8 +13,9 @@ __assign = Object.assign || function(t) { | ||
}; | ||
import { Broadcast, each, reduce, isEqual, isFn, isStr, isArr, setIn, getIn, deleteIn, clone, isEmpty, toArr, publishFormState, raf, caf, isChildField, getSchemaNodeFromPath, BufferList, defer } from './utils'; | ||
import { Field } from './field'; | ||
import { runValidation, format } from '@uform/validator'; | ||
import { Subject } from 'rxjs/internal/Subject'; | ||
import { filter } from 'rxjs/internal/operators/filter'; | ||
import { FormPath } from './path'; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var utils_1 = require("./utils"); | ||
var field_1 = require("./field"); | ||
var validator_1 = require("@uform/validator"); | ||
var Subject_1 = require("rxjs/internal/Subject"); | ||
var filter_1 = require("rxjs/internal/operators/filter"); | ||
var path_1 = require("./path"); | ||
var defaults = function (opts) { | ||
@@ -30,3 +32,3 @@ return (__assign({ initialValues: {}, values: {}, onSubmit: function (values) { }, effects: function ($) { } }, opts)); | ||
return new Promise(function (resolve) { | ||
if (isStr(path) || isArr(path) || isFn(path)) { | ||
if (utils_1.isStr(path) || utils_1.isArr(path) || utils_1.isFn(path)) { | ||
_this.updateQueue.push({ path: path, callback: callback, resolve: resolve }); | ||
@@ -40,5 +42,5 @@ } | ||
if (_this.updateRafId !== undefined) { | ||
caf(_this.updateRafId); | ||
utils_1.caf(_this.updateRafId); | ||
} | ||
_this.updateRafId = raf(function () { | ||
_this.updateRafId = utils_1.raf(function () { | ||
if (_this.destructed) { | ||
@@ -57,3 +59,3 @@ return; | ||
var field; | ||
each(_this.fields, function (innerField) { | ||
utils_1.each(_this.fields, function (innerField) { | ||
if (innerField.match(path)) { | ||
@@ -66,3 +68,3 @@ field = innerField; | ||
field.syncContextValue(); | ||
return isFn(callback) | ||
return utils_1.isFn(callback) | ||
? callback(field.publishState()) | ||
@@ -73,3 +75,3 @@ : field.publishState(); | ||
this.getFormState = function (callback) { | ||
return isFn(callback) ? callback(_this.publishState()) : _this.publishState(); | ||
return utils_1.isFn(callback) ? callback(_this.publishState()) : _this.publishState(); | ||
}; | ||
@@ -80,3 +82,3 @@ this.setFormState = function (reducer) { | ||
} | ||
if (!isFn(reducer)) { | ||
if (!utils_1.isFn(reducer)) { | ||
return; | ||
@@ -100,9 +102,9 @@ } | ||
if (!_this.subscribes[eventName]) { | ||
_this.subscribes[eventName] = new Subject(); | ||
_this.subscribes[eventName] = new Subject_1.Subject(); | ||
} | ||
if (isStr(eventFilter) || isFn(eventFilter)) { | ||
var predicate = isStr(eventFilter) | ||
? FormPath.match(eventFilter) | ||
if (utils_1.isStr(eventFilter) || utils_1.isFn(eventFilter)) { | ||
var predicate = utils_1.isStr(eventFilter) | ||
? path_1.FormPath.match(eventFilter) | ||
: eventFilter; | ||
return _this.subscribes[eventName].pipe(filter(predicate)); | ||
return _this.subscribes[eventName].pipe(filter_1.filter(predicate)); | ||
} | ||
@@ -112,3 +114,3 @@ return _this.subscribes[eventName]; | ||
this.options = defaults(opts); | ||
this.publisher = new Broadcast(); | ||
this.publisher = new utils_1.Broadcast(); | ||
this.initialized = false; | ||
@@ -119,3 +121,3 @@ this.state = {}; | ||
this.updateQueue = []; | ||
this.updateBuffer = new BufferList(); | ||
this.updateBuffer = new utils_1.BufferList(); | ||
this.editable = opts.editable; | ||
@@ -138,3 +140,3 @@ this.schema = opts.schema || {}; | ||
this.state.dirty = | ||
lastDirty || (this.initialized ? !isEqual(values, lastValues) : false); | ||
lastDirty || (this.initialized ? !utils_1.isEqual(values, lastValues) : false); | ||
this.updateFieldsValue(); | ||
@@ -144,3 +146,3 @@ }; | ||
this.editable = editable; | ||
each(this.fields, function (field, name) { | ||
utils_1.each(this.fields, function (field, name) { | ||
field.changeEditable(editable); | ||
@@ -150,3 +152,3 @@ }); | ||
Form.prototype.isDirtyValues = function (values) { | ||
return !isEmpty(values) && !isEqual(this.state.values, values); | ||
return !utils_1.isEmpty(values) && !utils_1.isEqual(this.state.values, values); | ||
}; | ||
@@ -170,3 +172,3 @@ Form.prototype.registerField = function (name, options) { | ||
else { | ||
this.fields[name] = new Field(this, { | ||
this.fields[name] = new field_1.Field(this, { | ||
name: name, | ||
@@ -178,9 +180,9 @@ value: value, | ||
}); | ||
var field_1 = this.fields[name]; | ||
var field_2 = this.fields[name]; | ||
if (options.onChange) { | ||
this.asyncUpdate(function () { | ||
_this.updateFieldStateFromBuffer(field_1); | ||
field_1.onChange(options.onChange); | ||
_this.updateFieldStateFromBuffer(field_2); | ||
field_2.onChange(options.onChange); | ||
}); | ||
this.dispatchEffect('onFieldChange', field_1.publishState()); | ||
this.dispatchEffect('onFieldChange', field_2.publishState()); | ||
} | ||
@@ -193,8 +195,8 @@ this.fieldSize++; | ||
var _this = this; | ||
setIn(this.state.values, name, value, function (path) { | ||
return getSchemaNodeFromPath(_this.schema, path); | ||
utils_1.setIn(this.state.values, name, value, function (path) { | ||
return utils_1.getSchemaNodeFromPath(_this.schema, path); | ||
}); | ||
}; | ||
Form.prototype.setInitialValueIn = function (name, value) { | ||
setIn(this.state.initialValues, name, value); | ||
utils_1.setIn(this.state.initialValues, name, value); | ||
}; | ||
@@ -222,8 +224,8 @@ Form.prototype.setValue = function (name, value) { | ||
} | ||
errors = toArr(errors); | ||
errors = utils_1.toArr(errors); | ||
var field = this.fields[name]; | ||
if (field) { | ||
var lastErrors = field.errors; | ||
if (!isEqual(lastErrors, errors)) { | ||
field.errors = errors.map(function (msg) { return format.apply(void 0, [msg].concat(args)); }); | ||
if (!utils_1.isEqual(lastErrors, errors)) { | ||
field.errors = errors.map(function (msg) { return validator_1.format.apply(void 0, [msg].concat(args)); }); | ||
if (errors.length) { | ||
@@ -247,6 +249,6 @@ field.invalid = true; | ||
} | ||
each(this.fields, function (field, $name) { | ||
if (isChildField(field, parent)) { | ||
utils_1.each(this.fields, function (field, $name) { | ||
if (utils_1.isChildField(field, parent)) { | ||
var newValue = _this.getValue($name); | ||
if (!isEqual(field.value, newValue)) { | ||
if (!utils_1.isEqual(field.value, newValue)) { | ||
field.dirty = true; | ||
@@ -265,6 +267,6 @@ field.value = newValue; | ||
} | ||
each(this.fields, function (field, $name) { | ||
if (isChildField(field, parent)) { | ||
utils_1.each(this.fields, function (field, $name) { | ||
if (utils_1.isChildField(field, parent)) { | ||
var newValue = _this.getInitialValue($name); | ||
if (!isEqual(field.initialValue, newValue)) { | ||
if (!utils_1.isEqual(field.initialValue, newValue)) { | ||
field.dirty = true; | ||
@@ -279,3 +281,3 @@ field.initialValue = newValue; | ||
if (this.state.dirty && this.initialized) { | ||
each(this.fields, function (field, name) { | ||
utils_1.each(this.fields, function (field, name) { | ||
var newValue = _this.getInitialValue(name); | ||
@@ -290,7 +292,7 @@ field.initialValue = newValue; | ||
if (validate === void 0) { validate = true; } | ||
var _a = defer(), promise = _a.promise, resolve = _a.resolve; | ||
var _a = utils_1.defer(), promise = _a.promise, resolve = _a.resolve; | ||
var update = function () { | ||
var updateList = []; | ||
_this.batchUpdateField = true; | ||
each(_this.fields, function (field, name) { | ||
utils_1.each(_this.fields, function (field, name) { | ||
var newValue = _this.getValue(name); | ||
@@ -302,3 +304,3 @@ field.updateState(function (state) { | ||
updateList.push(new Promise(function (resolve) { | ||
raf(function () { | ||
utils_1.raf(function () { | ||
if (_this.destructed) { | ||
@@ -335,7 +337,7 @@ return; | ||
} | ||
each(this.fields, function (field, $name) { | ||
utils_1.each(this.fields, function (field, $name) { | ||
if ($name === parent.name) { | ||
return; | ||
} | ||
if (isChildField(field, parent)) { | ||
if (utils_1.isChildField(field, parent)) { | ||
if (!visible) { | ||
@@ -345,3 +347,3 @@ _this.deleteIn($name); | ||
else { | ||
var value = field.value !== undefined ? field.value : clone(field.initialValue); | ||
var value = field.value !== undefined ? field.value : utils_1.clone(field.initialValue); | ||
if (field.value !== undefined) { | ||
@@ -374,7 +376,7 @@ _this.setIn($name, value); | ||
} | ||
each(this.fields, function (field, $name) { | ||
utils_1.each(this.fields, function (field, $name) { | ||
if ($name === parent.name) { | ||
return; | ||
} | ||
if (isChildField(field, parent)) { | ||
if (utils_1.isChildField(field, parent)) { | ||
if (field.display !== display) { | ||
@@ -400,7 +402,7 @@ if (display) { | ||
Form.prototype.getInitialValue = function (name, path) { | ||
var initialValue = getIn(this.state.initialValues, name); | ||
var initialValue = utils_1.getIn(this.state.initialValues, name); | ||
var schema; | ||
var schemaDefault; | ||
if (initialValue === undefined) { | ||
schema = path ? getSchemaNodeFromPath(this.schema, path) : undefined; | ||
schema = path ? utils_1.getSchemaNodeFromPath(this.schema, path) : undefined; | ||
schemaDefault = schema && schema.default; | ||
@@ -415,10 +417,10 @@ if (schemaDefault !== undefined) { | ||
return copy | ||
? clone(getIn(this.state.values, name)) | ||
: getIn(this.state.values, name); | ||
? utils_1.clone(utils_1.getIn(this.state.values, name)) | ||
: utils_1.getIn(this.state.values, name); | ||
}; | ||
Form.prototype.deleteIn = function (name) { | ||
deleteIn(this.state.values, name); | ||
utils_1.deleteIn(this.state.values, name); | ||
}; | ||
Form.prototype.deleteInitialValues = function (name) { | ||
deleteIn(this.state.initialValues, name); | ||
utils_1.deleteIn(this.state.initialValues, name); | ||
}; | ||
@@ -428,3 +430,3 @@ Form.prototype.reset = function (forceClear, validate) { | ||
if (validate === void 0) { validate = true; } | ||
each(this.fields, function (field, name) { | ||
utils_1.each(this.fields, function (field, name) { | ||
var value = _this.getValue(name); | ||
@@ -442,3 +444,3 @@ var initialValue = _this.getInitialValue(name, field.path); | ||
} | ||
if (!isEmpty(value) || !isEmpty(initialValue)) { | ||
if (!utils_1.isEmpty(value) || !utils_1.isEmpty(initialValue)) { | ||
field.updateState(function (state) { | ||
@@ -449,3 +451,3 @@ state.value = forceClear ? undefined : initialValue; | ||
if (field.dirty) { | ||
raf(function () { | ||
utils_1.raf(function () { | ||
if (_this.destructed) { | ||
@@ -461,3 +463,3 @@ return; | ||
this.dispatchEffect('onFormReset', formState); | ||
if (isFn(this.options.onReset)) { | ||
if (utils_1.isFn(this.options.onReset)) { | ||
this.options.onReset({ formState: formState }); | ||
@@ -469,3 +471,3 @@ } | ||
_this.formNotify(); | ||
raf(function () { | ||
utils_1.raf(function () { | ||
if (_this.destructed) { | ||
@@ -476,3 +478,3 @@ return; | ||
_this.dispatchEffect('onFormReset', formState); | ||
if (isFn(_this.options.onReset)) { | ||
if (utils_1.isFn(_this.options.onReset)) { | ||
_this.options.onReset({ formState: formState }); | ||
@@ -485,7 +487,7 @@ } | ||
Form.prototype.publishState = function () { | ||
return publishFormState(this.state); | ||
return utils_1.publishFormState(this.state); | ||
}; | ||
Form.prototype.formNotify = function (fieldState) { | ||
var formState = this.publishState(); | ||
if (isFn(this.options.onFieldChange)) { | ||
if (utils_1.isFn(this.options.onFieldChange)) { | ||
this.options.onFieldChange({ formState: formState, fieldState: fieldState }); | ||
@@ -507,3 +509,3 @@ } | ||
_this.formNotify(); | ||
raf(function () { | ||
utils_1.raf(function () { | ||
if (_this.destructed) { | ||
@@ -529,3 +531,3 @@ return; | ||
_this.dispatchEffect('onFormSubmit', formState); | ||
if (isFn(_this.options.onSubmit)) { | ||
if (utils_1.isFn(_this.options.onSubmit)) { | ||
_this.options.onSubmit({ formState: formState }); | ||
@@ -546,6 +548,6 @@ } | ||
this.publisher.unsubscribe(); | ||
each(this.subscribes, function (effect) { | ||
utils_1.each(this.subscribes, function (effect) { | ||
effect.unsubscribe(); | ||
}); | ||
each(this.fields, function (field, key) { | ||
utils_1.each(this.fields, function (field, key) { | ||
field.destructor(); | ||
@@ -559,3 +561,3 @@ delete _this.fields[key]; | ||
Form.prototype.syncUpdate = function (fn) { | ||
if (isFn(fn)) { | ||
if (utils_1.isFn(fn)) { | ||
this.syncUpdateMode = true; | ||
@@ -570,6 +572,6 @@ fn(); | ||
var lastDirty = this.state.dirty; | ||
var currentInitialValues = clone(initialValues) || {}; | ||
var currentValues = isEmpty(values) | ||
? clone(currentInitialValues) | ||
: clone(values) || {}; | ||
var currentInitialValues = utils_1.clone(initialValues) || {}; | ||
var currentValues = utils_1.isEmpty(values) | ||
? utils_1.clone(currentInitialValues) | ||
: utils_1.clone(values) || {}; | ||
this.state = { | ||
@@ -583,3 +585,3 @@ valid: true, | ||
dirty: lastDirty || | ||
(this.initialized ? !isEqual(currentValues, lastValues) : false) | ||
(this.initialized ? !utils_1.isEqual(currentValues, lastValues) : false) | ||
}; | ||
@@ -596,3 +598,3 @@ if (this.options.onFormChange && !this.initialized) { | ||
var effects = this.options.effects; | ||
if (isFn(effects)) { | ||
if (utils_1.isFn(effects)) { | ||
effects(this.selectEffect, { | ||
@@ -607,3 +609,3 @@ setFieldState: this.setFieldState, | ||
Form.prototype.checkState = function (published) { | ||
if (!isEqual(this.state.values, published.values)) { | ||
if (!utils_1.isEqual(this.state.values, published.values)) { | ||
this.state.values = published.values; | ||
@@ -613,3 +615,3 @@ this.state.dirty = true; | ||
} | ||
if (!isEqual(this.state.initialValues, published.initialValues)) { | ||
if (!utils_1.isEqual(this.state.initialValues, published.initialValues)) { | ||
this.state.initialValues = published.initialValues; | ||
@@ -622,3 +624,3 @@ this.state.dirty = true; | ||
Form.prototype.asyncUpdate = function (fn) { | ||
if (isFn(fn)) { | ||
if (utils_1.isFn(fn)) { | ||
if (this.syncUpdateMode) { | ||
@@ -638,7 +640,7 @@ this.syncUpdateMode = false; | ||
var rafIdMap = {}; | ||
each(this.updateQueue, function (_a, i) { | ||
utils_1.each(this.updateQueue, function (_a, i) { | ||
var path = _a.path, callback = _a.callback, resolve = _a.resolve; | ||
each(_this.fields, function (field) { | ||
if (path && (isFn(path) || isArr(path) || isStr(path))) { | ||
if (isFn(path) ? path(field) : field.pathEqual(path)) { | ||
utils_1.each(_this.fields, function (field) { | ||
if (path && (utils_1.isFn(path) || utils_1.isArr(path) || utils_1.isStr(path))) { | ||
if (utils_1.isFn(path) ? path(field) : field.pathEqual(path)) { | ||
field.updateState(callback); | ||
@@ -655,5 +657,5 @@ if (_this.syncUpdateMode) { | ||
if (rafIdMap[field.name]) { | ||
caf(rafIdMap[field.name]); | ||
utils_1.caf(rafIdMap[field.name]); | ||
} | ||
rafIdMap[field.name] = raf(function () { | ||
rafIdMap[field.name] = utils_1.raf(function () { | ||
if (_this.destructed) { | ||
@@ -677,3 +679,3 @@ return; | ||
if (_this.fieldSize <= failed[i]) { | ||
if (isArr(path)) { | ||
if (utils_1.isArr(path)) { | ||
_this.updateBuffer.push(path.join('.'), callback, { | ||
@@ -684,6 +686,6 @@ path: path, | ||
} | ||
else if (isStr(path)) { | ||
else if (utils_1.isStr(path)) { | ||
_this.updateBuffer.push(path, callback, { path: path, resolve: resolve }); | ||
} | ||
else if (isFn(path) && path.pattern) { | ||
else if (utils_1.isFn(path) && path.pattern) { | ||
_this.updateBuffer.push(path.pattern, callback, { | ||
@@ -698,3 +700,3 @@ path: path, | ||
}); | ||
if (resolve && isFn(resolve)) { | ||
if (resolve && utils_1.isFn(resolve)) { | ||
resolve(); | ||
@@ -710,3 +712,3 @@ } | ||
var path = _a.path, values = _a.values, key = _a.key; | ||
if (isFn(path) ? path(field) : field.pathEqual(path)) { | ||
if (utils_1.isFn(path) ? path(field) : field.pathEqual(path)) { | ||
values.forEach(function (callback) { return field.updateState(callback); }); | ||
@@ -720,5 +722,5 @@ if (_this.syncUpdateMode) { | ||
if (rafIdMap[field.name]) { | ||
caf(rafIdMap[field.name]); | ||
utils_1.caf(rafIdMap[field.name]); | ||
} | ||
rafIdMap[field.name] = raf(function () { | ||
rafIdMap[field.name] = utils_1.raf(function () { | ||
if (_this.destructed) { | ||
@@ -751,12 +753,12 @@ return; | ||
if (_this.rafValidateId) { | ||
caf(_this.rafValidateId); | ||
utils_1.caf(_this.rafValidateId); | ||
} | ||
_this.rafValidateId = raf(function () { | ||
_this.rafValidateId = utils_1.raf(function () { | ||
if (_this.destructed) { | ||
return resolve(); | ||
} | ||
return runValidation(values || _this.state.values, _this.fields, forceUpdate) | ||
return validator_1.runValidation(values || _this.state.values, _this.fields, forceUpdate) | ||
.then(function (response) { | ||
var lastValid = _this.state.valid; | ||
var newErrors = reduce(response, function (buf, _a) { | ||
var newErrors = utils_1.reduce(response, function (buf, _a) { | ||
var name = _a.name, errors = _a.errors; | ||
@@ -777,3 +779,3 @@ if (!errors.length) { | ||
var lastPristine = _this.state.pristine; | ||
if (!isEqual(_this.state.values, _this.state.initialValues)) { | ||
if (!utils_1.isEqual(_this.state.values, _this.state.initialValues)) { | ||
_this.state.pristine = false; | ||
@@ -795,2 +797,2 @@ } | ||
}()); | ||
export { Form }; | ||
exports.Form = Form; |
@@ -1,13 +0,22 @@ | ||
import { setLocale as setValidationLocale, setLanguage as setValidationLanguage } from '@uform/validator'; | ||
import { Form } from './form'; | ||
import { caculateSchemaInitialValues, isFn, each, isEmpty, clone } from './utils'; | ||
export * from './path'; | ||
export var createForm = function (_a) { | ||
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var validator_1 = require("@uform/validator"); | ||
exports.setValidationLocale = validator_1.setLocale; | ||
exports.setValidationLanguage = validator_1.setLanguage; | ||
var form_1 = require("./form"); | ||
exports.Form = form_1.Form; | ||
var utils_1 = require("./utils"); | ||
exports.caculateSchemaInitialValues = utils_1.caculateSchemaInitialValues; | ||
__export(require("./path")); | ||
exports.createForm = function (_a) { | ||
var initialValues = _a.initialValues, values = _a.values, onSubmit = _a.onSubmit, onReset = _a.onReset, schema = _a.schema, onFormChange = _a.onFormChange, onFieldChange = _a.onFieldChange, onFormWillInit = _a.onFormWillInit, subscribes = _a.subscribes, editable = _a.editable, effects = _a.effects, onValidateFailed = _a.onValidateFailed, traverse = _a.traverse; | ||
var fields = []; | ||
var calculatedValues = caculateSchemaInitialValues(schema, isEmpty(values) ? clone(initialValues) : clone(values), function (_a, schema, value) { | ||
var calculatedValues = utils_1.caculateSchemaInitialValues(schema, utils_1.isEmpty(values) ? utils_1.clone(initialValues) : utils_1.clone(values), function (_a, schema, value) { | ||
var name = _a.name, path = _a.path, schemaPath = _a.schemaPath; | ||
fields.push({ name: name, path: path, schemaPath: schemaPath, schema: schema, value: value }); | ||
}); | ||
if (isEmpty(values)) { | ||
if (utils_1.isEmpty(values)) { | ||
initialValues = calculatedValues; | ||
@@ -18,3 +27,3 @@ } | ||
} | ||
var form = new Form({ | ||
var form = new form_1.Form({ | ||
initialValues: initialValues, | ||
@@ -33,3 +42,3 @@ values: values, | ||
}); | ||
if (isFn(onFormWillInit)) { | ||
if (utils_1.isFn(onFormWillInit)) { | ||
onFormWillInit(form); | ||
@@ -46,3 +55,3 @@ } | ||
form.dispatchEffect('onFormInit', form.publishState()); | ||
each(fields, function (field) { | ||
utils_1.each(fields, function (field) { | ||
form.dispatchEffect('onFieldChange', field.publishState()); | ||
@@ -53,3 +62,2 @@ }, true); | ||
}; | ||
export { setValidationLocale, setValidationLanguage, Form, caculateSchemaInitialValues }; | ||
export default createForm; | ||
exports.default = exports.createForm; |
@@ -1,5 +0,10 @@ | ||
import createMatcher from 'dot-match'; | ||
import { resolveFieldPath, isStr, isFn, isArr, reduce } from './utils'; | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var dot_match_1 = __importDefault(require("dot-match")); | ||
var utils_1 = require("./utils"); | ||
var matchWithFilter = function (result, filter, payload) { | ||
if (isFn(filter)) { | ||
if (utils_1.isFn(filter)) { | ||
return result && filter(payload); | ||
@@ -10,7 +15,7 @@ } | ||
var wildcardRE = /\*/; | ||
export var FormPath = { | ||
exports.FormPath = { | ||
match: function (pattern, isRealPath, filter) { | ||
pattern = pattern + ''; | ||
var match = createMatcher(pattern); | ||
if (isFn(isRealPath)) { | ||
var match = dot_match_1.default(pattern); | ||
if (utils_1.isFn(isRealPath)) { | ||
filter = isRealPath; | ||
@@ -21,13 +26,13 @@ isRealPath = false; | ||
if (payload && payload.fieldState) { | ||
return matchWithFilter(match(resolveFieldPath(isRealPath ? payload.fieldState.path : payload.fieldState.name)), filter, payload.fieldState); | ||
return matchWithFilter(match(utils_1.resolveFieldPath(isRealPath ? payload.fieldState.path : payload.fieldState.name)), filter, payload.fieldState); | ||
} | ||
else if (payload && payload.name && payload.path) { | ||
return matchWithFilter(match(resolveFieldPath(isRealPath ? payload.path : payload.name)), filter, payload); | ||
return matchWithFilter(match(utils_1.resolveFieldPath(isRealPath ? payload.path : payload.name)), filter, payload); | ||
} | ||
else if (isStr(payload)) { | ||
return matchWithFilter(match(resolveFieldPath(payload)), filter, { | ||
else if (utils_1.isStr(payload)) { | ||
return matchWithFilter(match(utils_1.resolveFieldPath(payload)), filter, { | ||
name: payload | ||
}); | ||
} | ||
else if (isArr(payload)) { | ||
else if (utils_1.isArr(payload)) { | ||
return matchWithFilter(match(payload), filter, { path: payload }); | ||
@@ -43,6 +48,6 @@ } | ||
return function (path) { | ||
return isFn(matcher) | ||
return utils_1.isFn(matcher) | ||
? !matcher(path) | ||
: isStr(matcher) | ||
? !FormPath.match(matcher)(path) | ||
: utils_1.isStr(matcher) | ||
? !exports.FormPath.match(matcher)(path) | ||
: false; | ||
@@ -52,3 +57,3 @@ }; | ||
transform: function (path, regexp, calllback) { | ||
var args = reduce(resolveFieldPath(path), function (buf, key) { | ||
var args = utils_1.reduce(utils_1.resolveFieldPath(path), function (buf, key) { | ||
return new RegExp(regexp).test(key) ? buf.concat(key) : buf; | ||
@@ -55,0 +60,0 @@ }, []); |
@@ -0,1 +1,2 @@ | ||
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
@@ -12,6 +13,10 @@ __assign = Object.assign || function(t) { | ||
}; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
var _a; | ||
import { isArr, isStr, getPathSegments, toArr, clone, isFn, globalThisPolyfill } from '@uform/utils'; | ||
export * from '@uform/utils'; | ||
var self = globalThisPolyfill; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var utils_1 = require("@uform/utils"); | ||
__export(require("@uform/utils")); | ||
var self = utils_1.globalThisPolyfill; | ||
var getScheduler = function () { | ||
@@ -32,12 +37,12 @@ if (!self.requestAnimationFrame) { | ||
}; | ||
export var raf = (_a = getScheduler(), _a[0]), caf = _a[1]; | ||
export var resolveFieldPath = function (path) { | ||
if (!isArr(path)) { | ||
return isStr(path) ? resolveFieldPath(getPathSegments(path)) : undefined; | ||
exports.raf = (_a = getScheduler(), _a[0]), exports.caf = _a[1]; | ||
exports.resolveFieldPath = function (path) { | ||
if (!utils_1.isArr(path)) { | ||
return utils_1.isStr(path) ? exports.resolveFieldPath(utils_1.getPathSegments(path)) : undefined; | ||
} | ||
return path.reduce(function (buf, key) { | ||
return buf.concat(getPathSegments(key)); | ||
return buf.concat(utils_1.getPathSegments(key)); | ||
}, []); | ||
}; | ||
export var isChildField = function (field, parent) { | ||
exports.isChildField = function (field, parent) { | ||
if (field && parent && field.path && parent.path) { | ||
@@ -53,11 +58,11 @@ for (var i = 0; i < parent.path.length; i++) { | ||
}; | ||
export var hasRequired = function (rules) { | ||
return toArr(rules).some(function (rule) { | ||
exports.hasRequired = function (rules) { | ||
return utils_1.toArr(rules).some(function (rule) { | ||
return rule && rule.required; | ||
}); | ||
}; | ||
export var publishFormState = function (state) { | ||
exports.publishFormState = function (state) { | ||
var values = state.values, valid = state.valid, invalid = state.invalid, initialValues = state.initialValues, errors = state.errors, pristine = state.pristine, dirty = state.dirty; | ||
return { | ||
values: clone(values), | ||
values: utils_1.clone(values), | ||
valid: valid, | ||
@@ -68,9 +73,9 @@ invalid: invalid, | ||
dirty: dirty, | ||
initialValues: clone(initialValues) | ||
initialValues: utils_1.clone(initialValues) | ||
}; | ||
}; | ||
export var publishFieldState = function (state) { | ||
exports.publishFieldState = function (state) { | ||
var value = state.value, valid = state.valid, invalid = state.invalid, errors = state.errors, visible = state.visible, display = state.display, editable = state.editable, initialValue = state.initialValue, name = state.name, path = state.path, props = state.props, effectErrors = state.effectErrors, loading = state.loading, pristine = state.pristine, required = state.required, rules = state.rules; | ||
return { | ||
value: clone(value), | ||
value: utils_1.clone(value), | ||
valid: valid, | ||
@@ -84,3 +89,3 @@ invalid: invalid, | ||
pristine: pristine, | ||
initialValue: clone(initialValue), | ||
initialValue: utils_1.clone(initialValue), | ||
name: name, | ||
@@ -113,3 +118,3 @@ path: path, | ||
for (var i = 0; i < this.data.length; i++) { | ||
if (isFn(callback)) { | ||
if (utils_1.isFn(callback)) { | ||
callback(this.data[i], this.data[i].key); | ||
@@ -134,2 +139,2 @@ } | ||
}()); | ||
export { BufferList }; | ||
exports.BufferList = BufferList; |
{ | ||
"name": "@uform/core", | ||
"version": "0.3.8", | ||
"version": "0.3.9-beta.0", | ||
"license": "MIT", | ||
@@ -29,5 +29,5 @@ "main": "lib", | ||
"dependencies": { | ||
"@uform/types": "^0.3.8", | ||
"@uform/utils": "^0.3.8", | ||
"@uform/validator": "^0.3.8", | ||
"@uform/types": "^0.3.9-beta.0", | ||
"@uform/utils": "^0.3.9-beta.0", | ||
"@uform/validator": "^0.3.9-beta.0", | ||
"dot-match": "^0.1.18", | ||
@@ -39,3 +39,3 @@ "rxjs": "^6.3.3" | ||
}, | ||
"gitHead": "eb54c502a6236b9ac3249027576341cf30340aa8" | ||
"gitHead": "e2aeb3e405507b90c9d7d5a2b398446ced51d7d5" | ||
} |
109642
3284
Updated@uform/types@^0.3.9-beta.0
Updated@uform/utils@^0.3.9-beta.0