Skip to content
Draft
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
23 changes: 23 additions & 0 deletions .changeset/37250-fill-in-dialog-pending-options.md
Original file line number Diff line number Diff line change
@@ -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.

An option that depends on another value typed into the dialog appears once that value is typed. Options that are not visible on that record stay hidden. 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<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.

No metadata key, value or default changes.
11 changes: 11 additions & 0 deletions ui/packages/fields/src/RequiredFieldsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -306,6 +316,7 @@ export function RequiredFieldsDialog({
<FieldEditWidget
field={f.def as never}
value={values[f.name]}
dependentValues={liveRecord}
onChange={(v: unknown) => {
setValues((prev) => ({ ...prev, [f.name]: v }));
// A server reason is about the value it was given; a new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> = MOVE, shown = 'Closed Lost') => {
render(
<RequiredFieldsDialog
open
fields={labelled(collectRequiredWhenPromptFields(PENDING_FIELDS, STORED, move))}
recordName="Globex deal"
changes={[{ name: 'stage', label: 'Stage', value: shown }]}
onCancel={vi.fn()}
onSubmit={vi.fn()}
/>,
);
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();
});
});
12 changes: 11 additions & 1 deletion ui/packages/fields/src/requiredWhenPrompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ export interface RequiredWhenPromptField {
name: string;
/** The field definition, with `name` guaranteed present for the widget. */
def: Record<string, unknown> & { 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<string, unknown>;
}

/** How {@link collectRequiredWhenPromptFields} narrows its set. */
Expand Down Expand Up @@ -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;
}
Expand Down
Loading