Skip to content

Commit 5421283

Browse files
committed
Fix set action attrs on reference field
Always set reference field (o2o/m2o) attributes with it's field name prefix in parent form state. Fixes #1304
1 parent 6650b05 commit 5421283

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

axelor-front/src/views/form/builder/form-editors.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -883,8 +883,9 @@ const RecordEditor = memo(function RecordEditor({
883883
model,
884884
fields,
885885
view: editor,
886+
schema,
886887
}),
887-
[editor, fields, model],
888+
[schema, editor, fields, model],
888889
);
889890

890891
const editorFormAtom = useMemo(

axelor-front/src/views/form/builder/scope.ts

+23-8
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
RecordListener,
3939
WidgetErrors,
4040
} from "./types";
41-
import { nextId } from "./utils";
41+
import { isReferenceField, nextId } from "./utils";
4242

4343
type ContextCreator = () => DataContext;
4444

@@ -431,9 +431,17 @@ function useActionAttrs({
431431
useCallback(
432432
(get, set, { attrs }) => {
433433
const formState = get(formAtom);
434-
const isJsonScope = formState.meta.view?.json;
435-
const updateFormAtom = isJsonScope ? formState.parent! : formAtom;
436-
const updateFormState = isJsonScope ? get(updateFormAtom) : formState;
434+
const { meta } = formState;
435+
436+
const schema = (meta as any).schema as Schema;
437+
const parentFormAtom = formState.parent!;
438+
const isJsonScope = meta.view?.json;
439+
const isRefScope =
440+
parentFormAtom && schema?.name && isReferenceField(schema);
441+
442+
const updateFormAtom =
443+
isJsonScope || isRefScope ? parentFormAtom : formAtom;
444+
const updateFormState = get(updateFormAtom);
437445

438446
let { statesByName } = updateFormState;
439447

@@ -448,6 +456,16 @@ function useActionAttrs({
448456
const [target, targetFieldName] = (() => {
449457
const { target } = attr;
450458

459+
const toTargetNames = (refName: string) => {
460+
return target.startsWith(refName!)
461+
? [target, target.slice(refName!.length + 1)]
462+
: [`${refName}.${target}`, target];
463+
};
464+
465+
if (isRefScope) {
466+
return toTargetNames(schema.name!);
467+
}
468+
451469
const isFormField = updateFormState.fields[jsonItem?.name ?? ""];
452470
const isOwnJsonField =
453471
isJsonScope &&
@@ -462,10 +480,7 @@ function useActionAttrs({
462480
target.startsWith(jsonItem.modelField) ||
463481
isOwnJsonField)
464482
) {
465-
const { modelField } = jsonItem;
466-
return target.startsWith(modelField!)
467-
? [target, target.slice(modelField!.length + 1)]
468-
: [`${modelField}.${target}`, target];
483+
return toTargetNames(jsonItem.modelField);
469484
}
470485
return [target, target];
471486
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
title: Fix set action attrs on reference field
3+
type: fix

0 commit comments

Comments
 (0)