Skip to content

Commit 69c3dc4

Browse files
fix(fields): a dependsOn lookup filters by a populated parent's id, not "[object Object]" (#36938)
Fixes #36837 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户在记录详情页(以及编辑弹窗)里点一个依赖上级查找字段的选择器(例如报价单的「联系人」按所属客户过滤)时,原来永远显示「未找到选项」,现在能列出该客户下的联系人。 - 属于表单级联选择(依赖字段过滤)这一核心能力,替产品负责人报告的报价单场景解决。 - 若有误,最坏情况是依赖过滤取错值导致选择器为空,与修复前相同;改动只在一处,可直接撤回。 - 无。 ## What changes A lookup with `dependsOn` naming another lookup builds its candidate filter from the record the host hands it (`dependentValues`). A record page hands the record as the server returned it, so the parent lookup arrives **populated** (`{ id, name, … }`). The one place that turns that record into the filter, `LookupField`'s `dependentFilter` memo, did `String(v)` on it. The request then carried `["crm_account","=","[object Object]"]` and the picker never matched. `dependentFilter` now collapses the parent value through `collapseReference`. That is the rule `lookupRecordReference.ts` already applies to `{ $record }` filter comparands ("an expanded row collapses to its id"), now exported so both candidate-filter readers share it. It is fixed at that one producer. All four candidate surfaces read the same memo, so all four are fixed: the quick-select popover, the Level-2 table picker, the PeoplePicker and the create-new seed (`initialValues` / inline `create`). The fix applies whatever the host is: detail inline edit, header highlight, in-place editor or the record form. A bare-id parent is unchanged. Files: - `ui/packages/fields/src/widgets/LookupField.tsx`: the `dependentFilter` memo collapses each parent value; an import is added. - `ui/packages/fields/src/widgets/lookupRecordReference.ts`: `collapseReference` is exported and its doc names both readers. - `ui/packages/fields/src/widgets/LookupField.dependsOn.test.tsx`: a new top-level `describe` in the existing dependsOn harness. - `.changeset/36837-dependson-lookup-id.md`: `@object-ui/fields` patch. ## Tests - New pin "a populated dependsOn parent scopes by its id (objectstack#36837)": - The populated parent `{ account: { id: 'a1', name: 'Acme' } }` must put `$filter.account === 'a1'` on every `find` call, and the list must show only that account's contact. - Control: the same parent passed as a bare id sends the identical filter. - Red before the fix: `expected '[object Object]' to be 'a1'`, the card's captured request reproduced (1 failed | 10 passed). - Green after the fix: the five dependsOn / record-reference LookupField files pass, 39/39. The whole `ui/packages/fields/` suite passes at e19a6b2: Test Files 326 passed | 1 skipped, Tests 4982 passed | 7 skipped. - Ablation, one run at e19a6b2 through `scripts/ablation-replace.mjs`: - The mutation dropped `collapseReference(...)` from the memo and landed: anchor 1 → 0, blob c7c449f → 71cd8a0. - The new pin went red with `Received: "[object Object]"`. - The restore was verified: the blob equals HEAD's c7c449f and `git diff HEAD` is empty. - The test imports `./LookupField` from source, so no dist rebuild was needed. ## Acceptance notes - The `[object Object]` family the card names is in separate code. The kanban grouping (#36430) and the drill dialog (#36507) live in `plugin-kanban` / `plugin-dashboard`, and neither reads `dependentFilter` or `collapseReference`. They are not touched here. - The gate check (`dependenciesMissing`) already treats a populated object as "present", so it needed no change. - I kept the edit in the picker component to one line plus one import, because #36832 is also reworking the lookup picker. ## Verification First file to read: `LookupField.tsx`, the `dependentFilter` memo. It is the only risky part of the change. Two-minute check: `pnpm --dir ui exec vitest run packages/fields/src/widgets/LookupField.dependsOn.test.tsx` should show 11 passed. **Seat-written open.** Seat `repo:objectstack#4` (os-justin, `session_01QSeKYpEsWWE9Y5YtSsDgqZ`) opened this draft on the maintainer's ruling in the seat session, verbatim 「一选1+」 (2026-11-24): option 1+ — the seat performs only the writes the dev session's permission classifier refused, each checked against the branch; pull requests stay draft; no standing allow rule is added (evidence card #36936). Refused once in dev session `session_01QSeKYq2vSKPUSvT9fXdzEY`, reason `[Undermining Oversight]`: `node scripts/pm/fleet-write/dispatch.mjs --repo objectstack-ai/objectstack --actions-file FILE` carrying this one `pr_create`. Title and body above are the dev's text, unchanged; branch head checked at `e19a6b25` (4 files, +84 −3; the `LookupField.tsx` change read against the body). --- _Generated by [Claude Code](https://claude.ai/code/session_01QSeKYq2vSKPUSvT9fXdzEY)_ Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: objectstack-fleet[bot] <332303061+objectstack-fleet[bot]@users.noreply.github.com>
1 parent 129e637 commit 69c3dc4

