Skip to content

Commit 03cb550

Browse files
fix(plugin-detail): a related tab's 「+ 添加」 no longer reads a row's own party as its catalog (#36659) (#36704)
<!-- claude-projects-attribution: {"github_login":"os-warren"} --> _Requested by **Warren** · [project thread](https://claude.ai/code/project/chan_01U5RdnAAmkSQmZbqh6FtqZ8?thread=cmsg_01U5RdnAAmkSQmZbqh6FtqZ82pszhcdkA6eY6pSTqYSy4t)_ Fixes #36659 Clause-②: no Metadata-protocol: none ## 维护者速读 - 在客户的「合同」页签点「添加」,不再弹出一张列着所有客户联系人的选择框,而是直接新建一份属于这个客户的合同;报价、订单、商机的产品明细页签照旧弹出产品选择框。 - 属于记录详情页重做专题,修的是给相关子表页签加「添加」入口时,把合同上必填的「主要联系人」误当成了像产品那样的来源目录。 - 出错的话只是某个页签的「添加」从选择框变成直接新建(或反过来),不碰已有数据,可直接撤回。 - 无。 ## What changes The related tab's 「+ 添加」 (objectstack#35601) derives its picker from the relationship when the page authors no `add` (`derivedAddOf`, `ui/packages/plugin-detail/src/RelatedList.tsx`). For a `master_detail` child it took the child's one other required single-valued lookup as the child's **source catalog**: the picker listed that lookup's object and a pick created a child row naming it. That is right for a line's product and wrong for a row's own party: `crm_contract` is `master_detail` to `crm_account` and requires `crm_contact` (its primary contact, `dependsOn: ['crm_account']`). So an account's 合同 tab opened a picker of `crm_contact` with no filter (the reported `GET /api/v1/data/crm_contact?populate=crm_account&top=10&skip=0`), listing other accounts' contacts, and a pick would have tried to create a contract from a contact. The function's own docblock already stated the condition for a catalog: "the field every row must name, so a pick is a whole row". This PR checks both halves of that sentence before returning a catalog picker: - **The candidates do not depend on the row.** A required lookup with a `dependsOn` entry, or a `lookupFilters` comparand `{ $record }`, is scoped by the row; the picker lists the catalog for the whole list and cannot apply that scope. Such a lookup is a party of the row, not a catalog (`scopedByRow`). - **The pick leaves nothing for a person to write.** Every other required field of the child must be filled by the insert: a non-null `defaultValue` or a select option marked `default` (the engine's `applyFieldDefaults`, `packages/objectql/src/engine-select-option-default.test.ts`), `readonly`, or a computed type (`isComputedFieldType`: formula, summary, rollup, autonumber). `required` is the writer's contract, so a contract that still needs its value and dates is filled in on its form, not completed by a pick (`insertDefaults`). When either fails, `derivedAddOf` returns `undefined`, which is the existing "master_detail with no catalog" path: 「+ 添加」 opens New with the parent filled in. The `lookup` path (pick unlinked children of the list's own object) and an authored `add` are untouched. Measured against HotCRM 6.6.0 metadata (read, not run): `crm_contract` loses the picker on both counts (contact `dependsOn` the account; `contract_term_months`, `start_date`, `end_date`, `contract_value` required with no default). `crm_opportunity_line_item`, `crm_quote_line_item` keep the product picker (product required, no `dependsOn`, `quantity` required with `defaultValue: 1`, `unit_price` deliberately not `required`). Changeset: `@object-ui/plugin-detail` patch. ## Tests `ui/packages/plugin-detail/src/__tests__/RelatedList.enlargedToolbar-27524.test.tsx`, in the existing describe "「+ 添加」 is decided by the relationship": - a contract-shaped child (master_detail parent, required contact `dependsOn` the parent, required value and start date, autonumber number): 「+ 添加」 calls New, opens no picker, and no read of `contact` is made; - `derivedAddOf` returns `undefined` for a required lookup scoped by `dependsOn`, and for one whose `lookupFilters` reads `$record`; - `derivedAddOf` returns `undefined` when another required field has no default; - control: a line whose other required fields default (`defaultValue`, select option `default`), are `readonly` or computed, and whose product carries a static `lookupFilters`, keeps `{ picker: { object: 'product' }, linkField: 'product' }`. The existing catalog test ("master_detail with a catalog: the picker lists the catalog…") passes unchanged. Reverse verification: with the fix committed, `RelatedList.tsx` restored from `origin/main` and the file run, then restored from `HEAD` (`git diff HEAD` empty): the 4 new defect tests fail, the control and the 17 existing tests pass. <details><summary>Package run under the verify lock</summary> ``` pnpm --dir ui exec vitest run --maxWorkers=2 packages/plugin-detail/ Test Files 415 passed | 1 skipped (416) Tests 3898 passed | 18 skipped (3916) os-verify-lock: VERDICT command-exit 0 pnpm --filter @object-ui/plugin-detail type-check (at 26c5daf) > tsc --noEmit && tsc -p tsconfig.test.json os-verify-lock: VERDICT command-exit 0 ``` The package run started before the select-option-default arm was added; the touched test file was re-run at `26c5daf1`: `Tests 21 passed (21)`. </details> ## Acceptance notes - Not changed here, noted: the derived catalog picker does not carry the catalog lookup's own static `lookupFilters` (for example HotCRM's `crm_product` lookup `is_active = true` on opportunity lines), so it can offer a retired product. Read from code, not reproduced in a browser. - Not changed here, noted: the reported empty, English 「NAME」 column follows from the wrong object (contacts have no `name`); with the contract tab no longer opening a picker it does not appear there. - Found while verifying, not changed here (same tab, the `lookup` arm this PR does not touch): on an opportunity's 合同 tab, 「+ 添加」 lists unlinked contracts of **every** account (HotCRM 6.6.0, opportunity "Initech Cloud Migration": the picker offers CTR-0004 of Stark Medical next to CTR-0003 of Initech Solutions; request `GET /api/v1/data/crm_contract?populate=crm_account&top=10&skip=0&filter=[["crm_opportunity","is_empty"]]`), although `crm_contract.crm_opportunity` declares `dependsOn: ['crm_account']`. The same picker's first column is headed 「NAME」 and is empty, because the derived picker's label field falls back to `name` and a contract's name field is `contract_number`. Reported to the seat for filing. - The HotCRM-side card objectstack-ai/hotcrm#2747 (declare that master-detail children offer no "link existing") is not needed for the contract tab after this change; whether it is still wanted is that card's call. ## Verification Read first: 1. `ui/packages/plugin-detail/src/RelatedList.tsx`, `derivedAddOf`: the two new early returns (riskiest hunk: `leftToWrite`, which decides which existing line tabs keep their picker). 2. `scopedByRow` and `insertDefaults` just below it. Browser, HotCRM 6.6.0 (`d513851`, `objectstack dev --seed-admin --fresh`) behind this branch's Console (Vite, `DEV_PROXY_TARGET`), zh-CN, at `961e6e57`: - Account "Acme Corporation" → 合同 tab → 「+ 添加」: the 新建合同 form opens with 所属客户 = Acme Corporation filled in and 主要联系人 empty; no picker dialog (`[data-testid="record-picker-dialog"]` count 0); the requests after the click are the record page's own reads, no `GET /api/v1/data/crm_contact`. - The served schema carries what the rule reads: `GET /api/v1/meta/objects/crm_contract` → `crm_contact` `required: true`, `dependsOn: ["crm_account"]`; `contract_value`, `start_date` `required: true` with no default. - Before (`origin/main`, the card's own repro on 18.6.0): the same click opened 「添加」 listing contacts of two accounts. Screenshots are attached in the project thread (GitHub's image store takes browser uploads only): the account's 合同 tab; the New contract form after 「+ 添加」. Two-minute check: on a HotCRM account, open the 合同 tab and click 「+ 添加」: the New contract form opens with the account filled in, no picker. On an opportunity's product lines tab, 「+ 添加」 still opens the product picker. --- _Generated by [Claude Code](https://claude.ai/code/session_01JQwxbY1aynwSYJFZFRNKBL)_ <!-- signed: repo:objectstack#1 · os-warren · session_01JQwxbZeHNsF3RWcWpj7rGj --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 0fc430a commit 03cb550

3 files changed

Lines changed: 129 additions & 2 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
'@object-ui/plugin-detail': patch
3+
---
4+
5+
fix(plugin-detail): a related tab's 「+ 添加」 no longer reads a row's own party as its source catalog
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
On a record page's related tab, a `master_detail` child whose one other required lookup was not a catalog opened a picker of that lookup's object anyway. An account's 合同 (contracts) tab listed every account's contacts, unfiltered, because a contract requires its primary contact; a pick would have created a contract from a contact.
11+
12+
A required lookup now counts as the child's source catalog only when a pick makes a whole row:
13+
14+
- its candidates are the same for every row — no `dependsOn` and no `lookupFilters` comparand reading `$record` (a contact scoped to the contract's account is a party of that contract, not a catalog);
15+
- every other required field of the child is filled without a person: a non-null `defaultValue`, a select option marked `default`, `readonly`, or a computed type (formula, summary, rollup, autonumber).
16+
17+
Otherwise 「+ 添加」 creates a new row directly, as on a master-detail tab with no catalog. A line under a header whose product is required and whose quantity defaults (quote and opportunity lines) keeps its product picker.

‎ui/packages/plugin-detail/src/RelatedList.tsx‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ import { useSafeFieldLabel, useRecordContext, useRecordPagePreferences, type Rel
9292
import { usePermissions } from '@object-ui/permissions';
9393
import { useObjectLabel, useDisplayLocale, useLocalization, resolveFieldCurrency, recordCurrencyField, recordCurrencyPath, withFollowedParent } from '@object-ui/i18n';
9494
import { useDetailTranslation } from './useDetailTranslation';
95-
import { isMaskedDetailFieldType } from './fieldEnrichment';
95+
import { isComputedFieldType, isMaskedDetailFieldType } from './fieldEnrichment';
9696
import {
9797
COMPACT_RELATED_ROWS,
9898
RELATED_CARD_DROP_LINE,
@@ -1066,6 +1066,10 @@ interface RelationFieldDef {
10661066
multiple?: unknown;
10671067
hidden?: unknown;
10681068
readonly?: unknown;
1069+
defaultValue?: unknown;
1070+
options?: unknown;
1071+
dependsOn?: unknown;
1072+
lookupFilters?: unknown;
10691073
}
10701074

10711075
function relationFieldEntries(fields: unknown): Array<[string, RelationFieldDef]> {
@@ -1093,6 +1097,22 @@ function relationFieldEntries(fields: unknown): Array<[string, RelationFieldDef]
10931097
* - `master_detail` with no catalog (none, or more than one required
10941098
* lookup): `undefined`, and 「+ 添加」 creates a new row directly.
10951099
*
1100+
* A required lookup is a catalog only when both halves of "a pick is a whole
1101+
* row" hold (objectstack#36659 — a contract's required primary contact was
1102+
* read as its catalog, so the account's 合同 tab listed every account's
1103+
* contacts and a pick would have created a contract from a contact):
1104+
*
1105+
* - its candidates are the same for every row: no `dependsOn` and no
1106+
* `lookupFilters` entry reading `$record`. A lookup scoped by the row (the
1107+
* contact `dependsOn` the contract's account) is a party of that row, and
1108+
* the picker, which lists the catalog for the list, cannot apply the scope;
1109+
* - the pick leaves nothing for a person to write: every OTHER required
1110+
* field of the child has a default the insert applies (`defaultValue`, or
1111+
* a select option marked `default`), is `readonly`, or is computed.
1112+
* `required` is the writer's contract, so a child that still needs a
1113+
* start date or an amount is a record someone fills in on its form, not a
1114+
* line a pick completes.
1115+
*
10961116
* `undefined` too for any other field type and for a multi-valued
10971117
* relationship, whose membership a pick cannot write.
10981118
*/
@@ -1117,9 +1137,42 @@ export function derivedAddOf(
11171137
&& typeof def.reference === 'string' && def.reference !== '');
11181138
if (catalogs.length !== 1) return undefined;
11191139
const [catalogField, catalogDef] = catalogs[0];
1140+
if (scopedByRow(catalogDef)) return undefined;
1141+
const leftToWrite = entries.some(([name, def]) =>
1142+
name !== referenceField
1143+
&& name !== catalogField
1144+
&& def.required === true
1145+
&& !insertDefaults(def)
1146+
&& def.readonly !== true
1147+
&& !isComputedFieldType(undefined, def.type));
1148+
if (leftToWrite) return undefined;
11201149
return { picker: { object: catalogDef.reference as string }, linkField: catalogField };
11211150
}
11221151

1152+
/**
1153+
* Does the insert fill this field when a create omits it — the engine's
1154+
* `applyFieldDefaults`: a non-null `defaultValue`, else a select option marked
1155+
* `default: true`?
1156+
*/
1157+
function insertDefaults(def: RelationFieldDef): boolean {
1158+
if (def.defaultValue != null) return true;
1159+
return Array.isArray(def.options) && def.options.some((o) => (o as { default?: unknown } | null)?.default === true);
1160+
}
1161+
1162+
/**
1163+
* Does this lookup's candidate set depend on the row it is written on — a
1164+
* `dependsOn` entry, or a `lookupFilters` comparand `{ $record }`? Such a
1165+
* lookup is no catalog (`derivedAddOf`).
1166+
*/
1167+
function scopedByRow(def: RelationFieldDef): boolean {
1168+
if (Array.isArray(def.dependsOn) && def.dependsOn.length > 0) return true;
1169+
if (!Array.isArray(def.lookupFilters)) return false;
1170+
return def.lookupFilters.some((entry) => {
1171+
const value = (entry as { value?: unknown } | null)?.value;
1172+
return !!value && typeof value === 'object' && '$record' in (value as object);
1173+
});
1174+
}
1175+
11231176
/** One group of a stateful child list (`statusGroupsOf`). */
11241177
export interface RelatedStatusGroup {
11251178
key: string;

‎ui/packages/plugin-detail/src/__tests__/RelatedList.enlargedToolbar-27524.test.tsx‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import '@testing-library/jest-dom';
2828
import React from 'react';
2929
import { FilterScopeProvider, RecordContextProvider, RecordPagePreferencesProvider, type RecordPagePreference } from '@object-ui/react';
3030
import { ComponentRegistry } from '@object-ui/core';
31-
import { RelatedList } from '../RelatedList';
31+
import { RelatedList, derivedAddOf } from '../RelatedList';
3232
import { resetRelatedTabConditionsForTests } from '../relatedTabConditions';
3333

3434
// The related tab's conditions live in module memory for the visit (objectstack#35892).
@@ -319,6 +319,63 @@ describe('「+ 添加」 is decided by the relationship, and a pick is one trans
319319
]);
320320
});
321321

322+
// objectstack#36659 — an account's 合同 tab: a contract is the account's
323+
// master_detail child whose one other required lookup is its primary
324+
// contact, scoped to the account by `dependsOn`, and a contract still needs
325+
// its value and dates. Read as a catalog, the picker listed every account's
326+
// contacts and a pick would have created a contract from a contact.
327+
const CONTRACT_FIELDS = {
328+
contract_number: { type: 'autonumber', label: 'Number', required: true },
329+
lead: { type: 'master_detail', reference: 'lead', label: 'Account', required: true },
330+
contact: { type: 'lookup', reference: 'contact', label: 'Primary Contact', required: true, dependsOn: ['lead'] },
331+
contract_value: { type: 'currency', label: 'Value', required: true },
332+
start_date: { type: 'date', label: 'Start', required: true },
333+
};
334+
335+
it('a contract tab (required contact scoped by the account, value and dates to fill): 「+ 添加」 creates a contract directly, never a picker of contacts (objectstack#36659)', async () => {
336+
const onNew = vi.fn();
337+
const { dataSource } = renderList({ onNew, fields: CONTRACT_FIELDS });
338+
await waitFor(() => expect(screen.getByText('Send quote')).toBeInTheDocument());
339+
fireEvent.click(addButton()!);
340+
expect(onNew).toHaveBeenCalledTimes(1);
341+
expect(screen.queryByTestId('record-picker-dialog')).toBeNull();
342+
expect(dataSource.find.mock.calls.some(([r]: [string]) => r === 'contact')).toBe(false);
343+
});
344+
345+
it.each([
346+
['dependsOn names a field of the row', { dependsOn: ['lead'] }],
347+
['a lookupFilters comparand reads $record', { lookupFilters: [{ field: 'account', operator: 'eq', value: { $record: 'lead' } }] }],
348+
] as const)('a required lookup whose candidates depend on the row is no catalog: %s (objectstack#36659)', (_label, scope) => {
349+
const fields = {
350+
lead: { type: 'master_detail', reference: 'lead', required: true },
351+
contact: { type: 'lookup', reference: 'contact', required: true, ...scope },
352+
};
353+
expect(derivedAddOf(fields, 'lead', 'task')).toBeUndefined();
354+
});
355+
356+
it('a required lookup is no catalog while the child has another required field a pick leaves empty (objectstack#36659)', () => {
357+
const fields = {
358+
lead: { type: 'master_detail', reference: 'lead', required: true },
359+
product: { type: 'lookup', reference: 'product', required: true },
360+
contract_value: { type: 'currency', required: true },
361+
};
362+
expect(derivedAddOf(fields, 'lead', 'task')).toBeUndefined();
363+
});
364+
365+
it('control: a line whose other required fields default, are read-only or computed keeps its catalog — and a static lookupFilters does not scope by the row (objectstack#36659)', () => {
366+
const fields = {
367+
line_number: { type: 'autonumber', required: true },
368+
lead: { type: 'master_detail', reference: 'lead', required: true },
369+
product: { type: 'lookup', reference: 'product', required: true, lookupFilters: [{ field: 'is_active', operator: 'eq', value: true }] },
370+
quantity: { type: 'number', required: true, defaultValue: 1 },
371+
stage: { type: 'select', required: true, options: [{ value: 'open', default: true }, { value: 'won' }] },
372+
list_price: { type: 'currency', required: true, readonly: true },
373+
total: { type: 'formula', required: true },
374+
owner_id: { type: 'lookup', reference: 'sys_user' },
375+
};
376+
expect(derivedAddOf(fields, 'lead', 'task')).toEqual({ picker: { object: 'product' }, linkField: 'product' });
377+
});
378+
322379
it('master_detail with no catalog: 「+ 添加」 creates a row directly', async () => {
323380
const onNew = vi.fn();
324381
renderList({

0 commit comments

Comments
 (0)