New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@formily/core

Package Overview
Dependencies
Maintainers
2
Versions
242
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@formily/core - npm Package Compare versions

Comparing version 1.2.2 to 1.2.3-beta.0

8

lib/externals.d.ts

@@ -62,2 +62,3 @@ import { IFieldRegistryProps, IFieldState, IVirtualFieldRegistryProps, IFormState, IVirtualFieldState, FormHeartSubscriber, IFormSubmitResult, IFormResetOptions, IFormExtendedValidateFieldOptions } from './types';

lastShownStates: {};
clearStatesPatterns: {};
submittingTask: any;

@@ -75,2 +76,5 @@ };

updateRecoverableShownState: (parentState: IVirtualFieldState<FormilyCore.VirtualFieldProps> | IFieldState<FormilyCore.FieldProps>, childState: IVirtualFieldState<FormilyCore.VirtualFieldProps> | IFieldState<FormilyCore.FieldProps>, name: "visible" | "display") => void;
disableUnmountClearStates: (pattern?: FormPathPattern) => void;
enableUnmountClearStates: (pattern?: FormPathPattern) => void;
supportUnmountClearStates: (path: FormPathPattern) => any;
resetFormMessages: (fieldState: IFieldState<any>) => void;

@@ -172,3 +176,3 @@ syncFormMessages: (type: string, fieldState: IFieldState<any>) => void;

