Skip to content

Commit 81a0096

Browse files
committed
fix(fields): a record form names a { $record } hop's object through the spec's referenceTargetOf
A hop through a user field carries no reference: its target is fixed by the type. The stamp now asks the same arbiter the object linter's $record rule asks, so such a hop resolves. The census ledger row names what holds the runtime-only invariant and where the loose form-field shape is tracked. Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ADHkmTKRu1t68jcJGgRYi3
1 parent 5eb477a commit 81a0096

5 files changed

Lines changed: 97 additions & 8 deletions

File tree

‎ui/examples/schema-catalog/test/form-field-channel-census-12130.test.tsx‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,10 @@ const LEDGER: Readonly<Record<string, LedgerRow>> = {
335335
readers: ['entry@fields/src/widgets/lookupRecordReference.ts'],
336336
why: 'RUNTIME-ONLY (objectstack#34197) — stamped by `ObjectForm` and `buildSectionFields` (`withRecordBeingEdited`) on '
337337
+ 'a lookup whose `lookupFilters` read `{ $record }`: the record form\'s statement that one record is being edited, '
338-
+ 'with the object each hop points at. A host that edits no single record must not be able to claim it, so no '
339-
+ 'document authors it.',
338+
+ 'with the object each hop points at. A host that edits no single record must not be able to claim it. The '
339+
+ 'invariant is held by the two builders and by the closed spec shapes (the published `customFields` member and '
340+
+ 'action params refuse the key); this face\'s loose `FormFieldSchema`, which can author it on an inline form '
341+
+ 'field, is tracked by objectstack#34344.',
340342
},
341343
homeAmountSource: {
342344
readers: [CURRENCY],

‎ui/packages/fields/src/index.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5382,7 +5382,8 @@ export { PERMISSION_SET_GRANT_COLUMNS } from './widgets/permissionSetHolders.js'
53825382
// `permission-ref` field's definition, so the widget recognises the grant's
53835383
// name column by its object and its column, not by its column name alone.
53845384
export { FORM_OBJECT_KEY, withFormObject } from './widgets/permissionSetHolders.js';
5385-
// A lookup whose `lookupFilters` read a `{ $record }` hop carries the object each hop points at.
5385+
// A record form stamps any lookup whose `lookupFilters` read `{ $record }`: one record is being edited here,
5386+
// with the object each hop points at. Only a field carrying the stamp resolves a `{ $record }`.
53865387
export { RECORD_BEING_EDITED_KEY, withRecordBeingEdited } from './widgets/lookupRecordReference.js';
53875388
// New widgets according to @objectstack/spec
53885389
export * from './widgets/ColorField.js';

‎ui/packages/fields/src/widgets/LookupField.tsx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
type LookupFilterEntry,
2424
} from './lookupRecordReference.js';
2525
import type { RecordPickerFilterColumn } from './RecordPickerDialog.js';
26-
2726
import { PeoplePicker } from './PeoplePicker.js';
2827
import { useRecordQuery } from './useRecordQuery.js';
2928
import { objectResolvesSearch } from './searchOffer.js';

‎ui/packages/fields/src/widgets/lookupRecordReference.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
* - The picker resolves a reference only where its host SAYS a single record
2020
* is being edited: a record form stamps that on the field it hands the
2121
* widget (`withRecordBeingEdited`). Every other host — a bulk action over
22-
* selected rows, an action's parameter dialog, a filter panel — leaves every
23-
* `{ $record }` entry out, whatever values it passes the widget: the values
22+
* selected rows, an action's parameter dialog, inline edit on a record
23+
* page — leaves every `{ $record }` entry out, whatever values it passes the widget: the values
2424
* such a host passes are not the record the author's path names.
2525
* - An entry whose referenced value is empty — null, unset, `''`, an empty
2626
* list, a hop whose lookup is unset — is left out of the query, the rule an
@@ -32,6 +32,7 @@
3232
*/
3333
import { useContext, useEffect, useMemo, useRef, useState } from 'react';
3434
import { SchemaRendererContext } from '@object-ui/react';
35+
import { referenceTargetOf } from '@objectstack/spec/data';
3536
import type { Field as SpecField, LookupFilterRecordReference } from '@objectstack/spec/data';
3637

