Skip to content

Commit a8396da

Browse files
fix(app-shell): Lookup and roll-up target pickers take objects only (#37905)
<!-- claude-projects-attribution: {"github_login":"os-tesla"} --> _Requested by **Tesla** · [project thread](https://claude.ai/code/project/chan_01AXsQkLFKRdQCqQ3RTr6Jao?thread=cmsg_01AXsQkLFKRdQCqQ3RTr6Jao5S6gNFvmKHxrqpXWgottKg)_ Fixes #36747 Clause-②: no Metadata-protocol: none ## 维护者速读 - 搭建者在 Studio 字段面板里设置关联字段的目标对象、汇总字段的子对象时,以前随手打的文字也会保存,现在只能从列表里选对象。 - 属于 Studio 搭建体验评审中“元数据写错要当场拒收”这一组,替搭建者挡住打错字就存下一个指向不存在对象的字段。 - 打了不存在的对象名,面板会提示没有选中对象,原来已选的目标保留不动;只存在于草稿里的对象照样能选;可以直接撤回本次改动。 - 无。 ## What changes - `ObjectFieldInspector.tsx`: the Lookup *Related object* door and the roll-up *Child object* door now pass `objectsOnly` to `ObjectPicker`. Neither door accepts an expression. Flow references keep free text (the prop stays off by default). - `ObjectPicker.tsx` (rework round 1): with `objectsOnly`, typed text that names no pickable object is a refusal. The input goes back to the stored value, the "not chosen" line shows, and nothing is committed, so a typo no longer erases a target the field already has. Clearing the input on purpose still commits no object (an empty name), which the doors turn into removal. While the catalog is unread or its read failed, typed text is committed as before. The trigger-object picker in the new-automation dialog, the only other `objectsOnly` caller, starts empty and behaves as before; if it already holds an object, a refused typo now keeps it. **Premise verified (draft-only objects stay pickable).** The picker's catalog is `useObjectCatalog`, which reads `GET /meta/object?preview=draft` (`withPreviewDrafts(true).list('object')`). Server-side, `packages/rest/src/meta-draft-read-builder-gate.test.ts` ("every builder reads the draft overlay ... the draft-only app is listed") pins that the type-generic preview list carries draft-only items for builders. In the picker, a draft entry is an ordinary enabled option (only out-of-closure entries are disabled). The new pin asserts the draft-only object is absent from the published list and is still chosen at both doors. ## Tests New top-level `describe` in the existing harness `ObjectFieldInspector.objectPicker-11783.test.tsx` (ruling record 6583487846), six cases, three per door: - a field that already has a target (`sys_user`): a refused typo leaves it in place, no patch is written, and the refusal is shown; - typed text naming no listed object on an empty field: no target saved, refusal shown; - a listed object that exists only as a draft: chosen and saved. `ObjectPicker.test.tsx`: the objectsOnly pin from objectstack#30038 that asserted a typo clears a chosen object now asserts the object is kept, on Enter and on blur. A new pin asserts that an input cleared on purpose still commits an empty name. Both test files gain a module-top `import '@objectstack/lint'`, as `check:test-source-alias` prescribes for a test file whose source loads it lazily. <details><summary>Readings (head 0009f82)</summary> - `vitest run` (ui root) over ObjectFieldInspector*, ObjectPicker* and the trigger-object dialog's pins (StudioDesignSurface createTaken-26917, flowPresets-11861, newFlowTrigger-11788): Test Files 40 passed (40), Tests 439 passed (439) on d6491f4; final head 0009f82: ObjectPicker.test.tsx + the harness file 25/25 passed. - `@object-ui/app-shell` `type-check` (both `tsconfig.json` and `tsconfig.test.json`): exit 0. - Ablation, round 0: with both `objectsOnly` lines deleted on disk, the two refusal cases go red (2 failed / 5 passed) and the draft-only cases stay green, as they should. - Ablation, round 1: with the old clear-on-refusal branch put back in `commitTyped` on disk, 3 failed / 22 passed: the picker's keep-on-typo pin and both doors' existing-target pins. Restored from HEAD. - `dispatch-gates.mjs --commands`: 38 derived families run, all exit 0 (`check:test-source-alias` went red once per edited test file and was fixed each time by the lint import; the rerun exited 0 on 0009f82). `--ran`: 38 derived, 38 run, 0 UNRUN. </details> ## Acceptance notes - The save door itself does not check that a Lookup or roll-up target exists at authoring time; this PR closes the Studio picker path only. ## Verification Read first: `ObjectPicker.tsx` `commitTyped` (the refusal branch, the riskiest hunk), then `ObjectFieldInspector.tsx` (the two `objectsOnly` lines), then the new `describe` at the end of the harness file. Two-minute check: `cd ui && pnpm exec vitest run packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.objectPicker-11783.test.tsx` — expect 9 passed. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01L9qibzpqH1nQTsbGckLspg --- _Generated by [Claude Code](https://claude.ai/code/session_01L9qibzpqH1nQTsbGckLspg)_ <!-- signed: repo:objectstack#3 · os-tesla · session_01L9qibzpqH1nQTsbGckLspg --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent ad632a2 commit a8396da

5 files changed

Lines changed: 162 additions & 11 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@object-ui/app-shell': patch
3+
---
4+
5+
fix(app-shell): the Lookup's Related object and the roll-up's Child object take objects only
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
In Studio's field inspector, the Lookup's *Related object* and the roll-up summary's *Child object* pickers committed whatever text was typed, so a typo saved a field pointing at an object that does not exist. Neither door accepts an expression, so once the picker's object list has loaded, both commit only an object it lists: typed text that names no listed object is refused, the picker says no object is chosen, and the target the field already had stays (clearing the input on purpose still removes it). While the list is not yet read, or its read failed, the typed text is still committed as before. The trigger object in the new-automation dialog, the one other picker that takes objects only, now also keeps its chosen object on a refused typo. Objects that exist only as a pending draft stay choosable, because the picker lists the draft-overlaid catalog. Flow references keep their free text.

‎ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.objectPicker-11783.test.tsx‎

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import * as React from 'react';
1919
import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest';
2020
import { render, screen, cleanup, waitFor } from '@testing-library/react';
2121
import userEvent from '@testing-library/user-event';
22+
// Paid at collection, not inside a clocked case: the picker's closure and the
23+
// CEL authoring helpers load it lazily (check:test-source-alias).
24+
import '@objectstack/lint';
2225

2326
const h = vi.hoisted(() => {
2427
const NAME = 'repairs_repair_ticket';
@@ -131,3 +134,113 @@ describe('Lookup Related object — resolves the committed name only (objectui#1
131134
expect(refs).toEqual(['sys_user']);
132135
});
133136
});
137+
138+
/**
139+
* objectstack#36747 — the Lookup's *Related object* and the roll-up's *Child
140+
* object* take objects only. No expression is legal at either door, so once
141+
* the catalog has answered, typed text that names no listed object is refused:
142+
* the picker says so, nothing is committed, and a target the field already
143+
* has stays as it was. An object the catalog lists — here one that exists
144+
* only as a pending draft (served by the draft-overlaid list, absent from the
145+
* published one) — is still chosen.
146+
*/
147+
describe('Lookup and roll-up target doors take objects only (objectstack#36747)', () => {
148+
const DOORS = [
149+
{
150+
door: 'Lookup Related object',
151+
label: 'Related object',
152+
field: { type: 'lookup', label: 'Technician' },
153+
withTarget: { type: 'lookup', label: 'Technician', reference: 'sys_user' },
154+
target: (def: Record<string, unknown> | undefined) => def?.reference,
155+
},
156+
{
157+
door: 'roll-up Child object',
158+
label: 'Child object',
159+
field: { type: 'summary', label: 'Total' },
160+
withTarget: { type: 'summary', label: 'Total', summaryOperations: { object: 'sys_user', function: 'count' } },
161+
target: (def: Record<string, unknown> | undefined) =>
162+
(def?.summaryOperations as Record<string, unknown> | undefined)?.object,
163+
},
164+
] as const;
165+
166+
function DoorHarness({ field, onPatch }: { field: Record<string, unknown>; onPatch: (p: Record<string, unknown>) => void }) {
167+
const [draft, setDraft] = React.useState<Record<string, unknown>>({
168+
name: 'repairs_work_order',
169+
fields: { target_field: field },
170+
});
171+
return (
172+
<ObjectFieldInspector
173+
type="object"
174+
name="repairs_work_order"
175+
draft={draft}
176+
selection={{ kind: 'field', id: 'target_field' }}
177+
onPatch={(p) => {
178+
onPatch(p);
179+
setDraft((d) => ({ ...d, ...p }));
180+
}}
181+
onClearSelection={vi.fn()}
182+
onSelectionChange={vi.fn()}
183+
readOnly={false}
184+
locale="en-US"
185+
/>
186+
);
187+
}
188+
189+
/** The input under the door's visible label. */
190+
const doorInput = (label: string): HTMLInputElement =>
191+
screen.getByText(label, { selector: 'label' }).parentElement!.querySelector('input')!;
192+
193+
/** Focus the door and wait until the catalog has answered (its draft-only object is offered). */
194+
async function openLoaded(input: HTMLInputElement): Promise<HTMLElement> {
195+
await userEvent.click(input);
196+
return waitFor(() => {
197+
const o = document.querySelector(`[data-object-name="${h.NAME}"]`);
198+
expect(o).not.toBeNull();
199+
return o as HTMLElement;
200+
});
201+
}
202+
203+
for (const d of DOORS) {
204+
const targetsIn = (onPatch: ReturnType<typeof vi.fn>) =>
205+
onPatch.mock.calls
206+
.map(([p]) => d.target(readFields((p as Record<string, unknown>).fields).entries.find((e) => e.name === 'target_field')?.def))
207+
.filter((v) => v !== undefined);
208+
209+
it(`${d.door}: a refused typo keeps the target the field already has`, async () => {
210+
const onPatch = vi.fn();
211+
render(<DoorHarness field={d.withTarget} onPatch={onPatch} />);
212+
const input = doorInput(d.label);
213+
await openLoaded(input);
214+
await userEvent.clear(input);
215+
await userEvent.type(input, 'no_such_object');
216+
await userEvent.keyboard('{Enter}');
217+
// No patch touches the field: its stored target is not erased.
218+
expect(onPatch).not.toHaveBeenCalled();
219+
expect(input).toHaveValue('sys_user');
220+
expect(document.querySelector('[data-object-refused]')).not.toBeNull();
221+
});
222+
223+
it(`${d.door}: typed text naming no listed object is refused, not saved`, async () => {
224+
const onPatch = vi.fn();
225+
render(<DoorHarness field={d.field} onPatch={onPatch} />);
226+
const input = doorInput(d.label);
227+
await openLoaded(input);
228+
await userEvent.type(input, 'no_such_object');
229+
await userEvent.keyboard('{Enter}');
230+
expect(targetsIn(onPatch)).toEqual([]);
231+
expect(document.querySelector('[data-object-refused]')).not.toBeNull();
232+
});
233+
234+
it(`${d.door}: a listed object that exists only as a draft is chosen`, async () => {
235+
// The premise: the draft-only object is absent from the published list…
236+
expect((await h.client.list('object')).map((o) => o.name)).not.toContain(h.NAME);
237+
const onPatch = vi.fn();
238+
render(<DoorHarness field={d.field} onPatch={onPatch} />);
239+
// …and offered by the picker's catalog all the same.
240+
const option = await openLoaded(doorInput(d.label));
241+
await userEvent.click(option);
242+
expect(targetsIn(onPatch)).toEqual([h.NAME]);
243+
expect(document.querySelector('[data-object-refused]')).toBeNull();
244+
});
245+
}
246+
});

‎ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectFieldInspector.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,7 @@ export function ObjectFieldInspector({
17801780
disabled={locked}
17811781
placeholder={tr('designer.field.objectNamePlaceholder')}
17821782
locale={locale}
1783+
objectsOnly
17831784
/>
17841785
</DisplayPatchLock>
17851786
{heldNeed === 'target' && <HeldHint>{tr('designer.field.hint.pickTarget')}</HeldHint>}
@@ -3447,6 +3448,7 @@ function SummaryConfigFields({
34473448
disabled={readOnly}
34483449
placeholder={tr('designer.field.objectNamePlaceholder')}
34493450
locale={locale}
3451+
objectsOnly
34503452
/>
34513453
<InspectorSelectField
34523454
label={tr('designer.field.summary.function')}

‎ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectPicker.test.tsx‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import * as React from 'react';
1919
import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest';
2020
import { render, screen, cleanup, waitFor } from '@testing-library/react';
2121
import userEvent from '@testing-library/user-event';
22+
// Paid at collection, not inside a clocked case: the picker's closure loads
23+
// it lazily (check:test-source-alias).
24+
import '@objectstack/lint';
2225
import { MemoryRouter, Route, Routes } from 'react-router-dom';
2326

2427
const h = vi.hoisted(() => {
@@ -214,26 +217,36 @@ describe('ObjectPicker — objectsOnly: a door that takes no expression (objects
214217
);
215218
});
216219

217-
it('a typed name that is no object clears a chosen one, on Enter and on blur alike', async () => {
220+
it('a typed name that is no object keeps the chosen one and says it was refused, on Enter and on blur alike', async () => {
218221
const enter = renderPicker({ objectsOnly: true, value: 'showcase_account' });
219222
await openList(enter.input);
220223
await userEvent.clear(enter.input);
221224
await userEvent.type(enter.input, 'crm_custom_eval');
222225
await userEvent.keyboard('{Enter}');
223-
expect(enter.onCommit).toHaveBeenCalledTimes(1);
224-
expect(enter.onCommit).toHaveBeenCalledWith('');
226+
expect(enter.onCommit).not.toHaveBeenCalled();
227+
expect(enter.input).toHaveValue('showcase_account');
228+
expect(refusal()).not.toBeNull();
225229
cleanup();
226230

227231
const blur = renderPicker({ objectsOnly: true, value: 'showcase_account' });
228232
await openList(blur.input);
229233
await userEvent.clear(blur.input);
230234
await userEvent.type(blur.input, 'crm_custom_eval');
231235
await userEvent.tab();
232-
expect(blur.onCommit).toHaveBeenCalledTimes(1);
233-
expect(blur.onCommit).toHaveBeenCalledWith('');
236+
expect(blur.onCommit).not.toHaveBeenCalled();
234237
expect(refusal()).not.toBeNull();
235238
});
236239

240+
it('an input cleared on purpose still commits no object', async () => {
241+
const { input, onCommit } = renderPicker({ objectsOnly: true, value: 'showcase_account' });
242+
await openList(input);
243+
await userEvent.clear(input);
244+
await userEvent.keyboard('{Enter}');
245+
expect(onCommit).toHaveBeenCalledTimes(1);
246+
expect(onCommit).toHaveBeenCalledWith('');
247+
expect(refusal()).toBeNull();
248+
});
249+
237250
it('a name typed out in full is committed as itself', async () => {
238251
const { input, onCommit } = renderPicker({ objectsOnly: true });
239252
await openList(input);

‎ui/packages/app-shell/src/views/metadata-admin/inspectors/ObjectPicker.tsx‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@
2626
* typed, matched or not: a flow reference may hold an expression, and the
2727
* server judges an unknown name as it always did. Escape abandons the typing.
2828
* - **Unless the door takes objects only (`objectsOnly`).** Where no
29-
* expression is legal — the new-automation dialog's trigger object — what
29+
* expression is legal — the new-automation dialog's trigger object, a
30+
* Lookup's *Related object*, a summary's child object — what
3031
* was typed is never committed as itself: Enter and blur commit the object
3132
* it names exactly, else the only pickable match left, else no object at
32-
* all, and the picker says no object is chosen. A typed prefix with one
33-
* match shown is that match, the way a search box reads it.
33+
* all: the picker says no object is chosen and the value already stored
34+
* stays (a typo never erases a chosen target; clearing the input on
35+
* purpose still does). A typed prefix with one match shown is that match,
36+
* the way a search box reads it. While the catalog is unread or its read
37+
* failed nothing can be resolved, and the typed text is committed as is.
3438
* - **The options are the names already held,** from one list read made when
3539
* the picker mounts ({@link useObjectCatalog}), shown with their labels and
3640
* in three groups: this package's objects, then the other objects, then the
@@ -419,7 +423,9 @@ export function ObjectPicker({
419423
/**
420424
* Commit what the author typed. Free text by default; with `objectsOnly`,
421425
* the object it names: the pickable option named exactly so, else the only
422-
* pickable option left, else none — an empty commit and a refusal shown.
426+
* pickable option left. Text that names none is refused: the stored value
427+
* stays, the input shows it again, nothing is committed and the refusal is
428+
* shown. An input cleared on purpose still commits no object (`''`).
423429
*/
424430
const commitTyped = () => {
425431
if (!objectsOnly || catalog.status !== 'loaded') {
@@ -434,8 +440,15 @@ export function ObjectPicker({
434440
}
435441
const pickable = rows.flatMap((r) => (r.kind === 'option' && !r.disabled ? [r.entry.name] : []));
436442
const name = pickable.includes(wanted) ? wanted : pickable.length === 1 ? pickable[0] : null;
437-
setRefused(name === null ? wanted : null);
438-
commit(name ?? '');
443+
if (name === null) {
444+
// A typo must not erase the target already chosen: keep `value`.
445+
setRefused(wanted);
446+
setText(value);
447+
close();
448+
return;
449+
}
450+
setRefused(null);
451+
commit(name);
439452
};
440453

441454
const choose = (row: Row) => {

0 commit comments

Comments
 (0)