diff --git a/.changeset/36837-dependson-lookup-id.md b/.changeset/36837-dependson-lookup-id.md new file mode 100644 index 00000000000..fdc4704709a --- /dev/null +++ b/.changeset/36837-dependson-lookup-id.md @@ -0,0 +1,10 @@ +--- +'@object-ui/fields': patch +--- + +fix(fields): a dependent lookup filters by its parent's id when the parent arrives populated + +Clause-②: no +Metadata-protocol: none + +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. diff --git a/ui/packages/fields/src/widgets/LookupField.dependsOn.test.tsx b/ui/packages/fields/src/widgets/LookupField.dependsOn.test.tsx index b60ce52f0c8..185d601818b 100644 --- a/ui/packages/fields/src/widgets/LookupField.dependsOn.test.tsx +++ b/ui/packages/fields/src/widgets/LookupField.dependsOn.test.tsx @@ -303,3 +303,67 @@ describe('LookupField — "+ New" seeds the dependsOn value it filters by (objec expect(onModal.mock.calls[0][0]).not.toHaveProperty('initialValues'); }); }); + +describe('LookupField — a populated dependsOn parent scopes by its id (objectstack#36837)', () => { + const dependentField = { + name: 'contact', + label: 'Contact', + reference: 'contacts', + reference_field: 'name', + dependsOn: ['account'], + } as any; + + // A record page hands the widget the record as the server returned it: the + // parent lookup arrives populated (`populate=account`), not as a bare id. + const populatedParent = { account: { id: 'a1', name: 'Acme' } }; + + it('sends the parent record\'s id, never the stringified object', async () => { + const ds = makeDataSource(); + render( + , + ); + + await act(async () => { + fireEvent.click(screen.getByRole('button', { name: /select/i })); + }); + + await waitFor(() => expect(ds.find).toHaveBeenCalled()); + for (const [, params] of ds.find.mock.calls) { + expect(params.$filter.account).toBe('a1'); + } + await waitFor(() => { + expect(screen.getByText('Nora Field')).toBeInTheDocument(); + expect(screen.queryByText('Oscar Grant')).not.toBeInTheDocument(); + }); + }); + + it('control: the same parent as a bare id sends the identical filter', async () => { + const ds = makeDataSource(); + render( + , + ); + + await act(async () => { + fireEvent.click(screen.getByRole('button', { name: /select/i })); + }); + + await waitFor(() => expect(ds.find).toHaveBeenCalled()); + for (const [, params] of ds.find.mock.calls) { + expect(params.$filter.account).toBe('a1'); + } + }); +}); diff --git a/ui/packages/fields/src/widgets/LookupField.tsx b/ui/packages/fields/src/widgets/LookupField.tsx index 4717859dc28..70057078820 100644 --- a/ui/packages/fields/src/widgets/LookupField.tsx +++ b/ui/packages/fields/src/widgets/LookupField.tsx @@ -21,6 +21,7 @@ import { PHONE_TOUCH_HEIGHT, PHONE_TOUCH_SQUARE } from './touchTarget.js'; import type { DataSource, FormField, QueryParams, LookupColumnDef, LookupFieldMetadata } from '@object-ui/types'; import { RecordPickerDialog, lookupFiltersToRecord } from './RecordPickerDialog.js'; import { + collapseReference, readRecordBeingEdited, readsRecord, resolveLookupFilters, @@ -865,7 +866,9 @@ export function LookupField({ value, onChange, field, readonly, error: fieldErro const dependentFilter = useMemo | undefined>(() => { const f: Record = {}; for (const { field, param } of dependsOn) { - const v = resolvedDependentValues[field]; + // A record page hands over the record as the server returned it, so a + // lookup parent can arrive populated — its id is what the filter compares. + const v = collapseReference(resolvedDependentValues[field]); if (v === undefined || v === null || v === '') continue; f[param] = typeof v === 'number' ? v : String(v); } diff --git a/ui/packages/fields/src/widgets/lookupRecordReference.ts b/ui/packages/fields/src/widgets/lookupRecordReference.ts index 85d3abbee49..df8b5d058b1 100644 --- a/ui/packages/fields/src/widgets/lookupRecordReference.ts +++ b/ui/packages/fields/src/widgets/lookupRecordReference.ts @@ -122,8 +122,12 @@ function isEmptyValue(v: unknown): boolean { return v === undefined || v === null || v === '' || (Array.isArray(v) && v.length === 0); } -/** A related record as the query compares it: an expanded row collapses to its id. */ -function collapseReference(v: unknown): unknown { +/** + * A related record as the query compares it: an expanded row collapses to its id. + * The one rule for every candidate filter that reads a value of the host record — + * a `{ $record }` comparand here and a `dependsOn` parent in `LookupField`. + */ +export function collapseReference(v: unknown): unknown { if (!isRow(v) || v instanceof Date) return v; return v.id ?? v._id ?? v; }