Skip to content

Commit 82a8510

Browse files
fix(plugin-detail, plugin-grid): a refusal-named fill-in row reads the record the change would leave (#37394)
Fixes #37270 Clause-②: no Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:在阶段菜单、记录页保存、列表单元格弹出的补填弹窗里,服务端拒绝保存时临时补上的字段,下拉选项现在按这次改动后的记录判断显示;阶段菜单的纯确认框里不再出现没有可选项的补填行。 - 为什么做:补填弹窗选项判断的同族收尾,替上一张「失败原因下拉暂无可选项」的卡补上服务端临时点名字段这一条少见路径。 - 出错会怎样:只影响服务端点名前端没预料到的字段时那一行的可选项;改动只是给这一行带上同一份合并后的记录,可直接撤回。 - 要你做什么:无。 ## What changes Each host's `refusalField` builder now hands the row it adds the record the change would leave, the same merge the collector judges its own rows on (`collectRequiredWhenPromptFields`: stored record + change). `RequiredFieldsDialog` resolves every control against the first shown row's `record` plus the typed values; with no collected row shown, the refusal row was the only candidate and carried none, so its options were judged on the typed values alone. - `ui/packages/plugin-detail/src/renderers/recordPathStageChange.tsx`: `moved = { ...record, [statusField]: stage.value }`, set on the refusal row. This is the host where the defect is user-visible: a stage that needs nothing opens the dialog with no collected row. - `ui/packages/plugin-detail/src/InlineEditSaveBar.tsx`: `drafted = { ...data, ...ask.draft }`, set on the refusal row. - `ui/packages/plugin-grid/src/objectGridRequiredPrompt.tsx`: `{ ...record, [columnKey]: value }`, set on the refusal row. No new authoring surface, no spec change, no change in `ui/packages/fields`. Kanban passes no `refusalField`; the timesheet add-row dialog is out of scope (card). Files to read first (by risk): `recordPathStageChange.tsx` (the merge must match the collector's: `statusField` over the stored record), `objectGridRequiredPrompt.tsx` (`record` is the row with lacking keys read in, not the bare row), `InlineEditSaveBar.tsx`. Two-minute check: `pnpm --dir ui exec vitest run packages/plugin-detail/src/__tests__/RecordStageMenu.refusalRowRecord-37270.test.tsx` — 1 passed; the added `channel` row shows "Negotiation option" and not "Lost option". ## Tests One pin per host, each red without its host's line (ablation below): - `RecordStageMenu.refusalRowRecord-37270.test.tsx` (behaviour): a move to a stage that needs nothing, refused naming a radio whose options gate on `record.stage`; the added row offers the option visible on the moved record, not the empty state, and keeps the other stage's option hidden. - `InlineEditSaveBar.refusalRowRecord-37270.test.tsx` and `objectGridRequiredPrompt.refusalRowRecord-37270.test.tsx` (row shape): the row `refusalField` returns carries `{ ...stored, ...change }`. In these two hosts the dialog only opens with at least one collected row, which already carries the merged record, so the dialog's rendered options do not differ without the fix; the pin is on the row the host hands over. ## Acceptance notes - Done-when 1 ("visibleWhen reads the record the write would leave, also when no collected field is shown"): measured in the stage-menu host, the only host whose dialog can show no collected row. In the inline Save and list cell hosts it already held through the collected row's record; this PR makes their refusal rows carry it themselves, so it no longer depends on row order. - Done-when 2 ("one pin per host, red without the fix"): three pins, three ablations red. ## Verification Local readings land in the os-dev-report comment on #37270 and are folded in here once the package run finishes. --- _Generated by [Claude Code](https://claude.ai/code/session_01Utp6Ca6EBDpTBZk8v46Jjv)_ Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: objectstack-fleet[bot] <332303061+objectstack-fleet[bot]@users.noreply.github.com>
1 parent c2b2e4f commit 82a8510

7 files changed

Lines changed: 280 additions & 3 deletions
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
'@object-ui/plugin-detail': patch
3+
'@object-ui/plugin-grid': patch
4+
---
5+
6+
fix(plugin-detail, plugin-grid): a field the server's refusal adds to the fill-in dialog reads the record the change would leave
7+
8+
Clause-②: no
9+
Metadata-protocol: none
10+
11+
The fill-in dialog adds a row for a field a refusal names. That row now carries the record the change would leave (the stored record with the change merged in), as the dialog's other rows do, in all three hosts: the stage menu, the record page's inline Save and the list cell. An option `visibleWhen` on that row reads the change, so the stage menu's confirmation, which opens with no other row, no longer shows such a row with no options to pick.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,9 @@ export const InlineEditSaveBar: React.FC<InlineEditSaveBarProps> = ({
761761
return Array.isArray(value) ? value.map(one).join(', ') : one(value);
762762
};
763763
const exclude = Object.keys(ask.draft);
764+
// The record the draft would leave: the merge the collector judges its
765+
// rows on. A row a refusal names carries it too (objectstack#37270).
766+
const drafted = { ...data, ...ask.draft };
764767
return (
765768
<RequiredFieldsDialog
766769
open
@@ -777,7 +780,7 @@ export const InlineEditSaveBar: React.FC<InlineEditSaveBarProps> = ({
777780
const def = objectFields?.[name] as Record<string, unknown> | undefined;
778781
if (!def || typeof def !== 'object' || exclude.includes(name)) return null;
779782
if (!hasFieldEditWidget(def.type as string | undefined) || !fillable(name, def)) return null;
780-
return label({ name, def: { name, ...def } });
783+
return label({ name, def: { name, ...def }, record: drafted });
781784
}}
782785
labelOf={(name) => (objectFields?.[name] ? labelForField(name) : undefined)}
783786
// Cancel writes nothing and leaves the edit session as it was: the
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*
8+
* objectstack#37270 — the record page's inline Save dialog: the row a refusal
9+
* names carries the record the draft would leave (the record with the draft
10+
* merged in, the merge the collector judges its rows on), so an option
11+
* `visibleWhen` on that row reads the draft, as the collected rows' do.
12+
*/
13+
import { describe, it, expect, vi, afterEach } from 'vitest';
14+
import { render, screen, fireEvent, cleanup } from '@testing-library/react';
15+
import '@testing-library/jest-dom';
16+
import React from 'react';
17+
import { InlineEditProvider, RecordContextProvider, useInlineEdit } from '@object-ui/react';
18+
19+
type RefusalField = (name: string) => { name: string; record?: Record<string, unknown> } | null;
20+
const seen = vi.hoisted(() => ({ refusalField: null as null | ((name: string) => unknown) }));
21+
22+
vi.mock('@object-ui/fields', async (importOriginal) => {
23+
const actual = await importOriginal<typeof import('@object-ui/fields')>();
24+
const Dialog = (props: React.ComponentProps<typeof actual.RequiredFieldsDialog>) => {
25+
seen.refusalField = props.refusalField ?? null;
26+
return React.createElement(actual.RequiredFieldsDialog, props);
27+
};
28+
return { ...actual, RequiredFieldsDialog: Dialog };
29+
});
30+
31+
import { registerAllFields } from '@object-ui/fields';
32+
import { InlineEditSaveBar } from '../InlineEditSaveBar';
33+
34+
registerAllFields();
35+
afterEach(() => { cleanup(); seen.refusalField = null; });
36+
37+
const OBJECT_SCHEMA = {
38+
name: 'deal',
39+
fields: {
40+
name: { type: 'text', label: 'Name' },
41+
stage: { type: 'select', label: 'Stage', options: [{ value: 'new', label: 'New' }, { value: 'won', label: 'Won' }] },
42+
win_reason: { type: 'text', label: 'Win Reason', requiredWhen: 'record.stage == "won"' },
43+
// No `requiredWhen`: only a refusal names it.
44+
channel: {
45+
type: 'radio',
46+
label: 'Channel',
47+
options: [{ value: 'won_option', label: 'Won option', visibleWhen: 'record.stage == "won"' }],
48+
},
49+
},
50+
};
51+
const DATA = { id: 'd1', name: 'Acme deal', stage: 'new', win_reason: null, channel: null, updated_at: 'v1' };
52+
53+
function Harness() {
54+
const inline = useInlineEdit()!;
55+
return (
56+
<>
57+
<button onClick={() => inline.enter('stage')}>edit-enter</button>
58+
<button onClick={() => inline.setField('stage', 'won')}>edit-stage-won</button>
59+
</>
60+
);
61+
}
62+
63+
describe('#37270 the inline Save dialog hands a refusal-named row the drafted record', () => {
64+
it('the refusal row carries the record with the draft merged in', async () => {
65+
const update = vi.fn().mockResolvedValue({});
66+
render(
67+
<RecordContextProvider objectName="deal" recordId="d1" data={DATA} objectSchema={OBJECT_SCHEMA} dataSource={{ update } as never}>
68+
<InlineEditProvider canEdit>
69+
<Harness />
70+
<InlineEditSaveBar dataSource={{ update }} objectName="deal" recordId="d1" data={DATA} refresh={vi.fn()} />
71+
</InlineEditProvider>
72+
</RecordContextProvider>,
73+
);
74+
fireEvent.click(screen.getByText('edit-enter'));
75+
fireEvent.click(screen.getByText('edit-stage-won'));
76+
fireEvent.click(screen.getByRole('button', { name: 'Save' }));
77+
await screen.findByTestId('required-fields-dialog');
78+
expect(update).not.toHaveBeenCalled();
79+
const row = (seen.refusalField as RefusalField | null)?.('channel');
80+
expect(row?.name).toBe('channel');
81+
expect(row?.record).toEqual({ ...DATA, stage: 'won' });
82+
});
83+
});
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*
8+
* objectstack#37270 — the stage menu's dialog: a row a refusal names reads the
9+
* record the move would leave, also when no collected row is shown. A move to
10+
* a stage that needs nothing opens the dialog with no fields; the server then
11+
* refuses it naming a radio whose options read `record.stage`. The row it adds
12+
* offers the options visible on the moved record, not the empty state.
13+
*
14+
* `@object-ui/*` resolves to source through the root vitest alias table.
15+
*/
16+
17+
import * as React from 'react';
18+
import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest';
19+
import { render, cleanup, fireEvent, screen, waitFor, within } from '@testing-library/react';
20+
import '@testing-library/jest-dom';
21+
import { InlineEditProvider, RecordContextProvider } from '@object-ui/react';
22+
23+
const { permsStub } = vi.hoisted(() => ({
24+
permsStub: {
25+
isLoaded: true,
26+
checkField: () => true,
27+
check: () => ({ allowed: true }),
28+
getFieldPermissions: () => [],
29+
getRowFilter: () => undefined,
30+
getObjectApiOperations: () => undefined,
31+
roles: [],
32+
userId: null,
33+
systemPermissions: undefined,
34+
hasCapabilities: () => true,
35+
can: () => true,
36+
cannot: () => false,
37+
},
38+
}));
39+
40+
vi.mock('@object-ui/permissions', async (importOriginal) => {
41+
const actual = await importOriginal<typeof import('@object-ui/permissions')>();
42+
return { ...actual, usePermissions: () => permsStub as any };
43+
});
44+
45+
import { RecordStageMenu } from '../RecordStageMenu';
46+
47+
const OPPORTUNITY = {
48+
name: 'crm_opportunity',
49+
fields: {
50+
name: { type: 'text', label: 'Name' },
51+
stage: {
52+
type: 'select',
53+
label: 'Stage',
54+
options: [
55+
{ value: 'prospecting', label: 'Prospecting' },
56+
{ value: 'negotiation', label: 'Negotiation' },
57+
{ value: 'closed_lost', label: 'Closed Lost' },
58+
],
59+
},
60+
// No `requiredWhen`: the client cannot foresee it, only the server names it.
61+
channel: {
62+
type: 'radio',
63+
label: 'Channel',
64+
options: [
65+
{ value: 'negotiation_option', label: 'Negotiation option', visibleWhen: 'record.stage == "negotiation"' },
66+
{ value: 'lost_option', label: 'Lost option', visibleWhen: 'record.stage == "closed_lost"' },
67+
],
68+
},
69+
},
70+
};
71+
72+
beforeEach(() => {
73+
vi.stubGlobal('fetch', vi.fn(async () => ({ ok: false, status: 501, json: async () => ({}) })));
74+
});
75+
76+
afterEach(() => {
77+
cleanup();
78+
vi.unstubAllGlobals();
79+
});
80+
81+
describe('#37270 a refusal-named row in the stage dialog reads the moved record', () => {
82+
it('with no collected row shown, the added row offers the options visible on the record the move leaves', async () => {
83+
const refusal = Object.assign(new Error('VALIDATION_FAILED'), {
84+
code: 'VALIDATION_FAILED',
85+
fields: [{ field: 'channel', code: 'required', message: 'Channel is required to negotiate' }],
86+
});
87+
const dataSource = { update: vi.fn(async () => { throw refusal; }) };
88+
render(
89+
<RecordContextProvider
90+
objectName="crm_opportunity"
91+
recordId="o1"
92+
data={{ id: 'o1', name: 'Wayne deal', stage: 'prospecting', channel: null, updated_at: 'v1' }}
93+
objectSchema={OPPORTUNITY}
94+
dataSource={dataSource as any}
95+
reread={vi.fn(async () => {})}
96+
>
97+
<InlineEditProvider canEdit>
98+
<RecordStageMenu field="stage" label="Prospecting" tag={<span>Prospecting</span>} />
99+
</InlineEditProvider>
100+
</RecordContextProvider>,
101+
);
102+
fireEvent.pointerDown(screen.getByRole('button', { name: /^Stage: / }), { button: 0, ctrlKey: false, pointerType: 'mouse' });
103+
const menu = screen.getByRole('menu');
104+
fireEvent.click(within(menu).getAllByRole('menuitem').find((el) => el.textContent?.startsWith('Negotiation'))!);
105+
let dialog = await screen.findByRole('dialog');
106+
expect(dialog.querySelector('[data-required-fields-field]')).toBeNull();
107+
fireEvent.click(within(dialog).getByRole('button', { name: 'Move' }));
108+
await waitFor(() => expect(dataSource.update).toHaveBeenCalledTimes(1));
109+
dialog = await screen.findByRole('dialog');
110+
const row = await waitFor(() => {
111+
const el = dialog.querySelector('[data-required-fields-field="channel"]') as HTMLElement | null;
112+
expect(el).not.toBeNull();
113+
return el as HTMLElement;
114+
});
115+
expect(row.querySelector('[data-testid="radio-empty-channel"]')).toBeNull();
116+
expect(within(row).getByText('Negotiation option')).toBeInTheDocument();
117+
// Judged on the moved record, not opened up.
118+
expect(within(row).queryByText('Lost option')).toBeNull();
119+
});
120+
});

‎ui/packages/plugin-detail/src/renderers/recordPathStageChange.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ export function useStageChange(
398398
);
399399
const sendable = (values: Record<string, unknown>) =>
400400
noLongerAsked.size === 0 ? values : Object.fromEntries(Object.entries(values).filter(([k]) => !noLongerAsked.has(k)));
401+
// The record the move would leave: the merge the collector judges its
402+
// rows on. A row a refusal names carries it too, so its options read the
403+
// move even when no collected row is shown (objectstack#37270).
404+
const moved = { ...record, [statusField]: stage.value };
401405
return (
402406
<RequiredFieldsDialog
403407
// A new pick is a new collection: the dialog's typed values reset.
@@ -417,7 +421,7 @@ export function useStageChange(
417421
const def = objectFields?.[name] as Record<string, unknown> | undefined;
418422
if (!def || typeof def !== 'object' || name === statusField) return null;
419423
if (!hasFieldEditWidget(def.type as string | undefined) || !fillable(name, def)) return null;
420-
return label({ name, def: { name, ...def } });
424+
return label({ name, def: { name, ...def }, record: moved });
421425
}}
422426
// The stage field and an object-level (`_record`) reason read alone,
423427
// as `stageRefusalLines` reads them; another field by its label.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*
8+
* objectstack#37270 — the list cell's fill-in dialog: the row a refusal names
9+
* carries the record the change would leave (the row with the cell's value
10+
* merged in, the merge the collector judges its rows on), so an option
11+
* `visibleWhen` on that row reads the change, as the collected rows' do.
12+
*/
13+
14+
import { describe, it, expect, vi } from 'vitest';
15+
import { renderHook, act } from '@testing-library/react';
16+
import type React from 'react';
17+
import { useCellRequiredPrompt } from '../objectGridRequiredPrompt';
18+
19+
const FIELDS = {
20+
name: { type: 'text', label: 'Name' },
21+
stage: { type: 'select', label: 'Stage', options: [{ value: 'new', label: 'New' }, { value: 'won', label: 'Won' }] },
22+
win_reason: { type: 'text', label: 'Win Reason', requiredWhen: 'record.stage == "won"' },
23+
// No `requiredWhen`: only a refusal names it.
24+
channel: {
25+
type: 'radio',
26+
label: 'Channel',
27+
options: [{ value: 'won_option', label: 'Won option', visibleWhen: 'record.stage == "won"' }],
28+
},
29+
};
30+
const ROW = { id: 'd1', name: 'Acme deal', stage: 'new', win_reason: null, channel: null };
31+
32+
describe('#37270 the list cell dialog hands a refusal-named row the changed record', () => {
33+
it('the refusal row carries the row with the cell value merged in', () => {
34+
const { result } = renderHook(() => useCellRequiredPrompt({
35+
objectName: undefined,
36+
objectSchema: { name: 'deal', fields: FIELDS },
37+
objectFields: FIELDS,
38+
perms: undefined as never,
39+
labelOf: (f) => f,
40+
translateOptions: (_o, _f, options) => options,
41+
optionLabel: (_f, _c, fallback) => fallback,
42+
emptyText: '—',
43+
save: vi.fn(async () => {}),
44+
}));
45+
act(() => { void result.current.ask(ROW, 'stage', 'won', () => null); });
46+
const dialog = result.current.renderDialog() as React.ReactElement<{
47+
refusalField: (name: string) => { name: string; record?: Record<string, unknown> } | null;
48+
}>;
49+
expect(dialog).not.toBeNull();
50+
const row = dialog.props.refusalField('channel');
51+
expect(row?.name).toBe('channel');
52+
expect(row?.record).toEqual({ ...ROW, stage: 'won' });
53+
});
54+
});

‎ui/packages/plugin-grid/src/objectGridRequiredPrompt.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ export function useCellRequiredPrompt(inputs: CellRequiredPromptInputs): {
206206
// A field a refusal names joins the dialog when it could have been asked
207207
// for: it has an edit widget and this reader may write it.
208208
if (!hasFieldEditWidget(def.type) || !fillable(name, def)) return null;
209-
return label({ name, def: { name, ...def } });
209+
// The record the change would leave, the merge the collector judges its
210+
// rows on, so the row's options read the change (objectstack#37270).
211+
return label({ name, def: { name, ...def }, record: { ...record, [columnKey]: value } });
210212
};
211213
return (
212214
<RequiredFieldsDialog

0 commit comments

Comments
 (0)