@uform/core
Advanced tools
Comparing version 1.0.0-rc.3 to 1.0.0-rc.4
@@ -112,2 +112,7 @@ "use strict"; | ||
} | ||
else if (/array/gi.test(state.dataType) && | ||
state.value && | ||
state.value.length === 0) { | ||
state.value = initialValue; | ||
} | ||
} | ||
@@ -127,3 +132,3 @@ } | ||
field.setState(function (state) { | ||
if (state.visible) { | ||
if (state.visible || state.unmounted) { | ||
if (valuesChanged) { | ||
@@ -138,10 +143,10 @@ syncFieldValues(state); | ||
if (valuesChanged) { | ||
env.visiblePendingFields[state.name] = | ||
env.visiblePendingFields[state.name] || {}; | ||
env.visiblePendingFields[state.name].values = true; | ||
env.hiddenPendingFields[state.name] = | ||
env.hiddenPendingFields[state.name] || {}; | ||
env.hiddenPendingFields[state.name].values = true; | ||
} | ||
if (initialValuesChanged) { | ||
env.visiblePendingFields[state.name] = | ||
env.visiblePendingFields[state.name] || {}; | ||
env.visiblePendingFields[state.name].initialValues = true; | ||
env.hiddenPendingFields[state.name] = | ||
env.hiddenPendingFields[state.name] || {}; | ||
env.hiddenPendingFields[state.name].initialValues = true; | ||
} | ||
@@ -229,2 +234,21 @@ } | ||
var userUpdateFieldPath = env.userUpdateFields[env.userUpdateFields.length - 1]; | ||
var syncField = function () { | ||
if (env.hiddenPendingFields[published.name]) { | ||
field.setState(function (state) { | ||
if (env.hiddenPendingFields[state.name].values) { | ||
syncFieldValues(state); | ||
} | ||
if (env.hiddenPendingFields[state.name].initialValues) { | ||
syncFieldIntialValues(state); | ||
} | ||
delete env.hiddenPendingFields[state.name]; | ||
}); | ||
} | ||
}; | ||
var notifyFormValuesChange = function () { | ||
if (shared_1.isFn(options.onChange)) { | ||
options.onChange(state.getSourceState(function (state) { return shared_1.clone(state.values); })); | ||
} | ||
heart.publish(types_1.LifeCycleTypes.ON_FORM_VALUES_CHANGE, state); | ||
}; | ||
if (initializedChanged) { | ||
@@ -243,10 +267,15 @@ heart.publish(types_1.LifeCycleTypes.ON_FIELD_INIT, field); | ||
} | ||
var wasHidden = published.visible == false || published.unmounted === true; | ||
if (valueChanged) { | ||
userUpdating(field, function () { | ||
setFormValuesIn(path, published.value); | ||
}); | ||
if (!wasHidden) { | ||
userUpdating(field, function () { | ||
setFormValuesIn(path, published.value); | ||
}); | ||
} | ||
heart.publish(types_1.LifeCycleTypes.ON_FIELD_VALUE_CHANGE, field); | ||
} | ||
if (initialValueChanged) { | ||
setFormInitialValuesIn(path, published.initialValue); | ||
if (!wasHidden) { | ||
setFormInitialValuesIn(path, published.initialValue); | ||
} | ||
heart.publish(types_1.LifeCycleTypes.ON_FIELD_INITIAL_VALUE_CHANGE, field); | ||
@@ -256,19 +285,12 @@ } | ||
if (visibleChanged) { | ||
if (!published.visible) { | ||
deleteFormValuesIn(path, true); | ||
} | ||
else { | ||
setFormValuesIn(path, published.value); | ||
if (env.visiblePendingFields[published.name]) { | ||
field.setState(function (state) { | ||
if (env.visiblePendingFields[state.name].values) { | ||
syncFieldValues(state); | ||
} | ||
if (env.visiblePendingFields[state.name].initialValues) { | ||
syncFieldIntialValues(state); | ||
} | ||
delete env.visiblePendingFields[state.name]; | ||
}); | ||
userUpdating(field, function () { | ||
if (!published.visible) { | ||
deleteFormValuesIn(path, true); | ||
notifyFormValuesChange(); | ||
} | ||
} | ||
else { | ||
setFormValuesIn(path, published.value); | ||
syncField(); | ||
} | ||
}); | ||
} | ||
@@ -291,5 +313,7 @@ graph.eachChildren(path, function (childState) { | ||
deleteFormValuesIn(path, true); | ||
notifyFormValuesChange(); | ||
} | ||
else { | ||
setFormValuesIn(path, published.value); | ||
syncField(); | ||
} | ||
@@ -396,3 +420,3 @@ }); | ||
function registerField(_a) { | ||
var path = _a.path, name = _a.name, value = _a.value, initialValue = _a.initialValue, required = _a.required, rules = _a.rules, editable = _a.editable, visible = _a.visible, display = _a.display, computeState = _a.computeState, useDirty = _a.useDirty, props = _a.props; | ||
var path = _a.path, name = _a.name, value = _a.value, initialValue = _a.initialValue, required = _a.required, rules = _a.rules, editable = _a.editable, visible = _a.visible, display = _a.display, computeState = _a.computeState, dataType = _a.dataType, useDirty = _a.useDirty, props = _a.props; | ||
var field; | ||
@@ -409,2 +433,3 @@ var nodePath = shared_1.FormPath.parse(path || name); | ||
computeState: computeState, | ||
dataType: dataType, | ||
useDirty: shared_1.isValid(useDirty) ? useDirty : options.useDirty | ||
@@ -985,5 +1010,5 @@ }); | ||
return; | ||
var nodePath = field.getSourceState(function (state) { return state.path; }); | ||
var nodePath = field.state.path; | ||
if (nodePath) | ||
env.userUpdateFields.push(field.getSourceState(function (state) { return state.path; })); | ||
env.userUpdateFields.push(nodePath); | ||
if (shared_1.isFn(fn)) { | ||
@@ -1129,3 +1154,3 @@ fn(); | ||
removeNodes: {}, | ||
visiblePendingFields: {}, | ||
hiddenPendingFields: {}, | ||
lastShownStates: {}, | ||
@@ -1132,0 +1157,0 @@ submittingTask: undefined |
@@ -38,2 +38,17 @@ "use strict"; | ||
immer_1.setAutoFreeze(false); | ||
var defaults = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var result = {}; | ||
shared_1.each(args, function (target) { | ||
shared_1.each(target, function (value, key) { | ||
if (shared_1.isValid(value)) { | ||
result[key] = value; | ||
} | ||
}); | ||
}); | ||
return result; | ||
}; | ||
exports.createStateModel = function (Factory) { | ||
@@ -85,2 +100,23 @@ return (function (_super) { | ||
}; | ||
_this.watchProps = function (props, keys, callback) { | ||
if (!_this.cacheProps) { | ||
_this.cacheProps = __assign({}, props); | ||
} | ||
else { | ||
var changeNum_1 = 0; | ||
var changedProps_1 = {}; | ||
shared_1.toArr(keys).forEach(function (key) { | ||
if (!shared_1.isEqual(_this.cacheProps[key], props[key])) { | ||
changeNum_1++; | ||
changedProps_1[key] = props[key]; | ||
} | ||
}); | ||
if (changeNum_1 > 0) { | ||
if (shared_1.isFn(callback)) { | ||
callback(changedProps_1, props); | ||
} | ||
_this.cacheProps = __assign({}, props); | ||
} | ||
} | ||
}; | ||
_this.setState = function (callback, silent) { | ||
@@ -189,3 +225,3 @@ if (silent === void 0) { silent = false; } | ||
_this.prevState = __assign({}, Factory.defaultState); | ||
_this.props = __assign(__assign({}, Factory.defaultProps), defaultProps); | ||
_this.props = defaults(Factory.defaultProps, defaultProps); | ||
_this.dirtys = {}; | ||
@@ -192,0 +228,0 @@ _this.dirtyNum = 0; |
@@ -24,2 +24,3 @@ "use strict"; | ||
this.state.path = this.nodePath.entire; | ||
this.state.dataType = props.dataType || 'any'; | ||
} | ||
@@ -37,5 +38,10 @@ FieldState.prototype.readValues = function (_a) { | ||
} | ||
values = shared_1.toArr(values); | ||
if (/array/ig.test(this.state.dataType)) { | ||
value = shared_1.toArr(value); | ||
values[0] = shared_1.toArr(values[0]); | ||
} | ||
return { | ||
value: value, | ||
values: shared_1.toArr(values) | ||
values: values | ||
}; | ||
@@ -203,2 +209,3 @@ }; | ||
path: '', | ||
dataType: 'any', | ||
initialized: false, | ||
@@ -235,4 +242,5 @@ pristine: true, | ||
_a.defaultProps = { | ||
path: '' | ||
path: '', | ||
dataType: 'any' | ||
}, | ||
_a)); |
@@ -15,3 +15,3 @@ "use strict"; | ||
VirtualFieldState.prototype.computeState = function (draft, prevState) { | ||
if (draft.mounted === true) { | ||
if (draft.mounted === true && draft.mounted !== prevState.mounted) { | ||
draft.unmounted = false; | ||
@@ -22,3 +22,3 @@ } | ||
} | ||
if (draft.unmounted === true) { | ||
if (draft.unmounted === true && draft.unmounted !== prevState.unmounted) { | ||
draft.mounted = false; | ||
@@ -25,0 +25,0 @@ } |
@@ -82,2 +82,3 @@ import { FormPath, FormPathPattern, Subscribable } from '@uform/shared'; | ||
displayName?: string; | ||
dataType: string; | ||
name: string; | ||
@@ -121,2 +122,3 @@ path: string; | ||
dataPath?: FormPathPattern; | ||
dataType?: string; | ||
name?: string; | ||
@@ -133,2 +135,3 @@ value?: any; | ||
useDirty?: boolean; | ||
useListMode?: boolean; | ||
computeState?: (draft: IFieldState, prevState: IFieldState) => void; | ||
@@ -248,2 +251,7 @@ } | ||
setSourceState: (callback?: (state: S) => void) => void; | ||
watchProps: <T extends { | ||
[key: string]: any; | ||
}>(props: T, keys: string[], callback: (changedProps: { | ||
[key: string]: any; | ||
}, props: T) => void) => void; | ||
hasChanged: (path?: FormPathPattern) => boolean; | ||
@@ -250,0 +258,0 @@ isDirty: (key?: string) => boolean; |
{ | ||
"name": "@uform/core", | ||
"version": "1.0.0-rc.3", | ||
"version": "1.0.0-rc.4", | ||
"license": "MIT", | ||
@@ -29,4 +29,4 @@ "main": "lib", | ||
"dependencies": { | ||
"@uform/shared": "^1.0.0-rc.3", | ||
"@uform/validator": "^1.0.0-rc.3", | ||
"@uform/shared": "^1.0.0-rc.4", | ||
"@uform/validator": "^1.0.0-rc.4", | ||
"immer": "^3.2.0" | ||
@@ -37,3 +37,3 @@ }, | ||
}, | ||
"gitHead": "605adc99fb7e946d4dc2790d1ebda4c327d60777" | ||
"gitHead": "2b19603cfc5c63efcc07fe3773cf47c58019b14e" | ||
} |
@@ -99,2 +99,8 @@ import { | ||
state.value = initialValue | ||
} else if ( | ||
/array/gi.test(state.dataType) && | ||
state.value && | ||
state.value.length === 0 | ||
) { | ||
state.value = initialValue | ||
} | ||
@@ -116,3 +122,3 @@ } | ||
field.setState(state => { | ||
if (state.visible) { | ||
if (state.visible || state.unmounted) { | ||
if (valuesChanged) { | ||
@@ -127,10 +133,10 @@ syncFieldValues(state) | ||
if (valuesChanged) { | ||
env.visiblePendingFields[state.name] = | ||
env.visiblePendingFields[state.name] || {} | ||
env.visiblePendingFields[state.name].values = true | ||
env.hiddenPendingFields[state.name] = | ||
env.hiddenPendingFields[state.name] || {} | ||
env.hiddenPendingFields[state.name].values = true | ||
} | ||
if (initialValuesChanged) { | ||
env.visiblePendingFields[state.name] = | ||
env.visiblePendingFields[state.name] || {} | ||
env.visiblePendingFields[state.name].initialValues = true | ||
env.hiddenPendingFields[state.name] = | ||
env.hiddenPendingFields[state.name] || {} | ||
env.hiddenPendingFields[state.name].initialValues = true | ||
} | ||
@@ -233,2 +239,24 @@ } | ||
env.userUpdateFields[env.userUpdateFields.length - 1] | ||
const syncField = () => { | ||
if (env.hiddenPendingFields[published.name]) { | ||
field.setState((state: IFieldState) => { | ||
if (env.hiddenPendingFields[state.name].values) { | ||
syncFieldValues(state) | ||
} | ||
if (env.hiddenPendingFields[state.name].initialValues) { | ||
syncFieldIntialValues(state) | ||
} | ||
delete env.hiddenPendingFields[state.name] | ||
}) | ||
} | ||
} | ||
const notifyFormValuesChange = () => { | ||
if (isFn(options.onChange)) { | ||
options.onChange(state.getSourceState(state => clone(state.values))) | ||
} | ||
heart.publish(LifeCycleTypes.ON_FORM_VALUES_CHANGE, state) | ||
} | ||
if (initializedChanged) { | ||
@@ -246,10 +274,16 @@ heart.publish(LifeCycleTypes.ON_FIELD_INIT, field) | ||
} | ||
const wasHidden = | ||
published.visible == false || published.unmounted === true | ||
if (valueChanged) { | ||
userUpdating(field, () => { | ||
setFormValuesIn(path, published.value) | ||
}) | ||
if (!wasHidden) { | ||
userUpdating(field, () => { | ||
setFormValuesIn(path, published.value) | ||
}) | ||
} | ||
heart.publish(LifeCycleTypes.ON_FIELD_VALUE_CHANGE, field) | ||
} | ||
if (initialValueChanged) { | ||
setFormInitialValuesIn(path, published.initialValue) | ||
if (!wasHidden) { | ||
setFormInitialValuesIn(path, published.initialValue) | ||
} | ||
heart.publish(LifeCycleTypes.ON_FIELD_INITIAL_VALUE_CHANGE, field) | ||
@@ -259,18 +293,12 @@ } | ||
if (visibleChanged) { | ||
if (!published.visible) { | ||
deleteFormValuesIn(path, true) | ||
} else { | ||
setFormValuesIn(path, published.value) | ||
if (env.visiblePendingFields[published.name]) { | ||
field.setState((state: IFieldState) => { | ||
if (env.visiblePendingFields[state.name].values) { | ||
syncFieldValues(state) | ||
} | ||
if (env.visiblePendingFields[state.name].initialValues) { | ||
syncFieldIntialValues(state) | ||
} | ||
delete env.visiblePendingFields[state.name] | ||
}) | ||
userUpdating(field, () => { | ||
if (!published.visible) { | ||
deleteFormValuesIn(path, true) | ||
//考虑到隐藏删值,不应该同步子树,但是需要触发表单变化事件 | ||
notifyFormValuesChange() | ||
} else { | ||
setFormValuesIn(path, published.value) | ||
syncField() | ||
} | ||
} | ||
}) | ||
} | ||
@@ -295,4 +323,7 @@ graph.eachChildren(path, childState => { | ||
deleteFormValuesIn(path, true) | ||
//考虑到隐藏删值,不应该同步子树,但是需要触发表单变化事件 | ||
notifyFormValuesChange() | ||
} else { | ||
setFormValuesIn(path, published.value) | ||
syncField() | ||
} | ||
@@ -426,2 +457,3 @@ }) | ||
computeState, | ||
dataType, | ||
useDirty, | ||
@@ -441,2 +473,3 @@ props | ||
computeState, | ||
dataType, | ||
useDirty: isValid(useDirty) ? useDirty : options.useDirty | ||
@@ -1056,5 +1089,4 @@ }) | ||
if (!field) return | ||
const nodePath = field.getSourceState(state => state.path) | ||
if (nodePath) | ||
env.userUpdateFields.push(field.getSourceState(state => state.path)) | ||
const nodePath = field.state.path | ||
if (nodePath) env.userUpdateFields.push(nodePath) | ||
if (isFn(fn)) { | ||
@@ -1239,3 +1271,3 @@ fn() | ||
removeNodes: {}, | ||
visiblePendingFields: {}, | ||
hiddenPendingFields: {}, | ||
lastShownStates: {}, | ||
@@ -1242,0 +1274,0 @@ submittingTask: undefined |
@@ -10,3 +10,4 @@ import { | ||
FormPathPattern, | ||
isValid | ||
isValid, | ||
toArr | ||
} from '@uform/shared' | ||
@@ -25,2 +26,14 @@ import produce, { Draft, setAutoFreeze } from 'immer' | ||
const defaults = (...args:any[]):any=>{ | ||
const result = {} | ||
each(args,(target)=>{ | ||
each(target,(value,key)=>{ | ||
if(isValid(value)){ | ||
result[key] = value | ||
} | ||
}) | ||
}) | ||
return result | ||
} | ||
export const createStateModel = <State = {}, Props = {}>( | ||
@@ -37,2 +50,3 @@ Factory: IStateModelFactory<State, Props> | ||
} | ||
public cacheProps?: any | ||
public displayName?: string | ||
@@ -50,6 +64,3 @@ public dirtyNum: number | ||
this.prevState = { ...Factory.defaultState } | ||
this.props = { | ||
...Factory.defaultProps, | ||
...defaultProps | ||
} | ||
this.props = defaults(Factory.defaultProps,defaultProps) | ||
this.dirtys = {} | ||
@@ -107,2 +118,32 @@ this.dirtyNum = 0 | ||
watchProps = <T extends { [key: string]: any }>( | ||
props: T, | ||
keys: string[], | ||
callback: ( | ||
changedProps: { | ||
[key: string]: any | ||
}, | ||
props?: T | ||
) => void | ||
) => { | ||
if (!this.cacheProps) { | ||
this.cacheProps = { ...props } | ||
} else { | ||
let changeNum = 0 | ||
let changedProps = {} | ||
toArr(keys).forEach((key: string) => { | ||
if (!isEqual(this.cacheProps[key], props[key])) { | ||
changeNum++ | ||
changedProps[key] = props[key] | ||
} | ||
}) | ||
if (changeNum > 0) { | ||
if (isFn(callback)) { | ||
callback(changedProps, props) | ||
} | ||
this.cacheProps = { ...props } | ||
} | ||
} | ||
} | ||
setState = ( | ||
@@ -109,0 +150,0 @@ callback: (state: State | Draft<State>) => State | void, |
@@ -13,2 +13,3 @@ import { createStateModel } from '../shared/model' | ||
path: '', | ||
dataType: 'any', | ||
initialized: false, | ||
@@ -46,3 +47,4 @@ pristine: true, | ||
static defaultProps = { | ||
path: '' | ||
path: '', | ||
dataType: 'any' | ||
} | ||
@@ -62,2 +64,3 @@ | ||
this.state.path = this.nodePath.entire | ||
this.state.dataType = props.dataType || 'any' | ||
} | ||
@@ -74,5 +77,13 @@ | ||
} | ||
values = toArr(values) | ||
if(/array/ig.test(this.state.dataType)){ | ||
value = toArr(value) | ||
values[0] = toArr(values[0]) | ||
} | ||
return { | ||
value, | ||
values: toArr(values) | ||
values | ||
} | ||
@@ -79,0 +90,0 @@ } |
@@ -48,3 +48,3 @@ import { createStateModel } from '../shared/model' | ||
computeState(draft: IVirtualFieldState, prevState: IVirtualFieldState) { | ||
if (draft.mounted === true) { | ||
if (draft.mounted === true && draft.mounted !== prevState.mounted) { | ||
draft.unmounted = false | ||
@@ -55,3 +55,3 @@ } | ||
} | ||
if (draft.unmounted === true) { | ||
if (draft.unmounted === true && draft.unmounted !== prevState.unmounted) { | ||
draft.mounted = false | ||
@@ -58,0 +58,0 @@ } |
@@ -123,2 +123,3 @@ import { FormPath, FormPathPattern, isFn, Subscribable } from '@uform/shared' | ||
displayName?: string | ||
dataType: string | ||
name: string | ||
@@ -163,2 +164,3 @@ path: string | ||
dataPath?: FormPathPattern | ||
dataType?: string | ||
name?: string | ||
@@ -175,2 +177,3 @@ value?: any | ||
useDirty?: boolean | ||
useListMode?: boolean | ||
computeState?: (draft: IFieldState, prevState: IFieldState) => void | ||
@@ -328,2 +331,12 @@ } | ||
setSourceState: (callback?: (state: S) => void) => void | ||
watchProps: <T extends { [key: string]: any }>( | ||
props: T, | ||
keys: string[], | ||
callback: ( | ||
changedProps: { | ||
[key: string]: any | ||
}, | ||
props: T | ||
) => void | ||
) => void | ||
hasChanged: (path?: FormPathPattern) => boolean | ||
@@ -330,0 +343,0 @@ isDirty: (key?: string) => boolean |
237695
5105
Updated@uform/shared@^1.0.0-rc.4
Updated@uform/validator@^1.0.0-rc.4