4 files changed

Lines changed: 84 additions & 3 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/fields': patch
3+
---
4+
5+
fix(fields): a dependent lookup filters by its parent's id when the parent arrives populated
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
A lookup with `dependsOn` naming another lookup now scopes its candidates by that parent's id when the record holds the parent as a populated row, as a record page does. Before, the filter carried the stringified object (`"[object Object]"`), so the picker on a record page — for example a quote's contact, scoped by the quote's account — always answered "No options found". The quick-select list, the full picker, the people picker and the create-new seed all use the same filter, so all four are fixed. A parent held as a bare id is unchanged.

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,67 @@ describe('LookupField — "+ New" seeds the dependsOn value it filters by (objec
303303
expect(onModal.mock.calls[0][0]).not.toHaveProperty('initialValues');
304304
});
305305
});
306+
307+
describe('LookupField — a populated dependsOn parent scopes by its id (objectstack#36837)', () => {
308+
const dependentField = {
309+
name: 'contact',
310+
label: 'Contact',
311+
reference: 'contacts',
312+
reference_field: 'name',
313+
dependsOn: ['account'],
314+
} as any;
315+
316+
// A record page hands the widget the record as the server returned it: the
317+
// parent lookup arrives populated (`populate=account`), not as a bare id.
318+
const populatedParent = { account: { id: 'a1', name: 'Acme' } };
319+
320+
it('sends the parent record\'s id, never the stringified object', async () => {
321+
const ds = makeDataSource();
322+
render(
323+
<LookupField
324+
field={dependentField}
325+
value={undefined}
326+
onChange={vi.fn()}
327+
readonly={false}
328+
dataSource={ds}
329+
{...({ dependentValues: populatedParent } as any)}
330+
/>,
331+
);
332+
333+
await act(async () => {
334+
fireEvent.click(screen.getByRole('button', { name: /select/i }));
335+
});
336+
337+
await waitFor(() => expect(ds.find).toHaveBeenCalled());
338+
for (const [, params] of ds.find.mock.calls) {
339+
expect(params.$filter.account).toBe('a1');
340+
}
341+
await waitFor(() => {
342+
expect(screen.getByText('Nora Field')).toBeInTheDocument();
343+
expect(screen.queryByText('Oscar Grant')).not.toBeInTheDocument();
344+
});
345+
});
346+
347+
it('control: the same parent as a bare id sends the identical filter', async () => {
348+
const ds = makeDataSource();
349+
render(
350+
<LookupField
351+
field={dependentField}
352+
value={undefined}
353+
onChange={vi.fn()}
354+
readonly={false}
355+
dataSource={ds}
356+
{...({ dependentValues: { account: 'a1' } } as any)}
357+
/>,
358+
);
359+
360+
await act(async () => {
361+
fireEvent.click(screen.getByRole('button', { name: /select/i }));
362+
});
363+
364+
await waitFor(() => expect(ds.find).toHaveBeenCalled());
365+
for (const [, params] of ds.find.mock.calls) {
366+
expect(params.$filter.account).toBe('a1');
367+
}
368+
});
369+
});

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { PHONE_TOUCH_HEIGHT, PHONE_TOUCH_SQUARE } from './touchTarget.js';
2121
import type { DataSource, FormField, QueryParams, LookupColumnDef, LookupFieldMetadata } from '@object-ui/types';
2222
import { RecordPickerDialog, lookupFiltersToRecord } from './RecordPickerDialog.js';
2323
import {
24+
collapseReference,
2425
readRecordBeingEdited,
2526
readsRecord,
2627
resolveLookupFilters,
@@ -865,7 +866,9 @@ export function LookupField({ value, onChange, field, readonly, error: fieldErro
865866
const dependentFilter = useMemo<Record<string, any> | undefined>(() => {
866867
const f: Record<string, any> = {};
867868
for (const { field, param } of dependsOn) {
868-
const v = resolvedDependentValues[field];
869+
// A record page hands over the record as the server returned it, so a
870+
// lookup parent can arrive populated — its id is what the filter compares.
871+
const v = collapseReference(resolvedDependentValues[field]);
869872
if (v === undefined || v === null || v === '') continue;
870873
f[param] = typeof v === 'number' ? v : String(v);
871874
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,12 @@ function isEmptyValue(v: unknown): boolean {
122122
return v === undefined || v === null || v === '' || (Array.isArray(v) && v.length === 0);
123123
}
124124

125-
/** A related record as the query compares it: an expanded row collapses to its id. */
126-
function collapseReference(v: unknown): unknown {
125+
/**
126+
* A related record as the query compares it: an expanded row collapses to its id.
127+
* The one rule for every candidate filter that reads a value of the host record —
128+
* a `{ $record }` comparand here and a `dependsOn` parent in `LookupField`.
129+
*/
130+
export function collapseReference(v: unknown): unknown {
127131
if (!isRow(v) || v instanceof Date) return v;
128132
return v.id ?? v._id ?? v;
129133
}

0 commit comments

Comments
 (0)