Skip to content

Commit

Permalink
Fix nested field support in template parser
Browse files Browse the repository at this point in the history
Fixes #118
  • Loading branch information
mma-axelor committed Jun 14, 2023
1 parent ca6d6cf commit 481c349
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions axelor-front/src/hooks/use-parser/eval-context.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { get } from "lodash";
import { MouseEvent } from "react";
import get from "lodash/get";
import set from "lodash/set";

import { DataContext, DataRecord } from "@/services/client/data.types";
import { Field } from "@/services/client/meta.types";
import { session } from "@/services/client/session";
import format from "@/utils/format";
import { unaccent } from "@/utils/sanitize";
import { i18n } from "@/services/client/i18n";
import { ActionOptions } from "@/view-containers/action";
import { moment } from "@/services/client/l10n";
import { MouseEvent } from "react";
import format from "@/utils/format";

export type EvalContextOptions = {
valid?: (name?: string) => boolean;
Expand All @@ -22,7 +23,8 @@ export type EvalContextOptions = {

export function createEvalContext(
context: DataContext,
options?: EvalContextOptions
options?: EvalContextOptions,
template?: boolean
) {
const {
execute = () => Promise.resolve(),
Expand Down Expand Up @@ -193,11 +195,17 @@ export function createEvalContext(

type EvalContext = DataContext & typeof helpers & typeof filters;

const proxy = new Proxy<EvalContext>(context as any, {
const $context = template
? Object.keys(context).reduce(
(ctx, key) => set(ctx, key, context[key]),
{} as any
)
: context;
const proxy = new Proxy<EvalContext>($context, {
get(target, p, receiver) {
if (p in helpers) return helpers[p as keyof typeof helpers];
if (p in filters) return filters[p as keyof typeof filters];
const value = Reflect.get(target, p, receiver);
const value = Reflect.get(target, p, receiver) ?? get(target, p);
if (p === "id" && value && value <= 0) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion axelor-front/src/hooks/use-parser/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function useTemplate(template: string) {
return useMemo(() => {
const Comp = processTemplate(template);
return (props: { context: DataContext; options?: EvalContextOptions }) => {
const context = createEvalContext(props.context, props.options);
const context = createEvalContext(props.context, props.options, true);
return createElement(Comp, { context });
};
}, [template]);
Expand Down

0 comments on commit 481c349

Please sign in to comment.