Skip to content

Commit 9db11f1

Browse files
committed
feat(spec): an action's confirm and param dialogs can name their dismiss button — cancelText
WIP: schema key, translation slot, translateAction overlay, spec pins. Claude-Session: https://claude.ai/code/session_01ADHkmNhdF5oapRdMKNBctQ Co-authored-by: Claude <noreply@anthropic.com>
1 parent 7f4cbc9 commit 9db11f1

4 files changed

Lines changed: 328 additions & 6 deletions

File tree

‎packages/spec/src/system/i18n-resolver.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* objects.<object>._actions.<action_name>.successMessage
1818
* objects.<object>._actions.<action_name>.disabledReason
1919
* objects.<object>._actions.<action_name>.submitText
20+
* objects.<object>._actions.<action_name>.cancelText
2021
* objects.<object>._actions.<action_name>.outcomeMessages.<outcome>
2122
* objects.<object>._actions.<action_name>.params.<param_name>.label
2223
* objects.<object>._actions.<action_name>.params.<param_name>.helpText
@@ -48,7 +49,7 @@
4849
* object-less action (no `objectName`) reads only:
4950
*
5051
* globalActions.<action_name>.label / .description / .confirmText /
51-
* .successMessage / .disabledReason / .submitText /
52+
* .successMessage / .disabledReason / .submitText / .cancelText /
5253
* .outcomeMessages.<outcome> / .params.<param_name>.* / .resultDialog.*
5354
*
5455
* That is the declaration `TranslationDataSchema.globalActions` makes: global
@@ -235,6 +236,12 @@ export interface ActionLike {
235236
* label. Narrowed to `string` for the same reason `label` is.
236237
*/
237238
submitText?: string;
239+
/**
240+
* `ActionSchema.cancelText` (#28519) — the dismiss button label of the
241+
* action's confirm or param dialog. Narrowed to `string` for the same reason
242+
* `label` is.
243+
*/
244+
cancelText?: string;
238245
/**
239246
* `ActionSchema.outcomeMessages` — success copy per handler `outcome`, each
240247
* entry a `template` slot like `successMessage` (#27948).
@@ -667,7 +674,7 @@ function actionTranslationNode(
667674
function lookupActionField(
668675
bundle: TranslationBundle | undefined,
669676
action: ActionLike,
670-
field: 'label' | 'description' | 'confirmText' | 'successMessage' | 'disabledReason' | 'submitText',
677+
field: 'label' | 'description' | 'confirmText' | 'successMessage' | 'disabledReason' | 'submitText' | 'cancelText',
671678
opts?: ResolveOptions,
672679
): string | undefined {
673680
if (!bundle) return undefined;
@@ -1545,7 +1552,8 @@ export function translateView<T extends ViewLike>(
15451552
/**
15461553
* Apply the active locale to an action metadata document by overwriting
15471554
* `label`, `description`, `confirmText`, `successMessage`, `disabledReason`,
1548-
* `submitText` (for an action that declares `params`), each declared
1555+
* `submitText` (for an action that declares `params`), `cancelText` (for an
1556+
* action that declares `confirmText` or `params`), each declared
15491557
* `outcomeMessages` entry, the `params[]` copy and the `resultDialog` copy
15501558
* with translated values when available. The
15511559
* original document is not mutated; a shallow copy is returned.
@@ -1587,6 +1595,15 @@ export function translateAction<T extends ActionLike>(
15871595
const submitText = Array.isArray(action.params) && action.params.length > 0
15881596
? lookupActionField(bundle, action, 'submitText', opts) ?? action.submitText
15891597
: action.submitText;
1598+
// [#28519] The dismiss button, overlaid the same way, for an action that
1599+
// opens either dialog that carries it: the confirm dialog of a declared
1600+
// `confirmText`, or the param dialog of a non-empty `params`. Judged on the
1601+
// AUTHORED `confirmText`, the declaration `ActionSchema` judges the key
1602+
// against, so a bundle cannot give a button to a dialog that never opens.
1603+
const opensDialog = action.confirmText != null || (Array.isArray(action.params) && action.params.length > 0);
1604+
const cancelText = opensDialog
1605+
? lookupActionField(bundle, action, 'cancelText', opts) ?? action.cancelText
1606+
: action.cancelText;
15901607
const outcomeMessages = resolveActionOutcomeMessages(bundle, action, opts);
15911608
const resultDialog = resolveActionResultDialog(bundle, action, opts);
15921609
const params = translateActionParams(action, bundle, opts);
@@ -1598,6 +1615,7 @@ export function translateAction<T extends ActionLike>(
15981615
...(successMessage !== undefined ? { successMessage } : {}),
15991616
...(disabledReason !== undefined ? { disabledReason } : {}),
16001617
...(submitText !== undefined ? { submitText } : {}),
1618+
...(cancelText !== undefined ? { cancelText } : {}),
16011619
...(outcomeMessages !== action.outcomeMessages ? { outcomeMessages } : {}),
16021620
...(resultDialog !== undefined ? { resultDialog } : {}),
16031621
...(params !== action.params ? { params } : {}),

‎packages/spec/src/system/translation.zod.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ const actionTranslationSchema = (surface: string) => strictObject({
203203
disabledMessage: 'disabledReason', disabledTooltip: 'disabledReason',
204204
disabledHint: 'disabledReason', disabledText: 'disabledReason',
205205
submitLabel: 'submitText', confirmLabel: 'submitText',
206+
cancelLabel: 'cancelText',
206207
parameters: 'params', args: 'params', inputs: 'params',
207208
result: 'resultDialog', dialog: 'resultDialog',
208209
},
@@ -224,6 +225,11 @@ const actionTranslationSchema = (surface: string) => strictObject({
224225
// label; overlaid by `translateAction` like `description`, the same
225226
// dialog's line under its title, for an action that declares `params`.
226227
submitText: z.string().optional().describe('Translated label of the submit button in the action\'s param dialog (the action must declare `params`)'),
228+
// `ActionSchema.cancelText` (#28519) — the dismiss button label of the
229+
// dialog the action opens, its confirm dialog or its param dialog; overlaid
230+
// by `translateAction` like `submitText`, for an action that declares
231+
// `confirmText` or `params`.
232+
cancelText: z.string().optional().describe('Translated label of the dismiss button on the action\'s confirm or param dialog (the action must declare `confirmText` or `params`; no renderer shows it yet)'),
227233
// `ActionSchema.outcomeMessages` (#21095) — one translated message per
228234
// declared outcome, keyed by the same snake_case outcome name, so a bundle
229235
// key that could never match a handler's `outcome` is refused here rather
@@ -514,6 +520,7 @@ export const ObjectTranslationDataSchema = lazySchema(() => strictObject({
514520
* objects.<object>._actions.<action_name>.successMessage
515521
* objects.<object>._actions.<action_name>.disabledReason
516522
* objects.<object>._actions.<action_name>.submitText
523+
* objects.<object>._actions.<action_name>.cancelText
517524
* objects.<object>._actions.<action_name>.outcomeMessages.<outcome>
518525
* objects.<object>._actions.<action_name>.params.<param_name>.label
519526
* objects.<object>._actions.<action_name>.params.<param_name>.helpText
@@ -1043,6 +1050,7 @@ const appTranslationDataShape = () => ({
10431050
* globalActions.<action_name>.successMessage
10441051
* globalActions.<action_name>.disabledReason
10451052
* globalActions.<action_name>.submitText
1053+
* globalActions.<action_name>.cancelText
10461054
* globalActions.<action_name>.outcomeMessages.<outcome>
10471055
* globalActions.<action_name>.params.<param_name>.label
10481056
* globalActions.<action_name>.params.<param_name>.helpText
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
// Copyright (c) 2026 ObjectStack contributors. Apache-2.0 license.
2+
//
3+
// objectstack#28519 — `cancelText` on the action contract: the label of the
4+
// dismiss button on the dialog an action opens before it runs (its confirm
5+
// dialog, or its param dialog), in place of the dialog's default "Cancel".
6+
//
7+
// These pins hold the spec half: the parse on both host dialogs (both
8+
// I18nLabel forms), the blank refusal, the refusal on an action that opens no
9+
// dialog, the alias, and the translation slot with its `translateAction`
10+
// overlay. The dangling translation half is pinned in
11+
// `packages/lint/src/validate-translation-references.test.ts`; its twin, the
12+
// param dialog's submit label, in `action-submit-text.test.ts`.
13+
14+
import { describe, it, expect } from 'vitest';
15+
import { ActionSchema, InlineActionSchema } from './action.zod';
16+
import { ObjectTranslationDataSchema, TranslationDataSchema, type TranslationBundle } from '../system/translation.zod';
17+
import { translateAction, type ActionLike } from '../system/i18n-resolver';
18+
19+
/**
20+
* A param-LESS action that asks first — the confirm dialog's host, in the
21+
* shape of `sys_invitation`'s Cancel Invitation, the row that started this.
22+
*/
23+
const confirmHost = {
24+
name: 'cancel_invitation',
25+
label: 'Cancel Invitation',
26+
objectName: 'sys_invitation',
27+
type: 'api',
28+
target: '/api/v1/auth/organization/cancel-invitation',
29+
confirmText: 'Cancel this invitation? The recipient will no longer be able to accept it.',
30+
} as const;
31+
32+
/** An action that collects params — the param dialog's host. */
33+
const paramHost = {
34+
name: 'close_case',
35+
label: 'Close Case',
36+
objectName: 'crm_case',
37+
target: 'closeCase',
38+
params: [{ name: 'resolution', label: 'Resolution', type: 'textarea', required: true }],
39+
} as const;
40+
41+
/** An action that opens neither dialog: it runs on click. */
42+
const { confirmText: _confirmText, ...noDialog } = confirmHost;
43+
44+
const firstIssue = (input: unknown) => {
45+
const result = ActionSchema.safeParse(input);
46+
expect(result.success).toBe(false);
47+
return result.success ? undefined : result.error.issues[0];
48+
};
49+
50+
describe('ActionSchema.cancelText — the parse', () => {
51+
it('accepts a plain string on the confirm dialog\'s host and keeps it verbatim', () => {
52+
const result = ActionSchema.safeParse({ ...confirmHost, cancelText: 'Keep invitation' });
53+
54+
expect(result.success).toBe(true);
55+
expect(result.success && result.data.cancelText).toBe('Keep invitation');
56+
});
57+
58+
it('accepts it on the param dialog\'s host, beside that dialog\'s `submitText`', () => {
59+
const result = ActionSchema.safeParse({ ...paramHost, submitText: 'Close Case', cancelText: 'Keep open' });
60+
61+
expect(result.success).toBe(true);
62+
expect(result.success && result.data.cancelText).toBe('Keep open');
63+
});
64+
65+
it('accepts the inline locale map, as every I18nLabel does', () => {
66+
const localized = { en: 'Keep invitation', 'zh-CN': '保留邀请' };
67+
const result = ActionSchema.safeParse({ ...confirmHost, cancelText: localized });
68+
69+
expect(result.success).toBe(true);
70+
expect(result.success && result.data.cancelText).toEqual(localized);
71+
});
72+
73+
it('is optional — an action without it parses, and the dialog keeps "Cancel"', () => {
74+
for (const host of [confirmHost, paramHost]) {
75+
const result = ActionSchema.safeParse(host);
76+
77+
expect(result.success).toBe(true);
78+
expect(result.success && 'cancelText' in result.data).toBe(false);
79+
}
80+
});
81+
82+
it('refuses every non-I18nLabel shape the `label` key refuses', () => {
83+
for (const value of [42, null, ['a list'], { en: 7 }, { key: 'actions.keep' }] as unknown[]) {
84+
const asLabel = ActionSchema.safeParse({ ...confirmHost, label: value }).success;
85+
const asCancel = ActionSchema.safeParse({ ...confirmHost, cancelText: value }).success;
86+
87+
expect({ value, asLabel, asCancel }).toEqual({ value, asLabel: false, asCancel: false });
88+
}
89+
});
90+
});
91+
92+
describe('ActionSchema.cancelText — the refusals', () => {
93+
it('refuses the empty string and a whitespace-only one on either host, naming the default it would replace', () => {
94+
for (const host of [confirmHost, paramHost]) {
95+
for (const blank of ['', ' ']) {
96+
const issue = firstIssue({ ...host, cancelText: blank });
97+
98+
expect(issue?.code).toBe('custom');
99+
expect(issue?.path).toEqual(['cancelText']);
100+
expect(issue?.message).toMatch(/^a blank `cancelText` would replace the dialog's default dismiss button label, "Cancel"/);
101+
}
102+
}
103+
});
104+
105+
it('refuses it on an action that declares neither `confirmText` nor params — no dialog opens to show it', () => {
106+
for (const input of [{ ...noDialog, cancelText: 'Keep' }, { ...noDialog, params: [], cancelText: 'Keep' }]) {
107+
const issue = firstIssue(input);
108+
109+
expect(issue?.code).toBe('custom');
110+
expect(issue?.path).toEqual(['cancelText']);
111+
expect(issue?.message).toMatch(/^`cancelText` labels the dismiss button of the dialog an action opens before it runs/);
112+
}
113+
});
114+
115+
it('refuses the inline locale map on a dialog-less action too — the rule reads the key, not its form', () => {
116+
expect(firstIssue({ ...noDialog, cancelText: { en: 'Keep' } })?.path).toEqual(['cancelText']);
117+
});
118+
119+
it('the control: the same dialog-less action parses once the key is dropped', () => {
120+
expect(ActionSchema.safeParse(noDialog).success).toBe(true);
121+
});
122+
});
123+
124+
describe('ActionSchema.cancelText — the shape around it', () => {
125+
it('renames the measured near-miss spelling onto the key', () => {
126+
const issue = firstIssue({ ...confirmHost, cancelLabel: 'Keep invitation' });
127+
128+
expect(issue?.code).toBe('unrecognized_keys');
129+
expect(issue?.message).toContain('`cancelLabel` → `cancelText`');
130+
});
131+
132+
it('is not picked onto the INLINE action — that surface does not pick `submitText` either', () => {
133+
const result = InlineActionSchema.safeParse({
134+
type: 'api',
135+
target: '/api/v1/archive',
136+
label: 'Archive',
137+
confirmText: 'Archive this record?',
138+
cancelText: 'Keep it',
139+
});
140+
141+
expect(result.success).toBe(false);
142+
const issue = result.success ? undefined : result.error.issues[0];
143+
expect(issue?.code).toBe('unrecognized_keys');
144+
expect(issue?.message).toContain('cancelText');
145+
});
146+
});
147+
148+
describe('the translation slot and its overlay', () => {
149+
it('accepts the key at the object-scoped address, beside confirmText', () => {
150+
const result = ObjectTranslationDataSchema.safeParse({
151+
_actions: { cancel_invitation: { label: '取消邀请', confirmText: '取消此邀请?', cancelText: '保留邀请' } },
152+
});
153+
154+
expect(result.success).toBe(true);
155+
expect(result.success && result.data._actions?.cancel_invitation?.cancelText).toBe('保留邀请');
156+
});
157+
158+
it('accepts it at the globalActions address an object-less action reads', () => {
159+
const result = TranslationDataSchema.safeParse({
160+
globalActions: { close_session: { cancelText: '保持登录' } },
161+
});
162+
163+
expect(result.success).toBe(true);
164+
expect(result.success && result.data.globalActions?.close_session?.cancelText).toBe('保持登录');
165+
});
166+
167+
it('renames the same near-miss inside a bundle', () => {
168+
const result = ObjectTranslationDataSchema.safeParse({
169+
_actions: { cancel_invitation: { cancelLabel: '保留邀请' } },
170+
});
171+
172+
expect(result.success).toBe(false);
173+
const issue = result.success ? undefined : result.error.issues[0];
174+
expect(issue?.code).toBe('unrecognized_keys');
175+
expect(issue?.message).toContain('`cancelLabel` → `cancelText`');
176+
});
177+
178+
const bundle: TranslationBundle = {
179+
'zh-CN': {
180+
objects: {
181+
sys_invitation: { _actions: { cancel_invitation: { cancelText: '保留邀请' }, resend: { cancelText: '不发送' } } },
182+
crm_case: { _actions: { close_case: { cancelText: '保持打开' } } },
183+
},
184+
globalActions: { close_session: { cancelText: '保持登录' } },
185+
},
186+
};
187+
const confirmAction = {
188+
name: 'cancel_invitation',
189+
label: 'Cancel Invitation',
190+
objectName: 'sys_invitation',
191+
confirmText: 'Cancel this invitation?',
192+
cancelText: 'Keep invitation',
193+
};
194+
195+
it('translateAction overlays the translated label on the confirm dialog\'s host', () => {
196+
expect(translateAction(confirmAction, bundle, { locale: 'zh-CN' }).cancelText).toBe('保留邀请');
197+
});
198+
199+
it('overlays it on the param dialog\'s host too', () => {
200+
const closeCase = { name: 'close_case', label: 'Close Case', objectName: 'crm_case', params: [{ name: 'resolution' }] };
201+
expect(translateAction<ActionLike>(closeCase, bundle, { locale: 'zh-CN' }).cancelText).toBe('保持打开');
202+
});
203+
204+
it('reads an object-less action at globalActions', () => {
205+
const closeSession = { name: 'close_session', label: 'Close Session', confirmText: 'Sign out everywhere?', cancelText: 'Stay signed in' };
206+
expect(translateAction(closeSession, bundle, { locale: 'zh-CN' }).cancelText).toBe('保持登录');
207+
});
208+
209+
it('falls back to the authored label when the locale has none', () => {
210+
expect(translateAction(confirmAction, bundle, { locale: 'ja-JP' }).cancelText).toBe('Keep invitation');
211+
});
212+
213+
it('resolves the translated label for an action that opens a dialog but authors none', () => {
214+
const { cancelText: _authored, ...unlabelled } = confirmAction;
215+
const translated: ActionLike = translateAction<ActionLike>(unlabelled, bundle, { locale: 'zh-CN' });
216+
expect(translated.cancelText).toBe('保留邀请');
217+
});
218+
219+
it('gives no button to an action that opens no dialog, whatever the bundle carries', () => {
220+
const resend = { name: 'resend', label: 'Resend', objectName: 'sys_invitation' };
221+
expect('cancelText' in translateAction(resend, bundle, { locale: 'zh-CN' })).toBe(false);
222+
});
223+
});

0 commit comments

Comments
 (0)