diff --git a/.changeset/37250-fill-in-dialog-pending-options.md b/.changeset/37250-fill-in-dialog-pending-options.md new file mode 100644 index 00000000000..e43bf4d69db --- /dev/null +++ b/.changeset/37250-fill-in-dialog-pending-options.md @@ -0,0 +1,23 @@ +--- +'@object-ui/fields': patch +--- + +fix(fields): the required-fields fill-in dialog hands its controls the record the change would leave + +Clause-②: yes (widening) +Metadata-protocol: none + +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. + +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: + +- option visibility; +- `dependsOn` gates on option lists and lookups; +- dependent-lookup filters; +- the currency of a money field that takes its currency from the record or from a parent record. + +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. + +Widening: the exported `RequiredWhenPromptField` type gains one optional member, `record?: Record`. 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. + +No metadata key, value or default changes. diff --git a/ui/packages/fields/src/RequiredFieldsDialog.tsx b/ui/packages/fields/src/RequiredFieldsDialog.tsx index 41411824a26..7a3f7f9bf77 100644 --- a/ui/packages/fields/src/RequiredFieldsDialog.tsx +++ b/ui/packages/fields/src/RequiredFieldsDialog.tsx @@ -188,6 +188,16 @@ export function RequiredFieldsDialog({ shown.push(f); } const missing = shown.filter((f) => isMissingForRequired(values[f.name])); + // The record every control here resolves its options against + // (objectstack#37250): the record the prompt judged its fields on (the + // stored record with the change merged in, carried on each field + // `collectRequiredWhenPromptFields` returns) with the values typed here on + // top, i.e. the record the write would leave. An option `visibleWhen` reads + // `record.*` (`useCascadingOptions`, through `dependentValues`), and with no + // record handed over every option reading the field the change writes was + // judged hidden: the loss reason a move to 「失败」 asks for offered nothing. + const judged = shown.find((f) => f.record)?.record; + const liveRecord = React.useMemo(() => ({ ...judged, ...values }), [judged, values]); const presentRefusal = (err: unknown) => { const { entries, fallback } = refusalOf(err); @@ -306,6 +316,7 @@ export function RequiredFieldsDialog({ { setValues((prev) => ({ ...prev, [f.name]: v })); // A server reason is about the value it was given; a new diff --git a/ui/packages/fields/src/__tests__/RequiredFieldsDialog.inPlace-35956.test.tsx b/ui/packages/fields/src/__tests__/RequiredFieldsDialog.inPlace-35956.test.tsx index 5de14f0c879..7e54f8190cf 100644 --- a/ui/packages/fields/src/__tests__/RequiredFieldsDialog.inPlace-35956.test.tsx +++ b/ui/packages/fields/src/__tests__/RequiredFieldsDialog.inPlace-35956.test.tsx @@ -165,3 +165,118 @@ describe('RequiredFieldsDialog with nothing to fill (objectstack#35956 PR 2, the await waitFor(() => expect(textboxOf(dialog, 'win_reason')).toHaveFocus()); }); }); + +describe('RequiredFieldsDialog judges option visibleWhen on the record with the change merged in (objectstack#37250)', () => { + // The shape hotcrm authors on `crm_opportunity.loss_reason`: every option is + // offered only once the record is (or is being moved to) `closed_lost` — + // i.e. it reads the very field the change writes. + const LOST_ONLY = + '(has(record.stage) && record.stage == "closed_lost") || (has(record.requested_status) && record.requested_status == "closed_lost")'; + const WON_ONLY = + '(has(record.stage) && record.stage == "closed_won") || (has(record.requested_status) && record.requested_status == "closed_won")'; + const PENDING_FIELDS = { + stage: { + type: 'select', + label: 'Stage', + options: [ + { value: 'negotiation', label: 'Negotiation' }, + { value: 'closed_lost', label: 'Closed Lost' }, + { value: 'closed_won', label: 'Closed Won' }, + ], + }, + loss_reason: { + type: 'select', + label: 'Loss Reason', + requiredWhen: 'record.stage == "closed_lost"', + options: [ + { value: 'price', label: 'Price', visibleWhen: LOST_ONLY }, + { value: 'timing', label: 'Timing', visibleWhen: LOST_ONLY }, + ], + }, + loss_channel: { + type: 'radio', + label: 'Loss Channel', + requiredWhen: 'record.stage == "closed_lost"', + options: [ + { value: 'lost_option', label: 'Lost option', visibleWhen: LOST_ONLY }, + { value: 'won_option', label: 'Won option', visibleWhen: 'record.stage == "closed_won"' }, + { value: 'plain_option', label: 'Plain option' }, + { value: 'detail_option', label: 'Detail option', visibleWhen: 'record.loss_detail == "price"' }, + ], + }, + loss_detail: { type: 'text', label: 'Loss Detail', requiredWhen: 'record.stage == "closed_lost"' }, + // The mirror case: a win reason, shown only once the record is (or is + // being moved to) `closed_won`. + win_reason: { + type: 'select', + label: 'Win Reason', + requiredWhen: 'record.stage == "closed_won"', + options: [ + { value: 'product', label: 'Product', visibleWhen: WON_ONLY }, + { value: 'relationship', label: 'Relationship', visibleWhen: WON_ONLY }, + ], + }, + }; + const STORED = { + id: 'o2', name: 'Globex deal', stage: 'negotiation', loss_reason: null, loss_channel: null, loss_detail: null, win_reason: null, + }; + const MOVE = { stage: 'closed_lost' }; + + const renderPending = (move: Record = MOVE, shown = 'Closed Lost') => { + render( + , + ); + return screen.getByTestId('required-fields-dialog'); + }; + const rowOf = (dialog: HTMLElement, name: string) => + dialog.querySelector(`[data-required-fields-field="${name}"]`) as HTMLElement; + + it('the collector carries the record it judged: the stored record with the change merged in', () => { + const out = collectRequiredWhenPromptFields(PENDING_FIELDS, STORED, MOVE); + expect(out.map((f) => f.name)).toEqual(['loss_reason', 'loss_channel', 'loss_detail']); + for (const f of out) expect(f.record).toEqual({ ...STORED, ...MOVE }); + }); + + it('a select whose options read the changed field offers them, instead of the empty state', () => { + const dialog = renderPending(); + const row = rowOf(dialog, 'loss_reason'); + expect(row).not.toBeNull(); + expect(row.querySelector('[data-testid="select-empty-loss_reason"]')).toBeNull(); + expect(within(row).getByRole('combobox')).toBeInTheDocument(); + }); + + it('offers exactly the options visible on the merged record', () => { + const dialog = renderPending(); + const row = rowOf(dialog, 'loss_channel'); + expect(row.querySelector('[data-testid="radio-empty-loss_channel"]')).toBeNull(); + expect(within(row).getByText('Lost option')).toBeInTheDocument(); + expect(within(row).getByText('Plain option')).toBeInTheDocument(); + // Judged on the merged record, not opened up: `stage` is `closed_lost`, so + // the won-only option stays hidden. + expect(within(row).queryByText('Won option')).toBeNull(); + expect(within(row).queryByText('Detail option')).toBeNull(); + }); + + it('the mirror case: a move to closed_won offers the win reasons its options gate on', () => { + const dialog = renderPending({ stage: 'closed_won' }, 'Closed Won'); + expect(rowOf(dialog, 'loss_reason')).toBeNull(); + const row = rowOf(dialog, 'win_reason'); + expect(row).not.toBeNull(); + expect(row.querySelector('[data-testid="select-empty-win_reason"]')).toBeNull(); + expect(within(row).getByRole('combobox')).toBeInTheDocument(); + }); + + it('a value typed into the dialog joins the record the other rows read', () => { + const dialog = renderPending(); + expect(within(rowOf(dialog, 'loss_channel')).queryByText('Detail option')).toBeNull(); + fireEvent.change(within(rowOf(dialog, 'loss_detail')).getByRole('textbox'), { target: { value: 'price' } }); + expect(within(rowOf(dialog, 'loss_channel')).getByText('Detail option')).toBeInTheDocument(); + }); +}); diff --git a/ui/packages/fields/src/requiredWhenPrompt.ts b/ui/packages/fields/src/requiredWhenPrompt.ts index d072c4cee09..94c899a81b5 100644 --- a/ui/packages/fields/src/requiredWhenPrompt.ts +++ b/ui/packages/fields/src/requiredWhenPrompt.ts @@ -72,6 +72,16 @@ export interface RequiredWhenPromptField { name: string; /** The field definition, with `name` guaranteed present for the widget. */ def: Record & { type?: string }; + /** + * The record this field was judged against: the stored record with the + * change merged in, i.e. the record as the write would leave it. Set by + * {@link collectRequiredWhenPromptFields}; the fill-in dialog hands it to + * the field's control, so an option `visibleWhen` that reads the field the + * change writes (`record.stage == "closed_lost"` on a loss reason) is judged + * on the same record the prompt was (objectstack#37250). Absent on a row a + * host builds itself (a field a refusal names). + */ + record?: Record; } /** How {@link collectRequiredWhenPromptFields} narrows its set. */ @@ -152,7 +162,7 @@ export function collectRequiredWhenPromptFields( if (!hasFieldEditWidget(def.type as string | undefined)) continue; if (options.fillable && !options.fillable(name, def)) continue; - prompts.push({ name, def: { name, ...def } }); + prompts.push({ name, def: { name, ...def }, record: next }); } return prompts; }