3738
/** One authored `lookupFilters` entry, typed by reference to the spec row. */
@@ -120,6 +121,11 @@ function referencedId(v: unknown): string | undefined {
120121
* the object each `{ $record }` hop's lookup references (read off `fields`,
121122
* the edited object's field map). `def` itself when its filters read no
122123
* record. Only a host that edits one record calls this.
124+
*
125+
* A hop's object is the spec's `referenceTargetOf` — the one arbiter the
126+
* `$record` path rule of the object linter also asks — so a hop through a
127+
* field whose target the type fixes (`user` → `sys_user`, with no
128+
* `reference` written) is named like one through a `lookup`.
123129
*/
124130
export function withRecordBeingEdited<T extends object>(
125131
def: T,
@@ -129,8 +135,8 @@ export function withRecordBeingEdited<T extends object>(
129135
if (!readsRecord(filters)) return def;
130136
const hopObjects: Record<string, string> = {};
131137
for (const { via } of recordReferenceHops(filters)) {
132-
const reference = (fields?.[via] as { reference?: unknown } | undefined)?.reference;
133-
if (typeof reference === 'string' && reference !== '') hopObjects[via] = reference;
138+
const target = referenceTargetOf(fields?.[via]);
139+
if (target) hopObjects[via] = target;
134140
}
135141
const stamp: RecordBeingEdited = { hopObjects };
136142
return { ...def, [RECORD_BEING_EDITED_KEY]: stamp };

‎ui/packages/plugin-form/src/__tests__/lookupRecordHop-34197.test.tsx‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,3 +110,84 @@ describe('record forms say a record is being edited, and name the object a { $re
110110
expect(priceBookFilters).toEqual([EXPECTED]);
111111
});
112112
});
113+
114+
/**
115+
* A hop through a `user` field: its target is fixed by the type (`sys_user`),
116+
* with no `reference` written, so the form names it through the spec's
117+
* `referenceTargetOf` — the arbiter the object linter's `$record` rule asks.
118+
*/
119+
const TASK = {
120+
name: 'task',
121+
fields: {
122+
subject: { type: 'text', label: 'Subject' },
123+
owner: { type: 'user', label: 'Owner' },
124+
territory: {
125+
type: 'lookup',
126+
label: 'Territory',
127+
reference: 'territory',
128+
lookupFilters: [{ field: 'region', operator: 'eq', value: { $record: 'owner.region' } }],
129+
},
130+
},
131+
};
132+
133+
function userHopDataSource(owner: unknown) {
134+
const territoryFilters: unknown[] = [];
135+
const userReads: unknown[] = [];
136+
const row = { id: 't1', subject: 'T', owner, territory: null };
137+
const ds = {
138+
getObjectSchema: async (name: string) => (name === 'task' ? TASK : { name, fields: { name: { type: 'text' } } }),
139+
findOne: async (object: string, id: string, params: unknown) => {
140+
if (object === 'sys_user') {
141+
userReads.push([id, params]);
142+
return { id: 'u1', name: 'Ann', region: 'emea' };
143+
}
144+
return row;
145+
},
146+
find: async (object: string, params: any) => {
147+
if (object === 'territory') territoryFilters.push(params?.$filter);
148+
return { data: [], total: 0 };
149+
},
150+
create: async () => row,
151+
update: async () => row,
152+
};
153+
return { ds, territoryFilters, userReads };
154+
}
155+
156+
async function openTerritory() {
157+
let trigger: HTMLElement | null = null;
158+
await waitFor(() => {
159+
trigger = document.querySelector('[data-testid="lookup-trigger-territory"]');
160+
if (!trigger) throw new Error('form not ready');
161+
}, { timeout: 5000 });
162+
await act(async () => {
163+
fireEvent.click(trigger!);
164+
});
165+
}
166+
167+
describe('a { $record } hop through a user field resolves in a record form (#34197)', () => {
168+
it('the user held as an id: the form names sys_user, the picker reads the hop and sends its value', async () => {
169+
const { ds, territoryFilters, userReads } = userHopDataSource('u1');
170+
render(
171+
<SchemaRendererProvider dataSource={ds as any}>
172+
<ObjectForm schema={{ type: 'object-form', objectName: 'task', mode: 'edit', recordId: 't1' } as any} dataSource={ds as any} />
173+
</SchemaRendererProvider>,
174+
);
175+
await openTerritory();
176+
await waitFor(() => expect(territoryFilters.length).toBeGreaterThan(0), { timeout: 5000 });
177+
expect(territoryFilters).toEqual([{ region: 'emea' }]);
178+
expect(userReads).toEqual([['u1', { $select: ['id', 'region'] }]]);
179+
});
180+
181+
it('the user held as an expanded row: the value is read off it, with no request', async () => {
182+
const { ds, territoryFilters, userReads } = userHopDataSource({ id: 'u1', name: 'Ann', region: 'apac' });
183+
render(
184+
<SchemaRendererProvider dataSource={ds as any}>
185+
<ObjectForm schema={{ type: 'object-form', objectName: 'task', mode: 'edit', recordId: 't1' } as any} dataSource={ds as any} />
186+
</SchemaRendererProvider>,
187+
);
188+
await openTerritory();
189+
await waitFor(() => expect(territoryFilters.length).toBeGreaterThan(0), { timeout: 5000 });
190+
expect(territoryFilters).toEqual([{ region: 'apac' }]);
191+
expect(userReads).toEqual([]);
192+
});
193+
});

0 commit comments

Comments
 (0)