Skip to content

Commit 67f8e56

Browse files
committed
fix(plugin-form): a placed row editor is not drawn inside the slot's wrapper form
The expanded row editor is an ObjectForm of its own, so its placement in a group slot draws no wrapper form: a form inside a form is a nesting React refuses. Pinned: the editor's inputs are owned by its own form and no nesting warning is logged. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L6bhgW85kwjGaz7HqsmEuv
1 parent 615d995 commit 67f8e56

2 files changed

Lines changed: 49 additions & 6 deletions

File tree

‎ui/packages/plugin-form/src/MasterDetailForm.groupPlacement-35889.test.tsx‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,43 @@ describe('MasterDetailForm — a group-placed collection sits at its group (obje
150150
expect(submitted).not.toHaveBeenCalled();
151151
});
152152

153+
it('an expanded row editor opens in the slot beside its collection, a form of its own, with no nesting warning', async () => {
154+
const errors = vi.spyOn(console, 'error').mockImplementation(() => {});
155+
const parent = parentWith([HEADER_GROUP, LINES_GROUP, TOTALS_GROUP]);
156+
const child = { name: 'po_line', fields: { qty: { type: 'number', label: 'Qty' }, note: { type: 'text', label: 'Note' }, po: { type: 'master_detail', label: 'PO', reference: 'po' } } };
157+
const ds = makeDataSource(parent, {
158+
getObjectSchema: vi.fn(async (object: string) => (object === 'po_line' ? child : parent)),
159+
findOne: vi.fn().mockResolvedValue({ id: 'po1', ref: 'PO-7', memo: 'm' }),
160+
find: vi.fn().mockImplementation((object: string) =>
161+
Promise.resolve({ data: object === 'po_line' ? [{ id: 'l1', po: 'po1', qty: 3, note: 'n' }] : [] }),
162+
),
163+
});
164+
const withEditor = [{ ...details[0], formFields: ['qty', 'note'] }];
165+
const { container } = render(
166+
<MasterDetailForm schema={{ objectName: 'po', mode: 'edit', recordId: 'po1', details: withEditor } as any} dataSource={ds} />,
167+
);
168+
const expand = await waitFor(() => {
169+
const el = container.querySelector('[data-form-slot] [data-testid="line-items-expand-0"]') as HTMLElement | null;
170+
if (!el) throw new Error('no expand in the placed collection');
171+
return el;
172+
});
173+
fireEvent.click(expand);
174+
const editor = await waitFor(() => {
175+
const el = container.querySelector('[data-testid="md-row-form"]') as HTMLElement | null;
176+
if (!el || !el.querySelector('input')) throw new Error('editor not open');
177+
return el;
178+
});
179+
const header = container.querySelector('[data-form-section="__section_header"]')!.closest('form')!;
180+
expect(editor.closest('[data-form-slot]')).toBe(container.querySelector('[data-form-slot]'));
181+
for (const input of editor.querySelectorAll('input')) {
182+
expect((input as HTMLInputElement).form).not.toBe(header);
183+
expect(editor.contains((input as HTMLInputElement).form)).toBe(true);
184+
}
185+
const nesting = errors.mock.calls.filter((call) => /cannot be a descendant of|cannot contain a nested|validateDOMNesting/i.test(call.map(String).join(' ')));
186+
expect(nesting).toEqual([]);
187+
errors.mockRestore();
188+
});
189+
153190
it('saves the record and its lines in one batch from the placed table', async () => {
154191
const batchTransaction = vi.fn().mockResolvedValue({ results: [{ id: 'po1' }] });
155192
const ds = makeDataSource(parentWith([HEADER_GROUP, LINES_GROUP, TOTALS_GROUP]), { batchTransaction });

‎ui/packages/plugin-form/src/MasterDetailForm.tsx‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -913,18 +913,24 @@ interface MasterDetailLinesProps {
913913
* never blocks Save with a browser bubble), a line button without a `type`
914914
* submits only the wrapper, which does nothing, and Enter in a cell finds the
915915
* wrapper's disabled default button and submits nothing.
916+
*
917+
* `ownsForm` says `children` already are a form of their own (an expanded row
918+
* editor is an `ObjectForm`, its only other control a `type="button"`), so no
919+
* wrapper is drawn: a form inside a form is a nesting React refuses.
916920
*/
917921
const preventSubmit = (event: React.FormEvent) => event.preventDefault();
918922

919-
const FormSlotPlacement: React.FC<{ store: FormSlotStore; id: string; children: React.ReactNode }> = ({
920-
store,
921-
id,
922-
children,
923-
}) => {
923+
const FormSlotPlacement: React.FC<{
924+
store: FormSlotStore;
925+
id: string;
926+
ownsForm?: boolean;
927+
children: React.ReactNode;
928+
}> = ({ store, id, ownsForm = false, children }) => {
924929
useSyncExternalStore(store.subscribe, store.getVersion, store.getVersion);
925930
const at = store.placement(id);
926931
if (at === 'inline') return <>{children}</>;
927932
if (!at) return null;
933+
if (ownsForm) return createPortal(children, at);
928934
return createPortal(
929935
<form noValidate onSubmit={preventSubmit} data-form-slot-owner="">
930936
<button type="submit" disabled hidden aria-hidden="true" tabIndex={-1} />
@@ -2384,7 +2390,7 @@ export const MasterDetailForm: React.FC<MasterDetailFormProps> = ({
23842390
everything on the parent Save. */}
23852391
{expanded && expandedDetail && (
23862392
// Beside its collection: where the header placed that collection, there.
2387-
<FormSlotPlacement store={slotStore} id={expanded.entryId}>
2393+
<FormSlotPlacement store={slotStore} id={expanded.entryId} ownsForm>
23882394
<Card className="border-primary/40 shadow-none ring-1 ring-primary/10" data-testid="md-row-form">
23892395
<CardHeader className="pb-2 flex flex-row items-center justify-between gap-2 space-y-0">
23902396
<CardTitle className="text-sm font-medium">

0 commit comments

Comments
 (0)