Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .changeset/36837-dependson-lookup-id.md
Original file line number Diff line number Diff line change
@@ -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.
64 changes: 64 additions & 0 deletions ui/packages/fields/src/widgets/LookupField.dependsOn.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<LookupField
field={dependentField}
value={undefined}
onChange={vi.fn()}
readonly={false}
dataSource={ds}
{...({ dependentValues: populatedParent } as any)}
/>,
);

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(
<LookupField
field={dependentField}
value={undefined}
onChange={vi.fn()}
readonly={false}
dataSource={ds}
{...({ dependentValues: { account: 'a1' } } as any)}
/>,
);

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');
}
});
});
5 changes: 4 additions & 1 deletion ui/packages/fields/src/widgets/LookupField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -865,7 +866,9 @@ export function LookupField({ value, onChange, field, readonly, error: fieldErro
const dependentFilter = useMemo<Record<string, any> | undefined>(() => {
const f: Record<string, any> = {};
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);
}
Expand Down
8 changes: 6 additions & 2 deletions ui/packages/fields/src/widgets/lookupRecordReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading