Skip to content

Commit

Permalink
fix: fix override track changes
Browse files Browse the repository at this point in the history
  • Loading branch information
adyfk committed Aug 9, 2023
1 parent 55e1996 commit 515bd80
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/v2/logic/createForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface IState {
fieldsRef: Record<string, any>;
values: Record<string, any>;
error: Record<string, any>;
hasChangedByOverride: Record<string, any>;
}

export interface ISubject {
Expand Down Expand Up @@ -98,6 +99,7 @@ export const initializeState = {
fieldsRef: {},
error: {},
values: {},
hasChangedByOverride: {},
};

export function getSchemaKey(schema: ISchema, parent?: string) {
Expand Down Expand Up @@ -403,26 +405,33 @@ const createForm = <TSchema>(props: ICreateFormProps<TSchema>) => {
) => {
if (!schema.overrides) return;

function setValuesByOverride(values: Record<string, any>, expression: boolean = false) {
for (const key in values) {
_state.hasChangedByOverride[key] = true;
initValue(
key,
expression
? parse(values[key], { ...options.extraData }, schema.version)
: values[key],
);
}
}

for (const { condition = true, expression, values, valuesExpression } of schema.overrides) {
try {
const skip = !expression;
const result = skip ? true : parse(expression, { ...options.extraData }, schema.version);

if (skip || (condition === !!result)) {
if (values) { setValues(cloneDeep(values), { skipNotify: true }); }
if (valuesExpression) {
for (const key in valuesExpression) {
initValue(
key,
parse(valuesExpression[key], { ...options.extraData }, schema.version),
);
}

if (skip || (condition === !!parse(expression, { ...options.extraData }, schema.version))) {
if (values) {
setValuesByOverride(cloneDeep(values));
} else if (valuesExpression) {
setValuesByOverride(valuesExpression, true);
}
break;
}
} catch (error) {
if (!condition) {
setValues(cloneDeep(values), { skipNotify: true });
setValuesByOverride(cloneDeep(values));
break;
}
}
Expand Down Expand Up @@ -508,7 +517,7 @@ const createForm = <TSchema>(props: ICreateFormProps<TSchema>) => {
if (schema.variant === "FIELD" || schema.variant === "FIELD-ARRAY" || schema.variant === "FIELD-OBJECT") {
if (options.name === key) {
executeEachOverrideExpression(schema, options);
} else if (schema.overrideSelf) {
} else if (schema.overrideSelf && !_state.hasChangedByOverride[options.name!]) {
executeEachOverrideSelfExpression(schema, options);
}
}
Expand Down Expand Up @@ -561,6 +570,7 @@ const createForm = <TSchema>(props: ICreateFormProps<TSchema>) => {

const executeExpression = (name?: string) => {
_state.error = {};
_state.hasChangedByOverride = {};

// eslint-disable-next-line guard-for-in
for (const prop in _state.propsState) {
Expand Down

0 comments on commit 515bd80

Please sign in to comment.