Skip to content

Commit

Permalink
fix: enhance override values with expression
Browse files Browse the repository at this point in the history
  • Loading branch information
Adi Fatkhurozi committed Aug 3, 2023
1 parent 71a695e commit 55e1996
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
23 changes: 14 additions & 9 deletions src/v2/logic/createForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,16 +403,21 @@ const createForm = <TSchema>(props: ICreateFormProps<TSchema>) => {
) => {
if (!schema.overrides) return;

for (const { condition = true, expression, values } of schema.overrides) {
if (!expression) {
setValues(cloneDeep(values), { skipNotify: true });
break;
}

for (const { condition = true, expression, values, valuesExpression } of schema.overrides) {
try {
const result = parse(expression, { ...options.extraData }, schema.version);
if (condition === !!result) {
setValues(cloneDeep(values), { skipNotify: true });
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),
);
}
}
break;
}
} catch (error) {
Expand Down
7 changes: 2 additions & 5 deletions src/v2/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ import { IDefaultProp, IExpressionString, IProp, ISchemaCore } from "./core";

// Field ===========================

export interface IOverrideSchema extends ISchemaCore {
comopnent: string;
}

export interface IOverrideField {
condition?: boolean;
expression?: IExpressionString;
values: any;
values?: any;
valuesExpression?: Record<string, string>;
}

export type IRule = {
Expand Down

0 comments on commit 55e1996

Please sign in to comment.