getFieldState: (path: FormPathPattern, callback?: (state: IFieldState<FormilyCore.FieldProps>) => any) => any;
registerField: ({ path, name, value, initialValue, required, rules, editable, visible, display, computeState, dataType, unmountRemoveValue, props }: IFieldRegistryProps<FormilyCore.FieldProps>) => {
registerField: ({ path, name, value, initialValue, required, rules, editable, visible, display, computeState, dataType, props }: IFieldRegistryProps<FormilyCore.FieldProps>) => {
factory: import("./types").IModelSpec<Partial<IFieldState<any>>, import("./types").IFieldStateProps>;

@@ -279,2 +283,4 @@ state: IFieldState<any>;

getFieldInitialValue: (path?: FormPathPattern) => any;
disableUnmountClearStates: (pattern?: FormPathPattern) => void;
enableUnmountClearStates: (pattern?: FormPathPattern) => void;
isHostRendering: () => boolean;

@@ -281,0 +287,0 @@ hostUpdate: (callback?: () => any) => any;

65

lib/externals.js

@@ -55,18 +55,18 @@ "use strict";

exports.createFormExternals = function (internals) {
var options = internals.options, init = internals.init, env = internals.env, form = internals.form, heart = internals.heart, graph = internals.graph, validator = internals.validator, hostUpdate = internals.hostUpdate, afterUnmount = internals.afterUnmount, nextTick = internals.nextTick, isHostRendering = internals.isHostRendering, getDataPath = internals.getDataPath, getFormValuesIn = internals.getFormValuesIn, getFormInitialValuesIn = internals.getFormInitialValuesIn, deleteFormValuesIn = internals.deleteFormValuesIn, setFormValuesIn = internals.setFormValuesIn, setFormInitialValuesIn = internals.setFormInitialValuesIn, updateRecoverableShownState = internals.updateRecoverableShownState, resetFormMessages = internals.resetFormMessages, syncFormMessages = internals.syncFormMessages, batchRunTaskQueue = internals.batchRunTaskQueue, pushTaskQueue = internals.pushTaskQueue;
var options = internals.options, init = internals.init, env = internals.env, form = internals.form, heart = internals.heart, graph = internals.graph, validator = internals.validator, hostUpdate = internals.hostUpdate, afterUnmount = internals.afterUnmount, nextTick = internals.nextTick, isHostRendering = internals.isHostRendering, getDataPath = internals.getDataPath, getFormValuesIn = internals.getFormValuesIn, getFormInitialValuesIn = internals.getFormInitialValuesIn, deleteFormValuesIn = internals.deleteFormValuesIn, setFormValuesIn = internals.setFormValuesIn, setFormInitialValuesIn = internals.setFormInitialValuesIn, updateRecoverableShownState = internals.updateRecoverableShownState, supportUnmountClearStates = internals.supportUnmountClearStates, disableUnmountClearStates = internals.disableUnmountClearStates, enableUnmountClearStates = internals.enableUnmountClearStates, resetFormMessages = internals.resetFormMessages, syncFormMessages = internals.syncFormMessages, batchRunTaskQueue = internals.batchRunTaskQueue, pushTaskQueue = internals.pushTaskQueue;
function eachArrayExchanges(prevState, currentState, eacher) {
var _a, _b;
var _a, _b, _c, _d, _e, _f;
var exchanged = {};
(_b = (_a = currentState.value) === null || _a === void 0 ? void 0 : _a.forEach) === null || _b === void 0 ? void 0 : _b.call(_a, function (item, index) {
var _a, _b, _c, _d;
var prev = (_b = (_a = prevState.value) === null || _a === void 0 ? void 0 : _a[index]) === null || _b === void 0 ? void 0 : _b[field_1.ARRAY_UNIQUE_TAG];
for (var index = ((_a = currentState === null || currentState === void 0 ? void 0 : currentState.value) === null || _a === void 0 ? void 0 : _a.length) - 1; index >= 0; index--) {
var item = (_b = currentState === null || currentState === void 0 ? void 0 : currentState.value) === null || _b === void 0 ? void 0 : _b[index];
var prev = (_d = (_c = prevState.value) === null || _c === void 0 ? void 0 : _c[index]) === null || _d === void 0 ? void 0 : _d[field_1.ARRAY_UNIQUE_TAG];
var current = item === null || item === void 0 ? void 0 : item[field_1.ARRAY_UNIQUE_TAG];
if (prev === current)
return;
continue;
if (prev === undefined || current === undefined) {
return;
continue;
}
if (((_c = currentState.value) === null || _c === void 0 ? void 0 : _c.length) === ((_d = prevState.value) === null || _d === void 0 ? void 0 : _d.length)) {
if (((_e = currentState.value) === null || _e === void 0 ? void 0 : _e.length) === ((_f = prevState.value) === null || _f === void 0 ? void 0 : _f.length)) {
if (exchanged[prev] || exchanged[current])
return;
continue;
exchanged[prev] = true;

@@ -76,3 +76,3 @@ exchanged[current] = true;

eacher(prev, current);
});
}
}

@@ -223,9 +223,14 @@ function calculateMovePath(name, replace) {

}
if (dirtys.unmounted ||
dirtys.visible ||
dirtys.display ||
dirtys.editable) {
resetFormMessages(published);
if (dirtys.visible || dirtys.display || dirtys.editable) {
if (dirtys.unmounted) {
if (supportUnmountClearStates(published.path)) {
resetFormMessages(published);
}
}
else {
resetFormMessages(published);
}
}
heart.publish(types_1.LifeCycleTypes.ON_FIELD_CHANGE, field);
return !env.hostRendering;
};

@@ -262,2 +267,3 @@ }

heart.publish(types_1.LifeCycleTypes.ON_FIELD_CHANGE, field);
return !env.hostRendering;
};

@@ -276,3 +282,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, dataType = _a.dataType, unmountRemoveValue = _a.unmountRemoveValue, 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, props = _a.props;
var field;

@@ -290,4 +296,6 @@ var nodePath = shared_1.FormPath.parse(path || name);

},
needRemoveValue: function (path) {
supportUnmountClearStates: function (path) {
var _a;
if (!supportUnmountClearStates(path))
return false;
if (!((_a = env.realRemoveTags) === null || _a === void 0 ? void 0 : _a.length))

@@ -299,2 +307,5 @@ return true;

},
getEditable: function () {
return form.getState(function (state) { return state.editable; });
},
setValue: function (name, value) {

@@ -332,5 +343,2 @@ setFormValuesIn(name, value);

var syncInitialValue = pickNotEmpty(initialValue, formInitialValue);
if (shared_1.isValid(unmountRemoveValue)) {
state.unmountRemoveValue = unmountRemoveValue;
}
if (shared_1.isValid(syncInitialValue)) {

@@ -846,3 +854,9 @@ state.initialValue = syncInitialValue;

insert: function (index, value) {
var arr = shared_1.toArr(getValue()).reduce(function (buf, item, idx) {
var origin = shared_1.toArr(getValue());
if (origin.length === 0) {
var arr_1 = [value];
setValue(arr_1);
return arr_1;
}
var arr = origin.reduce(function (buf, item, idx) {
return idx === index ? buf.concat([value, item]) : buf.concat(item);

@@ -877,5 +891,6 @@ }, []);

var arr = shared_1.toArr(getValue()).slice();
var item = arr[$from];
arr.splice($from, 1);
arr.splice($to, 0, item);
var fromItem = arr[$from];
var toItem = arr[$to];
arr[$from] = toItem;
arr[$to] = fromItem;
setValue(arr);

@@ -932,2 +947,4 @@ return arr;

getFieldInitialValue: getFieldInitialValue,
disableUnmountClearStates: disableUnmountClearStates,
enableUnmountClearStates: enableUnmountClearStates,
isHostRendering: isHostRendering,

@@ -934,0 +951,0 @@ hostUpdate: hostUpdate,

@@ -28,3 +28,3 @@ import { IFormCreatorOptions } from './types';

getFieldState: (path: FormPathPattern, callback?: (state: import("./types").IFieldState<FormilyCore.FieldProps>) => any) => any;
registerField: ({ path, name, value, initialValue, required, rules, editable, visible, display, computeState, dataType, unmountRemoveValue, props }: import("./types").IFieldRegistryProps<FormilyCore.FieldProps>) => {
registerField: ({ path, name, value, initialValue, required, rules, editable, visible, display, computeState, dataType, props }: import("./types").IFieldRegistryProps<FormilyCore.FieldProps>) => {
factory: import("./types").IModelSpec<Partial<import("./types").IFieldState<any>>, import("./types").IFieldStateProps>;

@@ -135,2 +135,4 @@ state: import("./types").IFieldState<any>;

getFieldInitialValue: (path?: FormPathPattern) => any;
disableUnmountClearStates: (pattern?: FormPathPattern) => void;
enableUnmountClearStates: (pattern?: FormPathPattern) => void;
isHostRendering: () => boolean;

@@ -137,0 +139,0 @@ hostUpdate: (callback?: () => any) => any;

@@ -65,2 +65,3 @@ import { IFormCreatorOptions } from './types';

lastShownStates: {};
clearStatesPatterns: {};
submittingTask: any;

@@ -78,2 +79,5 @@ };

updateRecoverableShownState: (parentState: IVirtualFieldState<FormilyCore.VirtualFieldProps> | IFieldState<FormilyCore.FieldProps>, childState: IVirtualFieldState<FormilyCore.VirtualFieldProps> | IFieldState<FormilyCore.FieldProps>, name: "visible" | "display") => void;
disableUnmountClearStates: (pattern?: FormPathPattern) => void;
enableUnmountClearStates: (pattern?: FormPathPattern) => void;
supportUnmountClearStates: (path: FormPathPattern) => any;
resetFormMessages: (fieldState: IFieldState<any>) => void;

@@ -80,0 +84,0 @@ syncFormMessages: (type: string, fieldState: IFieldState<any>) => void;

@@ -30,13 +30,2 @@ "use strict";

}
if (dirtys.editable) {
hostUpdate(function () {
graph.eachChildren(function (field) {
if (types_1.isField(field)) {
field.setState(function (state) {
state.formEditable = published.editable;
});
}
});
});
}
if (dirtys.unmounted && published.unmounted) {

@@ -53,3 +42,4 @@ heart.publish(types_1.LifeCycleTypes.ON_FORM_UNMOUNT, form);

if (env.hostRendering) {
env.hostRendering = dirtys.values || dirtys.initialValues;
env.hostRendering =
dirtys.values || dirtys.initialValues || dirtys.editable;
}

@@ -171,5 +161,4 @@ return env.hostRendering;

form.setState(function (state) {
state.errors = state.errors || [];
state.warnings = state.warnings || [];
state.errors = state.errors.reduce(function (buf, item) {
var _a, _b, _c, _d;
var newErrors = (_b = (_a = state.errors) === null || _a === void 0 ? void 0 : _a.reduce) === null || _b === void 0 ? void 0 : _b.call(_a, function (buf, item) {
if (item.path === path) {

@@ -182,3 +171,3 @@ return buf;

}, []);
state.warnings = state.warnings.reduce(function (buf, item) {
var newWarnings = (_d = (_c = state.warnings) === null || _c === void 0 ? void 0 : _c.reduce) === null || _d === void 0 ? void 0 : _d.call(_c, function (buf, item) {
if (item.path === path) {

@@ -191,9 +180,9 @@ return buf;

}, []);
if (state.errors.length) {
state.invalid = true;
state.valid = false;
var errorsChanged = !shared_1.isEqual(state.errors, newErrors);
var warningsChanged = !shared_1.isEqual(state.warnings, newWarnings);
if (warningsChanged) {
state.warnings = newWarnings;
}
else {
state.invalid = false;
state.valid = true;
if (errorsChanged) {
state.errors = newErrors;
}

@@ -207,8 +196,8 @@ });

form.setState(function (state) {
var _a, _b;
var foundField = false;
state[type] = state[type] || [];
state[type] = state[type].reduce(function (buf, item) {
var newMessages = (_b = (_a = state[type]) === null || _a === void 0 ? void 0 : _a.reduce) === null || _b === void 0 ? void 0 : _b.call(_a, function (buf, item) {
if (item.path === path) {
foundField = true;
return messages.length ? buf.concat({ path: path, messages: messages }) : buf;
return messages.length ? buf.concat({ name: name, path: path, messages: messages }) : buf;
}

@@ -219,2 +208,6 @@ else {

}, []);
var messageChanged = !shared_1.isEqual(state[type], newMessages);
if (messageChanged) {
state[type] = newMessages;
}
if (!foundField && messages.length) {

@@ -227,10 +220,2 @@ state[type].push({

}
if (state.errors.length) {
state.invalid = true;
state.valid = false;
}
else {
state.invalid = false;
state.valid = true;
}
});

@@ -265,3 +250,3 @@ }

!env.taskQueue[taskIndex].callbacks.some(function (fn) {
return shared_1.isEqual(fn, callback) ? fn === callback : false;
return fn.toString() === callback.toString() ? fn === callback : false;
})) {

@@ -319,2 +304,21 @@ env.taskQueue[taskIndex].callbacks.push(callback);

}
function disableUnmountClearStates(pattern) {
if (pattern === void 0) { pattern = '*'; }
var path = shared_1.FormPath.parse(pattern);
env.clearStatesPatterns[path.toString()] = false;
}
function enableUnmountClearStates(pattern) {
if (pattern === void 0) { pattern = '*'; }
var path = shared_1.FormPath.parse(pattern);
env.clearStatesPatterns[path.toString()] = true;
}
function supportUnmountClearStates(path) {
for (var pattern in env.clearStatesPatterns) {
var enable = env.clearStatesPatterns[pattern];
if (matchStrategy(pattern, path)) {
return enable;
}
}
return true;
}
var graph = new graph_1.FormGraph({

@@ -342,2 +346,3 @@ matchStrategy: matchStrategy

lastShownStates: {},
clearStatesPatterns: {},
submittingTask: undefined

@@ -367,2 +372,5 @@ };

updateRecoverableShownState: updateRecoverableShownState,
disableUnmountClearStates: disableUnmountClearStates,
enableUnmountClearStates: enableUnmountClearStates,
supportUnmountClearStates: supportUnmountClearStates,
resetFormMessages: resetFormMessages,

@@ -369,0 +377,0 @@ syncFormMessages: syncFormMessages,

@@ -17,17 +17,11 @@ "use strict";

var shared_1 = require("@formily/shared");
var immer_1 = require("immer");
var normalizeMessages = function (messages) { return shared_1.toArr(messages).filter(function (v) { return !!v; }); };
var DEEP_INSPECT_PROPERTY_KEYS = [
'props',
'rules',
'errors',
'warnings',
'effectErrors',
'effectWarnings',
'ruleErrors',
'ruleWarnings'
];
var getOriginalValue = function (value) {
var origin = immer_1.original(value);
return shared_1.isValid(origin) ? origin : value;
var calculateEditable = function (selfEditable, formEditable, name) {
return shared_1.isValid(selfEditable)
? selfEditable
: shared_1.isValid(formEditable)
? shared_1.isFn(formEditable)
? formEditable(name)
: formEditable
: true;
};

@@ -46,3 +40,3 @@ exports.ARRAY_UNIQUE_TAG = Symbol.for('@@__YOU_CAN_NEVER_REMOVE_ARRAY_UNIQUE_TAG__@@');

return (_a = current === null || current === void 0 ? void 0 : current.map) === null || _a === void 0 ? void 0 : _a.call(current, function (item, index) {
if (typeof item === 'object') {
if (shared_1.isPlainObj(item)) {
item[exports.ARRAY_UNIQUE_TAG] = force

@@ -92,3 +86,2 @@ ? name + "." + index

unmounted: false,
unmountRemoveValue: true,
props: {}

@@ -102,2 +95,3 @@ };

var initialValue = _this.getInitialValueFromProps();
var formEditable = _this.getEditableFromProps();
if (_this.isArrayList()) {

@@ -108,3 +102,3 @@ value = _this.tagArrayList(shared_1.toArr(value));

var state = __assign(__assign({}, _this.state), { initialValue: initialValue,
value: value, values: [value].concat(_this.state.values.slice(1)) });
formEditable: formEditable, editable: calculateEditable(_this.state.selfEditable, formEditable, _this.state.name), value: value, values: [value].concat(_this.state.values.slice(1)) });
var compareResults = shared_1.isEqual(_this.state.value, value);

@@ -124,3 +118,12 @@ if (!compareResults && compareResults !== _this.lastCompareResults) {

this.props = props;
this.updates = [];
}
FieldStateFactory.prototype.dirtyCheck = function (path, currentValue, nextValue) {
if (path[0] === 'value') {
if (this.isArrayList()) {
return true;
}
}
return !shared_1.isEqual(currentValue, nextValue);
};
FieldStateFactory.prototype.getValueFromProps = function () {

@@ -133,2 +136,6 @@ var _a;

};
FieldStateFactory.prototype.getEditableFromProps = function () {
var _a, _b;
return (_b = (_a = this.props) === null || _a === void 0 ? void 0 : _a.getEditable) === null || _b === void 0 ? void 0 : _b.call(_a);
};
FieldStateFactory.prototype.getInitialValueFromProps = function () {

@@ -144,11 +151,2 @@ var _a;

};
FieldStateFactory.prototype.dirtyCheck = function (path, value, nextValue) {
var propName = path[0];
if (DEEP_INSPECT_PROPERTY_KEYS.includes(propName)) {
return !shared_1.isEqual(value, nextValue);
}
else {
return value !== nextValue;
}
};
FieldStateFactory.prototype.produceErrorsAndWarnings = function (draft, dirtys) {

@@ -180,12 +178,13 @@ if (dirtys.errors) {

}
draft.editable = shared_1.isValid(draft.selfEditable)
? draft.selfEditable
: shared_1.isValid(draft.formEditable)
? shared_1.isFn(draft.formEditable)
? draft.formEditable(draft.name)
: draft.formEditable
: true;
draft.editable = calculateEditable(draft.selfEditable, draft.formEditable, draft.name);
};
FieldStateFactory.prototype.supportUnmountClearStates = function () {
var _a, _b;
if (shared_1.isFn((_a = this.props) === null || _a === void 0 ? void 0 : _a.supportUnmountClearStates)) {
return (_b = this.props) === null || _b === void 0 ? void 0 : _b.supportUnmountClearStates(this.state.path);
}
return true;
};
FieldStateFactory.prototype.produceSideEffects = function (draft, dirtys) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
var supportClearStates = this.supportUnmountClearStates();
if (dirtys.validating) {

@@ -202,3 +201,3 @@ if (draft.validating === true) {

draft.visible === false ||
draft.unmounted === true) {
(dirtys.unmounted && draft.unmounted === true && supportClearStates)) {
draft.errors = [];

@@ -225,3 +224,3 @@ draft.effectErrors = [];

if (dirtys.visible || dirtys.mounted || dirtys.unmounted) {
if (draft.unmountRemoveValue && ((_b = (_a = this.props) === null || _a === void 0 ? void 0 : _a.needRemoveValue) === null || _b === void 0 ? void 0 : _b.call(_a, this.state.path))) {
if (supportClearStates) {
if (draft.display) {

@@ -239,3 +238,3 @@ if (draft.visible === false || draft.unmounted === true) {

draft.values[0] = undefined;
(_d = (_c = this.props).setValue) === null || _d === void 0 ? void 0 : _d.call(_c, this.state.name, undefined);
this.updates.push('value');
}

@@ -247,3 +246,3 @@ else if (draft.visible === true ||

draft.value = draft.visibleCacheValue;
(_f = (_e = this.props).setValue) === null || _f === void 0 ? void 0 : _f.call(_e, this.state.name, getOriginalValue(draft.value));
this.updates.push('value');
}

@@ -266,3 +265,2 @@ }

draft.values[0] = undefined;
(_h = (_g = this.props).setValue) === null || _h === void 0 ? void 0 : _h.call(_g, this.state.name, undefined);
}

@@ -272,3 +270,2 @@ else if (draft.visible === true) {

draft.value = draft.visibleCacheValue;
(_k = (_j = this.props).setValue) === null || _k === void 0 ? void 0 : _k.call(_j, this.state.name, getOriginalValue(draft.value));
}

@@ -295,3 +292,2 @@ }

FieldStateFactory.prototype.produceValue = function (draft, dirtys) {
var _a, _b, _c, _d;
var valueOrInitialValueChanged = dirtys.values || dirtys.value || dirtys.initialValue;

@@ -325,6 +321,6 @@ var valueChanged = dirtys.values || dirtys.value;

if (valueChanged) {
(_b = (_a = this.props).setValue) === null || _b === void 0 ? void 0 : _b.call(_a, this.state.name, getOriginalValue(draft.value));
this.updates.push('value');
}
if (dirtys.initialValue) {
(_d = (_c = this.props).setInitialValue) === null || _d === void 0 ? void 0 : _d.call(_c, this.state.name, getOriginalValue(draft.initialValue));
this.updates.push('initialValue');
}

@@ -401,2 +397,5 @@ if (valueOrInitialValueChanged) {

};
FieldStateFactory.prototype.beforeProduce = function () {
this.updates = [];
};
FieldStateFactory.prototype.produce = function (draft, dirtys) {

@@ -409,2 +408,14 @@ this.produceErrorsAndWarnings(draft, dirtys);

};
FieldStateFactory.prototype.afterProduce = function () {
var _this = this;
this.updates.forEach(function (type) {
var _a, _b, _c, _d;
if (type === 'value') {
(_b = (_a = _this.props) === null || _a === void 0 ? void 0 : _a.setValue) === null || _b === void 0 ? void 0 : _b.call(_a, _this.state.name, _this.state.value);
}
else {
(_d = (_c = _this.props) === null || _c === void 0 ? void 0 : _c.setInitialValue) === null || _d === void 0 ? void 0 : _d.call(_c, _this.state.name, _this.state.initialValue);
}
});
};
return FieldStateFactory;

@@ -411,0 +422,0 @@ }()),

@@ -103,6 +103,6 @@ "use strict";

eacher(node, path);
if (recursion) {
_this.eachChildren(path, selector, eacher, recursion);
}
}
if (recursion) {
_this.eachChildren(path, selector, eacher, recursion);
}
}

@@ -109,0 +109,0 @@ });

@@ -19,3 +19,5 @@ "use strict";

immer_1.enableAllPlugins();
immer_1.setAutoFreeze(false);
var produce = new immer_1.Immer({
autoFreeze: false
}).produce;
var applyPatches = function (target, patches) {

@@ -95,3 +97,3 @@ patches.forEach(function (_a) {

else {
return currentValue !== nextValue;
return !shared_1.isEqual(currentValue, nextValue);
}

@@ -103,2 +105,3 @@ }

if (silent === void 0) { silent = false; }
var _a, _b, _c, _d;
if (!shared_1.isFn(recipe))

@@ -112,3 +115,4 @@ return;

this.factory.state = base;
immer_1.produce(base, function (draft) {
(_b = (_a = this.factory) === null || _a === void 0 ? void 0 : _a.beforeProduce) === null || _b === void 0 ? void 0 : _b.call(_a);
produce(base, function (draft) {
recipe(draft);

@@ -121,3 +125,3 @@ if (shared_1.isFn(_this.props.computeState)) {

});
var produced = immer_1.produce(base, function (draft) {
var produced = produce(base, function (draft) {
applyPatches(draft, _this.patches);

@@ -129,15 +133,9 @@ var dirtys = _this.getDirtys(_this.patches, true);

}, function (patches) {
if (shared_1.isFn(_this.factory.dirtyCheck)) {
patches.forEach(function (patch) {
var path = patch.path, value = patch.value;
if (_this.dirtyCheck(path, value)) {
_this.patches.push(patch);
_this.dirtyCount++;
}
});
}
else {
_this.patches = _this.patches.concat(patches);
_this.dirtyCount += patches.length;
}
patches.forEach(function (patch) {
var path = patch.path, value = patch.value;
if (_this.dirtyCheck(path, value)) {
_this.patches.push(patch);
_this.dirtyCount++;
}
});
});

@@ -148,2 +146,3 @@ this.factory.state = produced;

this.patches = [];
(_d = (_c = this.factory) === null || _c === void 0 ? void 0 : _c.afterProduce) === null || _d === void 0 ? void 0 : _d.call(_c);
if (this.dirtyCount > 0 && !silent) {

@@ -191,3 +190,13 @@ if (this.batching) {

Model.prototype.getCache = function (key) {
return this.cache.get(key);
var value = this.cache.get(key);
if (shared_1.isValid(value))
return value;
if (this.cache.size === 1) {
var findKey_1 = null;
this.cache.forEach(function (value, key) {
findKey_1 = key;
});
return this.cache.get(findKey_1);
}
return value;
};

@@ -194,0 +203,0 @@ Model.prototype.removeCache = function (key) {

@@ -83,2 +83,4 @@ /// <reference types="react" />

getState?: (state?: State) => any;
beforeProduce?: () => void;
afterProduce?: () => void;
dirtyCheck?: (path: FormPathPattern, value: any, nextValue: any) => boolean;

@@ -139,2 +141,3 @@ produce?: (draft: Draft<State>, dirtys: StateDirtyMap<State>) => void;

dataType?: string;
getEditable?: () => boolean | ((name: string) => boolean);
getValue?: (name: FormPathPattern) => any;

@@ -145,3 +148,3 @@ getInitialValue?: (name: FormPathPattern) => any;

setInitialValue?: (name: FormPathPattern, initialValue: any) => void;
needRemoveValue?: (path: FormPathPattern) => boolean;
supportUnmountClearStates?: (path: FormPathPattern) => boolean;
computeState?: (draft: IFieldState, prevState: IFieldState) => void;

@@ -148,0 +151,0 @@ unControlledValueChanged?: () => void;

{
"name": "@formily/core",
"version": "1.2.2",
"version": "1.2.3-beta.0",
"license": "MIT",

@@ -28,4 +28,4 @@ "main": "lib",

"dependencies": {
"@formily/shared": "^1.2.2",
"@formily/validator": "^1.2.2",
"@formily/shared": "^1.2.3-beta.0",
"@formily/validator": "^1.2.3-beta.0",
"immer": "^6.0.3"

@@ -36,3 +36,3 @@ },

},
"gitHead": "5f10da195e878aecce3b030a0ebb1dc177ce19a0"
"gitHead": "b4f0725dc22db31ac55e77c359793403129dc333"
}

@@ -67,2 +67,5 @@ import {

updateRecoverableShownState,
supportUnmountClearStates,
disableUnmountClearStates,
enableUnmountClearStates,
resetFormMessages,

@@ -80,11 +83,12 @@ syncFormMessages,

const exchanged = {}
currentState.value?.forEach?.((item: any, index: number) => {
for (let index = currentState?.value?.length - 1; index >= 0; index--) {
const item = currentState?.value?.[index]
const prev = prevState.value?.[index]?.[ARRAY_UNIQUE_TAG]
const current = item?.[ARRAY_UNIQUE_TAG]
if (prev === current) return
if (prev === current) continue
if (prev === undefined || current === undefined) {
return
continue
}
if (currentState.value?.length === prevState.value?.length) {
if (exchanged[prev] || exchanged[current]) return
if (exchanged[prev] || exchanged[current]) continue
exchanged[prev] = true

@@ -94,3 +98,3 @@ exchanged[current] = true

eacher(prev, current)
})
}
}

@@ -259,13 +263,15 @@

if (
dirtys.unmounted ||
dirtys.visible ||
dirtys.display ||
dirtys.editable
) {
if (dirtys.visible || dirtys.display || dirtys.editable) {
//fix #682
resetFormMessages(published)
if (dirtys.unmounted) {
if (supportUnmountClearStates(published.path)) {
resetFormMessages(published)
}
} else {
resetFormMessages(published)
}
}
heart.publish(LifeCycleTypes.ON_FIELD_CHANGE, field)
return !env.hostRendering
}

@@ -312,2 +318,3 @@ }

heart.publish(LifeCycleTypes.ON_FIELD_CHANGE, field)
return !env.hostRendering
}

@@ -335,3 +342,2 @@ }

dataType,
unmountRemoveValue,
props

@@ -351,3 +357,4 @@ }: IFieldRegistryProps<FormilyCore.FieldProps>) {

},
needRemoveValue(path) {
supportUnmountClearStates(path) {
if (!supportUnmountClearStates(path)) return false
if (!env.realRemoveTags?.length) return true

@@ -358,2 +365,5 @@ return env.realRemoveTags.every(tag => {

},
getEditable() {
return form.getState(state => state.editable)
},
setValue(name, value) {

@@ -392,5 +402,2 @@ setFormValuesIn(name, value)

const syncInitialValue = pickNotEmpty(initialValue, formInitialValue)
if (isValid(unmountRemoveValue)) {
state.unmountRemoveValue = unmountRemoveValue
}

@@ -955,3 +962,9 @@ if (isValid(syncInitialValue)) {

insert(index: number, value: any) {
const arr = toArr(getValue()).reduce((buf, item, idx) => {
const origin = toArr(getValue())
if (origin.length === 0) {
const arr = [value]
setValue(arr)
return arr
}
const arr = origin.reduce((buf, item, idx) => {
return idx === index ? buf.concat([value, item]) : buf.concat(item)

@@ -988,5 +1001,6 @@ }, [])

const arr = toArr(getValue()).slice()
const item = arr[$from]
arr.splice($from, 1)
arr.splice($to, 0, item)
const fromItem = arr[$from]
const toItem = arr[$to]
arr[$from] = toItem
arr[$to] = fromItem
setValue(arr)

@@ -1053,2 +1067,4 @@ return arr

getFieldInitialValue,
disableUnmountClearStates,
enableUnmountClearStates,
isHostRendering,

@@ -1055,0 +1071,0 @@ hostUpdate,

@@ -21,4 +21,3 @@ import { IFormCreatorOptions } from './types'

IField,
IVirtualField,
isField
IVirtualField
} from './types'

@@ -36,13 +35,2 @@

}
if (dirtys.editable) {
hostUpdate(() => {
graph.eachChildren((field: IField | IVirtualField) => {
if (isField(field)) {
field.setState(state => {
state.formEditable = published.editable
})
}
})
})
}
if (dirtys.unmounted && published.unmounted) {

@@ -59,3 +47,4 @@ heart.publish(LifeCycleTypes.ON_FORM_UNMOUNT, form)

if (env.hostRendering) {
env.hostRendering = dirtys.values || dirtys.initialValues
env.hostRendering =
dirtys.values || dirtys.initialValues || dirtys.editable
}

@@ -213,5 +202,3 @@ return env.hostRendering

form.setState(state => {
state.errors = state.errors || []
state.warnings = state.warnings || []
state.errors = state.errors.reduce((buf: any, item: any) => {
const newErrors = state.errors?.reduce?.((buf: any, item: any) => {
if (item.path === path) {

@@ -223,3 +210,3 @@ return buf

}, [])
state.warnings = state.warnings.reduce((buf: any, item: any) => {
const newWarnings = state.warnings?.reduce?.((buf: any, item: any) => {
if (item.path === path) {

@@ -231,9 +218,10 @@ return buf

}, [])
if (state.errors.length) {
state.invalid = true
state.valid = false
} else {
state.invalid = false
state.valid = true
const errorsChanged = !isEqual(state.errors, newErrors)
const warningsChanged = !isEqual(state.warnings, newWarnings)
if (warningsChanged) {
state.warnings = newWarnings
}
if (errorsChanged) {
state.errors = newErrors
}
})

@@ -248,7 +236,6 @@ }

let foundField = false
state[type] = state[type] || []
state[type] = state[type].reduce((buf: any, item: any) => {
const newMessages = state[type]?.reduce?.((buf: any, item: any) => {
if (item.path === path) {
foundField = true
return messages.length ? buf.concat({ path, messages }) : buf
return messages.length ? buf.concat({ name, path, messages }) : buf
} else {

@@ -258,2 +245,6 @@ return buf.concat(item)

}, [])
const messageChanged = !isEqual(state[type], newMessages)
if (messageChanged) {
state[type] = newMessages
}
if (!foundField && messages.length) {

@@ -266,9 +257,2 @@ state[type].push({

}
if (state.errors.length) {
state.invalid = true
state.valid = false
} else {
state.invalid = false
state.valid = true
}
})

@@ -307,4 +291,4 @@ }

env.taskQueue[taskIndex] &&
!env.taskQueue[taskIndex].callbacks.some(fn =>
isEqual(fn, callback) ? fn === callback : false
!env.taskQueue[taskIndex].callbacks.some((fn: any) =>
fn.toString() === callback.toString() ? fn === callback : false
)

@@ -368,2 +352,22 @@ ) {

function disableUnmountClearStates(pattern: FormPathPattern = '*') {
const path = FormPath.parse(pattern)
env.clearStatesPatterns[path.toString()] = false
}
function enableUnmountClearStates(pattern: FormPathPattern = '*') {
const path = FormPath.parse(pattern)
env.clearStatesPatterns[path.toString()] = true
}
function supportUnmountClearStates(path: FormPathPattern) {
for (const pattern in env.clearStatesPatterns) {
const enable = env.clearStatesPatterns[pattern]
if (matchStrategy(pattern, path)) {
return enable
}
}
return true
}
const graph = new FormGraph({

@@ -398,2 +402,3 @@ matchStrategy

lastShownStates: {},
clearStatesPatterns: {},
submittingTask: undefined

@@ -423,2 +428,5 @@ }

updateRecoverableShownState,
disableUnmountClearStates,
enableUnmountClearStates,
supportUnmountClearStates,
resetFormMessages,

@@ -425,0 +433,0 @@ syncFormMessages,

@@ -15,22 +15,21 @@ import { createModel } from '../shared/model'

isEmpty,
isArr
isArr,
isPlainObj
} from '@formily/shared'
import { Draft, original } from 'immer'
import { Draft } from 'immer'
const normalizeMessages = (messages: any) => toArr(messages).filter(v => !!v)
const DEEP_INSPECT_PROPERTY_KEYS = [
'props',
'rules',
'errors',
'warnings',
'effectErrors',
'effectWarnings',
'ruleErrors',
'ruleWarnings'
]
const getOriginalValue = (value: any) => {
const origin = original(value)
return isValid(origin) ? origin : value
const calculateEditable = (
selfEditable: boolean,
formEditable: boolean | ((name: string) => boolean),
name: string
) => {
return isValid(selfEditable)
? selfEditable
: isValid(formEditable)
? isFn(formEditable)
? formEditable(name)
: formEditable
: true
}

@@ -51,3 +50,3 @@

return current?.map?.((item, index) => {
if (typeof item === 'object') {
if (isPlainObj(item)) {
item[ARRAY_UNIQUE_TAG] = force

@@ -71,2 +70,4 @@ ? `${name}.${index}`

updates: Array<'value' | 'initialValue'>
lastCompareResults?: boolean

@@ -107,3 +108,2 @@

unmounted: false,
unmountRemoveValue: true,
props: {}

@@ -119,4 +119,16 @@ }

this.props = props
this.updates = []
}
dirtyCheck(path: string[], currentValue: any, nextValue: any) {
if (path[0] === 'value') {
if (this.isArrayList()) {
//如果是ArrayList,不再做精准判断,因为数组内部侵入了Symbol,使用isEqual判断会有问题
return true
}
}
return !isEqual(currentValue, nextValue)
}
getValueFromProps() {

@@ -129,2 +141,6 @@ if (isFn(this.props?.getValue)) {

getEditableFromProps() {
return this.props?.getEditable?.()
}
getInitialValueFromProps() {

@@ -140,11 +156,2 @@ if (isFn(this.props?.getInitialValue)) {

dirtyCheck(path: string[], value: any, nextValue: any) {
const propName = path[0]
if (DEEP_INSPECT_PROPERTY_KEYS.includes(propName)) {
return !isEqual(value, nextValue)
} else {
return value !== nextValue
}
}
getState = () => {

@@ -154,2 +161,3 @@ if (!this.state.initialized) return this.state

let initialValue = this.getInitialValueFromProps()
let formEditable = this.getEditableFromProps()
if (this.isArrayList()) {

@@ -163,2 +171,8 @@ value = this.tagArrayList(toArr(value))

initialValue,
formEditable,
editable: calculateEditable(
this.state.selfEditable,
formEditable,
this.state.name
),
value,

@@ -206,12 +220,18 @@ values: [value].concat(this.state.values.slice(1))

}
draft.editable = isValid(draft.selfEditable)
? draft.selfEditable
: isValid(draft.formEditable)
? isFn(draft.formEditable)
? draft.formEditable(draft.name)
: draft.formEditable
: true
draft.editable = calculateEditable(
draft.selfEditable,
draft.formEditable,
draft.name
)
}
supportUnmountClearStates() {
if (isFn(this.props?.supportUnmountClearStates)) {
return this.props?.supportUnmountClearStates(this.state.path)
}
return true
}
produceSideEffects(draft: Draft<IFieldState>, dirtys: FieldStateDirtyMap) {
const supportClearStates = this.supportUnmountClearStates()
if (dirtys.validating) {

@@ -228,3 +248,3 @@ if (draft.validating === true) {

draft.visible === false ||
draft.unmounted === true
(dirtys.unmounted && draft.unmounted === true && supportClearStates)
) {

@@ -252,6 +272,3 @@ draft.errors = []

if (dirtys.visible || dirtys.mounted || dirtys.unmounted) {
if (
draft.unmountRemoveValue &&
this.props?.needRemoveValue?.(this.state.path)
) {
if (supportClearStates) {
if (draft.display) {

@@ -269,3 +286,3 @@ if (draft.visible === false || draft.unmounted === true) {

draft.values[0] = undefined
this.props.setValue?.(this.state.name, undefined)
this.updates.push('value')
} else if (

@@ -278,6 +295,3 @@ draft.visible === true ||

draft.value = draft.visibleCacheValue
this.props.setValue?.(
this.state.name,
getOriginalValue(draft.value)
)
this.updates.push('value')
}

@@ -299,10 +313,5 @@ }

draft.values[0] = undefined
this.props.setValue?.(this.state.name, undefined)
} else if (draft.visible === true) {
if (!isValid(draft.value)) {
draft.value = draft.visibleCacheValue
this.props.setValue?.(
this.state.name,
getOriginalValue(draft.value)
)
}

@@ -361,9 +370,6 @@ }

if (valueChanged) {
this.props.setValue?.(this.state.name, getOriginalValue(draft.value))
this.updates.push('value')
}
if (dirtys.initialValue) {
this.props.setInitialValue?.(
this.state.name,
getOriginalValue(draft.initialValue)
)
this.updates.push('initialValue')
}

@@ -457,2 +463,6 @@ if (valueOrInitialValueChanged) {

beforeProduce() {
this.updates = []
}
produce(draft: Draft<IFieldState>, dirtys: FieldStateDirtyMap) {

@@ -466,2 +476,16 @@ this.produceErrorsAndWarnings(draft, dirtys)

afterProduce() {
//Because the draft data cannot be consumed externally, I can only cache the changes and handle it uniformly
this.updates.forEach(type => {
if (type === 'value') {
this.props?.setValue?.(this.state.name, this.state.value)
} else {
this.props?.setInitialValue?.(
this.state.name,
this.state.initialValue
)
}
})
}
static defaultProps = {

@@ -468,0 +492,0 @@ path: '',

@@ -159,6 +159,6 @@ import {

eacher(node, path)
if (recursion) {
this.eachChildren(path, selector, eacher, recursion)
}
}
if (recursion) {
this.eachChildren(path, selector, eacher, recursion)
}
}

@@ -165,0 +165,0 @@ })

@@ -9,10 +9,14 @@ import {

shallowClone,
isStr
isStr,
isValid
} from '@formily/shared'
import { produce, enableAllPlugins, setAutoFreeze, Draft } from 'immer'
import { Immer, enableAllPlugins, Draft } from 'immer'
import { StateDirtyMap, IDirtyModelFactory, NormalRecord } from '../types'
enableAllPlugins()
setAutoFreeze(false)
const { produce } = new Immer({
autoFreeze: false
})
type Recipe<State> = (state?: State) => any

@@ -110,3 +114,3 @@

} else {
return currentValue !== nextValue
return !isEqual(currentValue,nextValue)
}

@@ -124,2 +128,3 @@ }

this.factory.state = base
this.factory?.beforeProduce?.()
produce(

@@ -147,14 +152,9 @@ base,

patches => {
if (isFn(this.factory.dirtyCheck)) {
patches.forEach(patch => {
const { path, value } = patch
if (this.dirtyCheck(path, value)) {
this.patches.push(patch)
this.dirtyCount++
}
})
} else {
this.patches = this.patches.concat(patches)
this.dirtyCount += patches.length
}
patches.forEach(patch => {
const { path, value } = patch
if (this.dirtyCheck(path, value)) {
this.patches.push(patch)
this.dirtyCount++
}
})
}

@@ -166,2 +166,3 @@ )

this.patches = []
this.factory?.afterProduce?.()
if (this.dirtyCount > 0 && !silent) {

@@ -219,3 +220,12 @@ if (this.batching) {

getCache(key: CacheKey) {
return this.cache.get(key)
const value = this.cache.get(key)
if (isValid(value)) return value
if (this.cache.size === 1) {
let findKey = null
this.cache.forEach((value, key) => {
findKey = key
})
return this.cache.get(findKey)
}
return value
}

@@ -222,0 +232,0 @@

@@ -122,2 +122,4 @@ import { FormPath, FormPathPattern, isFn } from '@formily/shared'

getState?: (state?: State) => any
beforeProduce?: () => void
afterProduce?: () => void
dirtyCheck?: (path: FormPathPattern, value: any, nextValue: any) => boolean

@@ -194,2 +196,3 @@ produce?: (draft: Draft<State>, dirtys: StateDirtyMap<State>) => void

dataType?: string
getEditable?: () => boolean | ((name: string) => boolean)
getValue?: (name: FormPathPattern) => any

@@ -200,3 +203,3 @@ getInitialValue?: (name: FormPathPattern) => any

setInitialValue?: (name: FormPathPattern, initialValue: any) => void
needRemoveValue?: (path: FormPathPattern) => boolean
supportUnmountClearStates?: (path: FormPathPattern) => boolean
computeState?: (draft: IFieldState, prevState: IFieldState) => void

@@ -203,0 +206,0 @@ unControlledValueChanged?: () => void

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc