Skip to content

Commit 0ad44a3

Browse files
fix(fields): the fill-in dialog judges option visibleWhen on the record the write would leave (#37264)
repo:objectstack#4 · os-justin · session_01QSeKYpEsWWE9Y5YtSsDgqZ Fixes #37250 Clause-②: yes (widening) — the exported `RequiredWhenPromptField` of `@object-ui/fields` gains one optional member, `record?: Record<string, unknown>` Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:在记录上就地改动(改阶段、改列表单元格、行内编辑、看板拖动)弹出「补填必填字段」时,弹窗里的下拉、单选、级联选择、关联查找和金额币种,都按这次改动后的记录来显示;比如商机改为「失败」后,丢单原因会列出该有的选项,不再显示「暂无可选项」。 - 为什么做:补填弹窗之前根本没把记录交给里面的控件,凡是要读记录的控件都按一条空记录判断,于是有的选项被藏掉、要先选上级的列表一直停在「请先选择」,改动保存不了。 - 出错会怎样:影响补填弹窗里各控件如何读取记录(选项显示、级联、关联查找的过滤、金额跟随的币种);若有误,最多是弹窗里某些选项多显示或少显示,可直接撤回本次改动,不涉及数据。 - 要你做什么:无 ## What changes The required-fields fill-in dialog rendered each control with no record at all, so an option `visibleWhen` reading `record.*` was judged on an empty record. An option list whose every option reads the field the change writes (hotcrm's `crm_opportunity.loss_reason`, shown only while `stage == "closed_lost"`) offered nothing: the select drew its empty state and the change could not be saved. - `collectRequiredWhenPromptFields` already judges the prompt on one merge, the stored record with the change (`next = { ...record, ...changes }`). Each field it returns now carries that record as `record` (new optional member of `RequiredWhenPromptField`). - `RequiredFieldsDialog` hands every control `dependentValues` = that record with the values typed into the dialog on top, i.e. the record the write would leave. A row a host builds itself (a refusal-named field) reads the same record whenever any shown field carries one. - `requiredWhen` / field `visibleWhen` / `readonlyWhen`: already judged on `next` by the collector (unchanged); the new collector assertion pins that the carried record is that same merge. ## Tests `ui/packages/fields/src/__tests__/RequiredFieldsDialog.inPlace-35956.test.tsx`, new top-level describe (objectstack#37250), five cases: the collector carries the merged record; a select whose options read the changed field offers them instead of the empty state; a radio offers exactly the options visible on the merged record (a won-only option stays hidden); a value typed into the dialog joins the record the other rows read; the mirror case, a select whose options are won-only, offers them on a move to closed-won. ## Acceptance notes - Same family, outside this card's file surface, not changed here: the hosts' `refusalField` rows are built as `{ name, def }` with no record. With at least one collected field shown they now read the dialog's record; with zero collected fields (the record header's stage menu opening the dialog as a pure confirmation) a refusal-added option row reads only the typed values. Hosts: `ui/packages/plugin-detail/src/renderers/recordPathStageChange.tsx`, `ui/packages/plugin-detail/src/InlineEditSaveBar.tsx`, `ui/packages/plugin-grid/src/objectGridRequiredPrompt.tsx` (kanban passes no `refusalField`). - `ui/packages/plugin-timesheet/src/ObjectTimesheet.tsx` add-row dialog renders `FieldEditWidget` with no `dependentValues`, so an option `visibleWhen` reading a sibling row field is judged on an empty record there too. Not a pending-change case; noted only. ## Verification Two-minute check: `cd ui && pnpm --filter @object-ui/fields exec vitest run --root ../.. --maxWorkers=2 packages/fields/src/__tests__/RequiredFieldsDialog.inPlace-35956.test.tsx` — expect 12/12 passed. Read first: `ui/packages/fields/src/RequiredFieldsDialog.tsx` (the `liveRecord` memo and the `dependentValues` prop), then `ui/packages/fields/src/requiredWhenPrompt.ts` (`record: next`). ## Rework round 1 (head `f731d2461f2`) Per the seat L1 7158746595: M1 — the widening is declared here and in the changeset; S1 — the changeset now states the condition as "the dialog handed its controls no record" and names what now resolves (option visibility, `dependsOn` gates, dependent-lookup filters, a money field's followed currency); N1 — a `win_reason` mirror pin. Dialog file 12/12; `@object-ui/fields` 5021 passed; type-check 0; 38/38 derived gates exit 0. Follow-up for host refusal rows: #37270. Host refusal rows (refusal-named fields with no `record`) are carried by #37270. ## Provenance Opened by seat repo:objectstack#4 on the maintainer's authorization in the seat session (「#37250(推送被拦):放行」). The seat's os-dev subagent's first push was refused by its permission check; it stopped without retrying. Branch `claude/issue-37250-fill-in-dialog-pending-values` @ `57e988ac224`. --- 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01QSeKYpEsWWE9Y5YtSsDgqZ --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent c94191d commit 0ad44a3

4 files changed

Lines changed: 160 additions & 1 deletion

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
'@object-ui/fields': patch
3+
---
4+
5+
fix(fields): the required-fields fill-in dialog hands its controls the record the change would leave
6+
7+
Clause-②: yes (widening)
8+
Metadata-protocol: none
9+
10+
A change made in place (a stage move on the record page, a list cell, an inline edit, a kanban drop) can make another field required. The fill-in dialog then asks for that field. The dialog handed its controls no record, so every control that reads the record judged it on an empty one. Any option `visibleWhen` that read a record field was judged without that field. A condition guarded by `has(record.x)` was false, so the option was hidden. A condition without that guard could not be evaluated, and the option was offered whatever the condition said. A choice field whose options were all hidden showed "No options available", and the change could not be saved. Example: each loss reason is shown only while the stage is "closed lost", so moving an opportunity to "closed lost" asked for a loss reason and offered none. Every option list gated by `dependsOn`, and every dependent lookup, stayed on its "select the parent first" gate.
11+
12+
The dialog now hands every control the record as the write would leave it: the saved record, with the change and the values typed into the dialog so far applied on top. What reads that record now resolves on it:
13+
14+
- option visibility;
15+
- `dependsOn` gates on option lists and lookups;
16+
- dependent-lookup filters;
17+
- the currency of a money field that takes its currency from the record or from a parent record.
18+
19+
Each option is now shown or hidden by its condition on the record the write would leave. If the condition is true on that record, the option is shown. If it is false, the option is hidden. This includes an option the dialog used to offer only because its condition could not be evaluated on an empty record. For example, an unguarded `record.stage == "closed_lost"` used to be offered whatever the stage. It is now hidden when the stage stays "negotiation". So in an app whose option conditions do not guard with `has()`, the dialog can offer fewer options than before. A condition that still cannot be evaluated on that record is still offered, for example one that names a field the record does not carry. An option that depends on another value typed into the dialog appears once that value is typed. Which fields the dialog asks for was already decided on this record and does not change.
20+
21+
Widening: the exported `RequiredWhenPromptField` type gains one optional member, `record?: Record<string, unknown>`. That is the record the field was judged against. `collectRequiredWhenPromptFields` sets it on every field it returns, and `RequiredFieldsDialogField` inherits it. The dialog passes it, with the typed values, to every control as `dependentValues`. A row a host builds itself (a field a refusal names) carries no `record`. It reads the record of another field shown in the dialog. When no shown field carries one, it reads only the typed values.
22+
23+
No metadata key, value or default changes.

‎ui/packages/fields/src/RequiredFieldsDialog.tsx‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,16 @@ export function RequiredFieldsDialog({
188188
shown.push(f);
189189
}
190190
const missing = shown.filter((f) => isMissingForRequired(values[f.name]));
191+
// The record every control here resolves its options against
192+
// (objectstack#37250): the record the prompt judged its fields on (the
193+
// stored record with the change merged in, carried on each field
194+
// `collectRequiredWhenPromptFields` returns) with the values typed here on
195+
// top, i.e. the record the write would leave. An option `visibleWhen` reads
196+
// `record.*` (`useCascadingOptions`, through `dependentValues`), and with no
197+
// record handed over every option reading the field the change writes was
198+
// judged hidden: the loss reason a move to 「失败」 asks for offered nothing.
199+
const judged = shown.find((f) => f.record)?.record;
200+
const liveRecord = React.useMemo(() => ({ ...judged, ...values }), [judged, values]);
191201

192202
const presentRefusal = (err: unknown) => {
193203
const { entries, fallback } = refusalOf(err);
@@ -306,6 +316,7 @@ export function RequiredFieldsDialog({
306316
<FieldEditWidget
307317
field={f.def as never}
308318
value={values[f.name]}
319+
dependentValues={liveRecord}
309320
onChange={(v: unknown) => {
310321
setValues((prev) => ({ ...prev, [f.name]: v }));
311322
// A server reason is about the value it was given; a new

‎ui/packages/fields/src/__tests__/RequiredFieldsDialog.inPlace-35956.test.tsx‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,118 @@ describe('RequiredFieldsDialog with nothing to fill (objectstack#35956 PR 2, the
165165
await waitFor(() => expect(textboxOf(dialog, 'win_reason')).toHaveFocus());
166166
});
167167
});
168+
169+
describe('RequiredFieldsDialog judges option visibleWhen on the record with the change merged in (objectstack#37250)', () => {
170+
// The shape hotcrm authors on `crm_opportunity.loss_reason`: every option is
171+
// offered only once the record is (or is being moved to) `closed_lost` —
172+
// i.e. it reads the very field the change writes.
173+
const LOST_ONLY =
174+
'(has(record.stage) && record.stage == "closed_lost") || (has(record.requested_status) && record.requested_status == "closed_lost")';
175+
const WON_ONLY =
176+
'(has(record.stage) && record.stage == "closed_won") || (has(record.requested_status) && record.requested_status == "closed_won")';
177+
const PENDING_FIELDS = {
178+
stage: {
179+
type: 'select',
180+
label: 'Stage',
181+
options: [
182+
{ value: 'negotiation', label: 'Negotiation' },
183+
{ value: 'closed_lost', label: 'Closed Lost' },
184+
{ value: 'closed_won', label: 'Closed Won' },
185+
],
186+
},
187+
loss_reason: {
188+
type: 'select',
189+
label: 'Loss Reason',
190+
requiredWhen: 'record.stage == "closed_lost"',
191+
options: [
192+
{ value: 'price', label: 'Price', visibleWhen: LOST_ONLY },
193+
{ value: 'timing', label: 'Timing', visibleWhen: LOST_ONLY },
194+
],
195+
},
196+
loss_channel: {
197+
type: 'radio',
198+
label: 'Loss Channel',
199+
requiredWhen: 'record.stage == "closed_lost"',
200+
options: [
201+
{ value: 'lost_option', label: 'Lost option', visibleWhen: LOST_ONLY },
202+
{ value: 'won_option', label: 'Won option', visibleWhen: 'record.stage == "closed_won"' },
203+
{ value: 'plain_option', label: 'Plain option' },
204+
{ value: 'detail_option', label: 'Detail option', visibleWhen: 'record.loss_detail == "price"' },
205+
],
206+
},
207+
loss_detail: { type: 'text', label: 'Loss Detail', requiredWhen: 'record.stage == "closed_lost"' },
208+
// The mirror case: a win reason, shown only once the record is (or is
209+
// being moved to) `closed_won`.
210+
win_reason: {
211+
type: 'select',
212+
label: 'Win Reason',
213+
requiredWhen: 'record.stage == "closed_won"',
214+
options: [
215+
{ value: 'product', label: 'Product', visibleWhen: WON_ONLY },
216+
{ value: 'relationship', label: 'Relationship', visibleWhen: WON_ONLY },
217+
],
218+
},
219+
};
220+
const STORED = {
221+
id: 'o2', name: 'Globex deal', stage: 'negotiation', loss_reason: null, loss_channel: null, loss_detail: null, win_reason: null,
222+
};
223+
const MOVE = { stage: 'closed_lost' };
224+
225+
const renderPending = (move: Record<string, unknown> = MOVE, shown = 'Closed Lost') => {
226+
render(
227+
<RequiredFieldsDialog
228+
open
229+
fields={labelled(collectRequiredWhenPromptFields(PENDING_FIELDS, STORED, move))}
230+
recordName="Globex deal"
231+
changes={[{ name: 'stage', label: 'Stage', value: shown }]}
232+
onCancel={vi.fn()}
233+
onSubmit={vi.fn()}
234+
/>,
235+
);
236+
return screen.getByTestId('required-fields-dialog');
237+
};
238+
const rowOf = (dialog: HTMLElement, name: string) =>
239+
dialog.querySelector(`[data-required-fields-field="${name}"]`) as HTMLElement;
240+
241+
it('the collector carries the record it judged: the stored record with the change merged in', () => {
242+
const out = collectRequiredWhenPromptFields(PENDING_FIELDS, STORED, MOVE);
243+
expect(out.map((f) => f.name)).toEqual(['loss_reason', 'loss_channel', 'loss_detail']);
244+
for (const f of out) expect(f.record).toEqual({ ...STORED, ...MOVE });
245+
});
246+
247+
it('a select whose options read the changed field offers them, instead of the empty state', () => {
248+
const dialog = renderPending();
249+
const row = rowOf(dialog, 'loss_reason');
250+
expect(row).not.toBeNull();
251+
expect(row.querySelector('[data-testid="select-empty-loss_reason"]')).toBeNull();
252+
expect(within(row).getByRole('combobox')).toBeInTheDocument();
253+
});
254+
255+
it('offers exactly the options visible on the merged record', () => {
256+
const dialog = renderPending();
257+
const row = rowOf(dialog, 'loss_channel');
258+
expect(row.querySelector('[data-testid="radio-empty-loss_channel"]')).toBeNull();
259+
expect(within(row).getByText('Lost option')).toBeInTheDocument();
260+
expect(within(row).getByText('Plain option')).toBeInTheDocument();
261+
// Judged on the merged record, not opened up: `stage` is `closed_lost`, so
262+
// the won-only option stays hidden.
263+
expect(within(row).queryByText('Won option')).toBeNull();
264+
expect(within(row).queryByText('Detail option')).toBeNull();
265+
});
266+
267+
it('the mirror case: a move to closed_won offers the win reasons its options gate on', () => {
268+
const dialog = renderPending({ stage: 'closed_won' }, 'Closed Won');
269+
expect(rowOf(dialog, 'loss_reason')).toBeNull();
270+
const row = rowOf(dialog, 'win_reason');
271+
expect(row).not.toBeNull();
272+
expect(row.querySelector('[data-testid="select-empty-win_reason"]')).toBeNull();
273+
expect(within(row).getByRole('combobox')).toBeInTheDocument();
274+
});
275+
276+
it('a value typed into the dialog joins the record the other rows read', () => {
277+
const dialog = renderPending();
278+
expect(within(rowOf(dialog, 'loss_channel')).queryByText('Detail option')).toBeNull();
279+
fireEvent.change(within(rowOf(dialog, 'loss_detail')).getByRole('textbox'), { target: { value: 'price' } });
280+
expect(within(rowOf(dialog, 'loss_channel')).getByText('Detail option')).toBeInTheDocument();
281+
});
282+
});

‎ui/packages/fields/src/requiredWhenPrompt.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ export interface RequiredWhenPromptField {
7272
name: string;
7373
/** The field definition, with `name` guaranteed present for the widget. */
7474
def: Record<string, unknown> & { type?: string };
75+
/**
76+
* The record this field was judged against: the stored record with the
77+
* change merged in, i.e. the record as the write would leave it. Set by
78+
* {@link collectRequiredWhenPromptFields}; the fill-in dialog hands it to
79+
* the field's control, so an option `visibleWhen` that reads the field the
80+
* change writes (`record.stage == "closed_lost"` on a loss reason) is judged
81+
* on the same record the prompt was (objectstack#37250). Absent on a row a
82+
* host builds itself (a field a refusal names).
83+
*/
84+
record?: Record<string, unknown>;
7585
}
7686

7787
/** How {@link collectRequiredWhenPromptFields} narrows its set. */
@@ -152,7 +162,7 @@ export function collectRequiredWhenPromptFields(
152162
if (!hasFieldEditWidget(def.type as string | undefined)) continue;
153163
if (options.fillable && !options.fillable(name, def)) continue;
154164

155-
prompts.push({ name, def: { name, ...def } });
165+
prompts.push({ name, def: { name, ...def }, record: next });
156166
}
157167
return prompts;
158168
}

0 commit comments

Comments
 (0)