@tbox.cn/app-template-agent 1.0.0-alpha.7 → 1.0.0-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitignore +7 -0
- package/.tbox/app.json +1 -1
- package/.tbox/default-modules.json +1 -1
- package/.tbox/factory/chat.json +9 -0
- package/.tbox/miniapp-containers.json +1 -1
- package/AGENTS.md +11 -8
- package/apps/client/index.html +0 -43
- package/apps/client/package.json +1 -0
- package/apps/client/scripts/acceptance-changes.test.mjs +97 -0
- package/apps/client/scripts/check-evidence.mjs +625 -108
- package/apps/client/scripts/color-scan.mjs +66 -0
- package/apps/client/scripts/color-scan.test.mjs +25 -0
- package/apps/client/scripts/evidence-gate.mjs +337 -0
- package/apps/client/scripts/evidence-gate.test.mjs +135 -0
- package/apps/client/scripts/experience-checks.mjs +163 -0
- package/apps/client/scripts/geometry-kernel.test.mjs +92 -0
- package/apps/client/scripts/in-process-checks.mjs +51 -0
- package/apps/client/scripts/lib/card-registry.mjs +81 -0
- package/apps/client/scripts/lib/card-registry.test.mjs +59 -0
- package/apps/client/scripts/lib/evidence-inputs.mjs +91 -0
- package/apps/client/scripts/lib/evidence-inputs.test.mjs +27 -0
- package/apps/client/scripts/lib/static-render.mjs +35 -0
- package/apps/client/scripts/lib/static-render.test.mjs +21 -0
- package/apps/client/scripts/probe-checks.mjs +85 -0
- package/apps/client/scripts/render-card.mjs +95 -126
- package/apps/client/scripts/sample-entries.test.mjs +31 -0
- package/apps/client/scripts/storyboard.test.mjs +149 -0
- package/apps/client/scripts/surface-css.mjs +54 -0
- package/apps/client/scripts/theme-kernel.mjs +236 -0
- package/apps/client/scripts/theme-kernel.test.mjs +136 -0
- package/apps/client/src/app.config.ts +4 -1
- package/apps/client/src/styles/brand.css +13 -14
- package/apps/client/src/styles/tailwind.css +20 -21
- package/apps/client/vite.config.ts +2 -1
- package/apps/server/scripts/probe.mjs +6 -1
- package/apps/server/scripts/scenario-lib.mjs +56 -8
- package/apps/server/scripts/scenario.mjs +41 -7
- package/config/AGENTS.md +1 -0
- package/config/chat.json +21 -9
- package/docs/agent/agent-prompt.md +1 -1
- package/docs/agent/card-action.md +5 -0
- package/docs/agent/card.md +45 -2
- package/docs/agent/kit.md +3 -3
- package/docs/agent/shell.md +1 -0
- package/docs/agent/style.md +11 -3
- package/docs/agent/tool-emits-card.md +7 -2
- package/docs/agent/tool.md +2 -0
- package/docs/agent/verify.md +59 -16
- package/package.json +21 -7
- package/packages/agent-ui-kit/AGENTS.md +1 -1
- package/packages/agent-ui-kit/README.md +21 -1
- package/packages/agent-ui-kit/catalog.json +8 -4
- package/packages/agent-ui-kit/package.json +9 -1
- package/packages/agent-ui-kit/src/._surface.tsx +0 -0
- package/packages/agent-ui-kit/src/ChatPage.tsx +70 -17
- package/packages/agent-ui-kit/src/card/._action-list-pending.test.tsx +0 -0
- package/packages/agent-ui-kit/src/card/._define-card.test.ts +0 -0
- package/packages/agent-ui-kit/src/card/._view-card-inline.test.tsx +0 -0
- package/packages/agent-ui-kit/src/card/._view-card.test.tsx +0 -0
- package/packages/agent-ui-kit/src/card/._view-card.tsx +0 -0
- package/packages/agent-ui-kit/src/card/action-list-pending.test.tsx +91 -0
- package/packages/agent-ui-kit/src/card/define-card.test.ts +25 -0
- package/packages/agent-ui-kit/src/card/define-card.ts +18 -5
- package/packages/agent-ui-kit/src/card/primitives.test.tsx +10 -0
- package/packages/agent-ui-kit/src/card/primitives.tsx +136 -37
- package/packages/agent-ui-kit/src/card/view-card-inline.test.tsx +42 -0
- package/packages/agent-ui-kit/src/card/view-card.test.tsx +79 -0
- package/packages/agent-ui-kit/src/card/view-card.tsx +250 -0
- package/packages/agent-ui-kit/src/chat-config.ts +11 -7
- package/packages/agent-ui-kit/src/components/ActionBar/DebugButton.test.tsx +3 -0
- package/packages/agent-ui-kit/src/components/ActionBar/DebugButton.tsx +2 -7
- package/packages/agent-ui-kit/src/components/BottomSheet/._index.test.tsx +0 -0
- package/packages/agent-ui-kit/src/components/BottomSheet/._index.tsx +0 -0
- package/packages/agent-ui-kit/src/components/BottomSheet/index.test.tsx +90 -0
- package/packages/agent-ui-kit/src/components/BottomSheet/index.tsx +129 -0
- package/packages/agent-ui-kit/src/components/ChatInput/index.test.tsx +2 -2
- package/packages/agent-ui-kit/src/components/ChatInput/index.tsx +22 -17
- package/packages/agent-ui-kit/src/components/HistoryEntry/index.tsx +3 -2
- package/packages/agent-ui-kit/src/components/MessageBubble/index.tsx +11 -5
- package/packages/agent-ui-kit/src/components/QuickCommands/index.test.tsx +3 -1
- package/packages/agent-ui-kit/src/components/QuickCommands/index.tsx +31 -8
- package/packages/agent-ui-kit/src/components/ScrollToBottomButton/index.tsx +7 -3
- package/packages/agent-ui-kit/src/components/ToolCallBlock/index.tsx +4 -2
- package/packages/agent-ui-kit/src/index.ts +8 -0
- package/packages/agent-ui-kit/src/preview-provider.tsx +47 -0
- package/packages/agent-ui-kit/src/shell.contract.json +9 -0
- package/packages/agent-ui-kit/src/shell.test.ts +10 -0
- package/packages/agent-ui-kit/src/shell.ts +15 -0
- package/packages/agent-ui-kit/src/surface.test.tsx +35 -0
- package/packages/agent-ui-kit/src/surface.tsx +44 -0
- package/packages/agent-ui-kit/src/theme/base.css +9 -11
- package/packages/agent-ui-kit/src/theme/contract.json +123 -2
- package/packages/agent-ui-kit/src/theme/theme-contract.test.ts +16 -0
- package/packages/agent-ui-kit/src/theme/tokens.css +9 -12
- package/pnpm-lock.template.yaml +97 -91
- package/pnpm-workspace.template.yaml +10 -10
- package/profiles/mall/._chat.overrides.json +0 -0
- package/profiles/mall/chat.overrides.json +12 -0
- package/profiles/mall/config/chat.json +14 -6
- package/profiles/mall.json +1 -1
- package/scripts/._assert-pack.mjs +0 -0
- package/scripts/._deliver.mjs +0 -0
- package/scripts/._shell-probe.mjs +0 -0
- package/scripts/._storyboard.mjs +0 -0
- package/scripts/assert-pack.mjs +48 -0
- package/scripts/check-css-target.mjs +6 -3
- package/scripts/check-fast.mjs +1 -0
- package/scripts/checks.json +400 -26
- package/scripts/deliver.mjs +183 -0
- package/scripts/lib/._chrome-pipe.mjs +0 -0
- package/scripts/lib/._geometry-kernel.mjs +0 -0
- package/scripts/lib/chrome-pipe.mjs +95 -0
- package/scripts/lib/geometry-kernel.mjs +137 -0
- package/scripts/shell-probe.mjs +283 -0
- package/scripts/storyboard.mjs +350 -0
- package/.tbox/card-render/baseline/demo.png +0 -0
- package/.tbox/card-render/demo.html +0 -342
- package/.tbox/card-render/demo.png +0 -0
- package/.tbox/card-render/scale-demo.audit.html +0 -984
- package/.tbox/card-render/scale-demo.html +0 -939
- package/.tbox/evidence/cards.txt +0 -1
- package/.tbox/evidence/gate.json +0 -1
- package/.tbox/evidence/last-gate.json +0 -1
- package/.tbox/evidence/scenarios.json +0 -1
- package/apps/client/.tbox-gen/app-registry.ts +0 -3
- package/apps/client/.tbox-gen/documents/_tbox/card-preview.html +0 -62
- package/apps/client/.tbox-gen/documents/index.html +0 -63
- package/apps/client/.tbox-gen/entries/card-preview.tsx +0 -6
- package/apps/client/.tbox-gen/entries/index.tsx +0 -11
- package/apps/client/.tbox-gen/manifest.json +0 -17
- package/apps/client/.tbox-gen/registry.client.ts +0 -6
- package/apps/client/.tbox-gen/registry.server.ts +0 -15
- package/apps/client/STYLE_GUIDE.md +0 -48
- package/apps/client/VISUAL_RESTORE.md +0 -90
- package/profiles/mall/config/llm_provider.json +0 -5
- package/profiles/mall/config/plugins.json +0 -3
- package/scripts/compat-probe.mjs +0 -108
- package/scripts/gen-tailwind-sources.mjs +0 -55
- /package/.tbox/{card-render/._demo.html → factory/._chat.json} +0 -0
- /package/{.tbox/card-render/._demo.png → apps/client/scripts/._acceptance-changes.test.mjs} +0 -0
- /package/{.tbox/card-render/._scale-demo.audit.html → apps/client/scripts/._color-scan.mjs} +0 -0
- /package/{.tbox/card-render/._scale-demo.html → apps/client/scripts/._color-scan.test.mjs} +0 -0
- /package/{.tbox/card-render/baseline/._demo.png → apps/client/scripts/._evidence-gate.mjs} +0 -0
- /package/{.tbox/evidence/._cards.txt → apps/client/scripts/._evidence-gate.test.mjs} +0 -0
- /package/{.tbox/evidence/._gate.json → apps/client/scripts/._experience-checks.mjs} +0 -0
- /package/{.tbox/evidence/._last-gate.json → apps/client/scripts/._geometry-kernel.test.mjs} +0 -0
- /package/{.tbox/evidence/._scenarios.json → apps/client/scripts/._in-process-checks.mjs} +0 -0
- /package/apps/client/{._STYLE_GUIDE.md → scripts/._probe-checks.mjs} +0 -0
- /package/apps/client/{._VISUAL_RESTORE.md → scripts/._sample-entries.test.mjs} +0 -0
- /package/apps/client/{.tbox-gen/._app-registry.ts → scripts/._storyboard.test.mjs} +0 -0
- /package/apps/client/{.tbox-gen/._manifest.json → scripts/._surface-css.mjs} +0 -0
- /package/apps/client/{.tbox-gen/._registry.client.ts → scripts/._theme-kernel.mjs} +0 -0
- /package/apps/client/{.tbox-gen/._registry.server.ts → scripts/._theme-kernel.test.mjs} +0 -0
- /package/apps/client/{.tbox-gen/documents/._index.html → scripts/lib/._card-registry.mjs} +0 -0
- /package/apps/client/{.tbox-gen/documents/_tbox/._card-preview.html → scripts/lib/._card-registry.test.mjs} +0 -0
- /package/apps/client/{.tbox-gen/entries/._card-preview.tsx → scripts/lib/._evidence-inputs.mjs} +0 -0
- /package/apps/client/{.tbox-gen/entries/._index.tsx → scripts/lib/._evidence-inputs.test.mjs} +0 -0
- /package/{profiles/mall/config/._llm_provider.json → apps/client/scripts/lib/._static-render.mjs} +0 -0
- /package/{profiles/mall/config/._plugins.json → apps/client/scripts/lib/._static-render.test.mjs} +0 -0
- /package/{scripts/._compat-probe.mjs → packages/agent-ui-kit/src/._preview-provider.tsx} +0 -0
- /package/{scripts/._gen-tailwind-sources.mjs → packages/agent-ui-kit/src/._surface.test.tsx} +0 -0
|
@@ -5,6 +5,10 @@
|
|
|
5
5
|
* - app-build 扫描器要求 `export const card = defineCard({ type: '…' }, …)` 的首参是对象字面量;
|
|
6
6
|
* - 生成 registry 把导出值直接当 `CardComponent` 注册,所以运行期必须是组件且带 `.type`。
|
|
7
7
|
* 手写 Object.assign 每张卡重复一遍形态,且容易漏掉 `.type`——本工厂是唯一写法。
|
|
8
|
+
*
|
|
9
|
+
* 动作所有权:默认 `auto`——SDK 用服务端下发的按钮条渲染动作,保持旧卡兼容;
|
|
10
|
+
* 卡片自己渲染动作(`ActionList` / `ViewCard`)时显式 `actions: 'self'`,SDK 不再叠加
|
|
11
|
+
*(确认弹层、入参、历史禁用都由卡内实现)。视图卡自动派生时由生成器写 `self`。
|
|
8
12
|
*/
|
|
9
13
|
import type { ComponentType } from 'react';
|
|
10
14
|
import type { CardComponent } from '@tbox.cn/app-contracts';
|
|
@@ -17,18 +21,27 @@ export interface CardRenderProps<T> {
|
|
|
17
21
|
actionTokens?: Record<string, string>;
|
|
18
22
|
}
|
|
19
23
|
|
|
20
|
-
/** `defineCard` 的返回类型:既是可注册的卡片组件,也带静态 `type
|
|
21
|
-
export type DefinedCard<T> = CardComponent<T> & {
|
|
24
|
+
/** `defineCard` 的返回类型:既是可注册的卡片组件,也带静态 `type` 与动作所有权标记。 */
|
|
25
|
+
export type DefinedCard<T> = CardComponent<T> & {
|
|
26
|
+
type: string;
|
|
27
|
+
selfRendersActions: boolean;
|
|
28
|
+
/** 动作所有权来源:default = 未显式声明(SDK 默认条),auto / self = 显式 */
|
|
29
|
+
actionsMode: 'default' | 'auto' | 'self';
|
|
30
|
+
};
|
|
22
31
|
|
|
23
32
|
/**
|
|
24
33
|
* 声明一张应用级卡片。
|
|
25
34
|
*
|
|
26
|
-
* @param meta 卡片类型声明;`type` 必须与同 base 的服务端 meta 的 `cardType`
|
|
35
|
+
* @param meta 卡片类型声明;`type` 必须与同 base 的服务端 meta 的 `cardType` 一致。
|
|
36
|
+
* `actions` 缺省 `'auto'`(SDK 默认按钮条,兼容旧卡);自己渲染动作的卡写 `'self'`,
|
|
37
|
+
* 否则会出现两条按钮或动作被禁用。
|
|
27
38
|
* @param Component 渲染组件,入参为 `CardRenderProps`
|
|
28
39
|
*/
|
|
29
40
|
export function defineCard<T>(
|
|
30
|
-
meta: { type: string },
|
|
41
|
+
meta: { type: string; actions?: 'self' | 'auto' },
|
|
31
42
|
Component: ComponentType<CardRenderProps<T>>,
|
|
32
43
|
): DefinedCard<T> {
|
|
33
|
-
|
|
44
|
+
const selfRendersActions = meta.actions === 'self';
|
|
45
|
+
const actionsMode = meta.actions ?? 'default';
|
|
46
|
+
return Object.assign(Component, { type: meta.type, selfRendersActions, actionsMode }) as unknown as DefinedCard<T>;
|
|
34
47
|
}
|
|
@@ -89,6 +89,16 @@ describe('第二版基元', () => {
|
|
|
89
89
|
);
|
|
90
90
|
expect(markup).toContain('tbox-hero');
|
|
91
91
|
expect(markup).toContain('我的积分');
|
|
92
|
+
// 深底主视觉:底用 primary-dark、字用 primary-foreground,全部走令牌(无字面量色值)。
|
|
93
|
+
const inverse = html(<Hero eyebrow="ROUTE" title="导航" value={8} unit="分钟" tone="inverse" />);
|
|
94
|
+
expect(inverse).toContain('tbox-hero--inverse');
|
|
95
|
+
expect(inverse).toContain('bg-primary-dark');
|
|
96
|
+
expect(inverse).toContain('text-primary-foreground');
|
|
97
|
+
expect(inverse).not.toMatch(/#[0-9a-fA-F]{3,8}\b|rgba?\(/);
|
|
98
|
+
// 未知 tone(旧数据 / 新 kit 错配)回落 primary,不拼空类名。
|
|
99
|
+
const unknown = html(<Hero title="兜底" tone={'legacy' as never} />);
|
|
100
|
+
expect(unknown).toContain('tbox-hero--legacy');
|
|
101
|
+
expect(unknown).toContain('from-primary-foreground');
|
|
92
102
|
expect(markup).toContain('tbox-stats__value');
|
|
93
103
|
expect(markup).toContain('tbox-timeline__item--gain');
|
|
94
104
|
expect(markup).toContain('+100');
|
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
*
|
|
4
4
|
* 设计约束:
|
|
5
5
|
* - 全部走主题 token(`--tbox-*`),换主题自动跟随;不写死品牌色;
|
|
6
|
-
* -
|
|
6
|
+
* - 尺寸与字号一律物理 px(没有 rem 换算,也没有动态根字号);
|
|
7
7
|
* - 不引入新的交互语义:按钮一律由调用方决定 `onClick`,禁用由 `useCardActions().disabled` 统一控制。
|
|
8
8
|
*
|
|
9
9
|
* 信息类卡片(明细、票据、状态、路线)用这些积木拼即可,不必自写结构与样式;
|
|
10
10
|
* 需要完全自定义布局时再写组件,规则见 `docs/agent/style.md`。
|
|
11
11
|
*/
|
|
12
|
-
import { useState } from 'react';
|
|
12
|
+
import { useEffect, useRef, useState } from 'react';
|
|
13
13
|
import type { ReactNode } from 'react';
|
|
14
14
|
import type { CardActions } from './use-card-actions';
|
|
15
15
|
|
|
@@ -18,18 +18,18 @@ export type Tone = 'neutral' | 'info' | 'success' | 'warning' | 'error';
|
|
|
18
18
|
/** 状态色:中性走契约语义色,其余是旧 less 里的固定色值(透明度底 + 深色字) */
|
|
19
19
|
const TAG_TONE: Record<Tone, string> = {
|
|
20
20
|
neutral: 'bg-muted text-secondary-foreground',
|
|
21
|
-
info: 'bg-
|
|
22
|
-
success: 'bg-
|
|
23
|
-
warning: 'bg-
|
|
24
|
-
error: 'bg-
|
|
21
|
+
info: 'bg-primary-soft text-primary',
|
|
22
|
+
success: 'bg-success-soft text-success',
|
|
23
|
+
warning: 'bg-warning-soft text-warn-ink',
|
|
24
|
+
error: 'bg-danger-soft text-destructive',
|
|
25
25
|
};
|
|
26
26
|
|
|
27
27
|
const NOTICE_TONE: Record<Tone, string> = {
|
|
28
28
|
neutral: 'bg-muted',
|
|
29
|
-
info: 'bg-
|
|
30
|
-
success: 'bg-
|
|
31
|
-
warning: 'bg-
|
|
32
|
-
error: 'bg-
|
|
29
|
+
info: 'bg-primary-soft',
|
|
30
|
+
success: 'bg-success-soft',
|
|
31
|
+
warning: 'bg-warning-soft',
|
|
32
|
+
error: 'bg-danger-soft',
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
export interface CardProps {
|
|
@@ -191,9 +191,9 @@ export function ActionRow({ primary, secondary, disabled }: ActionRowProps) {
|
|
|
191
191
|
* @behavior primary 撑满整行;secondary 与 link 宽度随内容;禁用态不触发点击。
|
|
192
192
|
*/
|
|
193
193
|
const BUTTON_VARIANT = {
|
|
194
|
-
primary: 'w-full bg-primary px-
|
|
195
|
-
secondary: 'w-
|
|
196
|
-
link: 'w-auto bg-transparent px-
|
|
194
|
+
primary: 'w-full bg-primary px-4 py-2.5 text-sm text-white',
|
|
195
|
+
secondary: 'w-full border border-border bg-transparent px-4 py-2.5 text-sm text-primary',
|
|
196
|
+
link: 'w-auto bg-transparent px-1 py-1 text-sm text-primary',
|
|
197
197
|
} as const;
|
|
198
198
|
|
|
199
199
|
export function CardButton({
|
|
@@ -201,18 +201,22 @@ export function CardButton({
|
|
|
201
201
|
onClick,
|
|
202
202
|
variant = 'primary',
|
|
203
203
|
disabled,
|
|
204
|
+
busy,
|
|
204
205
|
}: {
|
|
205
206
|
children: ReactNode;
|
|
206
207
|
onClick?: () => void;
|
|
207
208
|
variant?: 'primary' | 'secondary' | 'link';
|
|
208
209
|
disabled?: boolean;
|
|
210
|
+
/** 动作进行中:只影响可访问性语义,视觉由调用方给 spinner */
|
|
211
|
+
busy?: boolean;
|
|
209
212
|
}) {
|
|
210
213
|
return (
|
|
211
214
|
<button
|
|
212
|
-
className={`tbox-btn tbox-btn--${variant} inline-flex cursor-pointer items-center justify-center rounded-full border-0 leading-tight disabled:cursor-not-allowed ${BUTTON_VARIANT[variant]}`}
|
|
215
|
+
className={`tbox-btn tbox-btn--${variant} inline-flex min-h-11 cursor-pointer items-center justify-center rounded-full border-0 leading-tight outline-offset-2 focus-visible:outline-2 focus-visible:outline-primary disabled:cursor-not-allowed disabled:opacity-55 ${BUTTON_VARIANT[variant]}`}
|
|
213
216
|
type="button"
|
|
214
217
|
onClick={onClick}
|
|
215
218
|
disabled={disabled}
|
|
219
|
+
aria-busy={busy || undefined}
|
|
216
220
|
>
|
|
217
221
|
{children}
|
|
218
222
|
</button>
|
|
@@ -239,22 +243,40 @@ export function ConfirmSheet({
|
|
|
239
243
|
onConfirm,
|
|
240
244
|
onCancel,
|
|
241
245
|
}: ConfirmSheetProps) {
|
|
246
|
+
useEffect(() => {
|
|
247
|
+
if (!open) return undefined;
|
|
248
|
+
const onKey = (event: KeyboardEvent) => {
|
|
249
|
+
if (event.key === 'Escape') onCancel();
|
|
250
|
+
};
|
|
251
|
+
document.addEventListener('keydown', onKey);
|
|
252
|
+
return () => document.removeEventListener('keydown', onKey);
|
|
253
|
+
}, [open, onCancel]);
|
|
242
254
|
if (!open) return null;
|
|
243
|
-
|
|
244
|
-
|
|
255
|
+
// 就地渲染 fixed 弹层:卡框已不再用 filter(filter 会给 fixed 子元素造新的定位基准),
|
|
256
|
+
// 因此弹层相对视口定位;保留 SSR 可渲染(证据包用 renderToStaticMarkup 断言内容)。
|
|
257
|
+
const sheet = (
|
|
258
|
+
<div
|
|
259
|
+
className="tbox-sheet fixed inset-0 z-50"
|
|
260
|
+
role="dialog"
|
|
261
|
+
aria-modal="true"
|
|
262
|
+
aria-label={typeof title === 'string' ? title : undefined}
|
|
263
|
+
>
|
|
245
264
|
<div className="tbox-sheet__mask absolute inset-0 bg-scrim" onClick={onCancel} />
|
|
246
|
-
<div className="tbox-sheet__panel absolute inset-x-0 bottom-0 flex flex-col gap-2.5 rounded-t-xl bg-card px-4 pb-
|
|
265
|
+
<div className="tbox-sheet__panel absolute inset-x-0 bottom-0 flex flex-col gap-2.5 rounded-t-xl bg-card px-4 pb-[calc(20px+env(safe-area-inset-bottom))] pt-4">
|
|
247
266
|
<h5 className="tbox-sheet__title m-0 text-base">{title}</h5>
|
|
248
|
-
{description ? <p className="tbox-sheet__desc m-0 text-
|
|
249
|
-
<div className="tbox-sheet__actions
|
|
267
|
+
{description ? <p className="tbox-sheet__desc m-0 text-sm text-secondary-foreground">{description}</p> : null}
|
|
268
|
+
<div className="tbox-sheet__actions mt-1 grid grid-cols-2 gap-2">
|
|
250
269
|
<CardButton variant="secondary" onClick={onCancel}>
|
|
251
|
-
{cancelText}
|
|
270
|
+
<span className="whitespace-nowrap">{cancelText}</span>
|
|
271
|
+
</CardButton>
|
|
272
|
+
<CardButton onClick={onConfirm}>
|
|
273
|
+
<span className="whitespace-nowrap">{confirmText}</span>
|
|
252
274
|
</CardButton>
|
|
253
|
-
<CardButton onClick={onConfirm}>{confirmText}</CardButton>
|
|
254
275
|
</div>
|
|
255
276
|
</div>
|
|
256
277
|
</div>
|
|
257
278
|
);
|
|
279
|
+
return sheet;
|
|
258
280
|
}
|
|
259
281
|
|
|
260
282
|
const TICKET_TONE = {
|
|
@@ -266,6 +288,8 @@ const HERO_TONE = {
|
|
|
266
288
|
primary: 'bg-linear-to-br from-primary-foreground to-card',
|
|
267
289
|
warm: 'bg-linear-to-br from-warm to-card',
|
|
268
290
|
jade: 'bg-linear-to-br from-jade to-card',
|
|
291
|
+
// 深色底:只用契约里的令牌(primary-dark / primary-light),不引入字面量色值。
|
|
292
|
+
inverse: 'bg-primary-dark text-primary-foreground [&_.tbox-hero__eyebrow]:text-primary-foreground [&_.tbox-hero__footnote]:text-primary-foreground [&_.tbox-hero__value_small]:text-primary-foreground',
|
|
269
293
|
neutral: 'bg-muted',
|
|
270
294
|
} as const;
|
|
271
295
|
|
|
@@ -360,13 +384,15 @@ export interface HeroProps {
|
|
|
360
384
|
/** 数值下方一句说明 */
|
|
361
385
|
footnote?: ReactNode;
|
|
362
386
|
icon?: ReactNode;
|
|
363
|
-
tone?: 'primary' | 'warm' | 'jade' | 'neutral';
|
|
387
|
+
tone?: 'primary' | 'warm' | 'jade' | 'inverse' | 'neutral';
|
|
364
388
|
}
|
|
365
389
|
|
|
366
390
|
/** 品牌色块:主数值 + 说明,用于会员、积分、钱包类卡片的头部。 */
|
|
367
391
|
export function Hero({ eyebrow, title, value, unit, footnote, icon, tone = 'primary' }: HeroProps) {
|
|
392
|
+
// 旧 kit 与新数据错配时(未知 tone)回落 primary,不拼出空类名。
|
|
393
|
+
const toneClass = HERO_TONE[tone] ?? HERO_TONE.primary;
|
|
368
394
|
return (
|
|
369
|
-
<div className={`tbox-hero tbox-hero--${tone} flex items-center gap-2 rounded-md p-3.5 pb-3.25 ${
|
|
395
|
+
<div className={`tbox-hero tbox-hero--${tone} flex items-center gap-2 rounded-md p-3.5 pb-3.25 ${toneClass}`}>
|
|
370
396
|
<div className="tbox-hero__text flex min-w-0 flex-1 flex-col gap-1.5">
|
|
371
397
|
{eyebrow ? <span className="tbox-hero__eyebrow text-xs tracking-wide text-muted-foreground">{eyebrow}</span> : null}
|
|
372
398
|
<span className="tbox-hero__title text-sm font-semibold">{title}</span>
|
|
@@ -575,11 +601,47 @@ export interface CardActionSpec {
|
|
|
575
601
|
*
|
|
576
602
|
* 用法:`const api = useCardActions({ cardId, isHistory, actionTokens }); <ActionList api={api} actions={[...]} />`
|
|
577
603
|
*
|
|
578
|
-
* @behavior
|
|
604
|
+
* @behavior 首个动作为整行主按钮,其余并排一行;confirm 走挂到 body 的 ConfirmSheet;disabled 自动接 api.disabled。
|
|
579
605
|
*/
|
|
606
|
+
const PENDING_TIMEOUT_MS = 20000;
|
|
607
|
+
|
|
580
608
|
export function ActionList({ actions, api }: { actions: CardActionSpec[]; api: CardActions }) {
|
|
581
|
-
const [
|
|
582
|
-
const
|
|
609
|
+
const [confirmIndex, setConfirmIndex] = useState<number | null>(null);
|
|
610
|
+
const [running, setRunning] = useState<number | null>(null);
|
|
611
|
+
// 单飞用 ref 同步判重:state 要等重渲染,连点两下会都通过。
|
|
612
|
+
const runningRef = useRef<number | null>(null);
|
|
613
|
+
const wasGenerating = useRef(false);
|
|
614
|
+
|
|
615
|
+
// 兜底解除:没有回执(方案 A 不改协议)时,按钮不能永远转圈。
|
|
616
|
+
useEffect(() => {
|
|
617
|
+
if (running === null) return undefined;
|
|
618
|
+
const timer = setTimeout(() => {
|
|
619
|
+
runningRef.current = null;
|
|
620
|
+
setRunning(null);
|
|
621
|
+
}, PENDING_TIMEOUT_MS);
|
|
622
|
+
return () => clearTimeout(timer);
|
|
623
|
+
}, [running]);
|
|
624
|
+
|
|
625
|
+
// 生成态从 true 回到 false = 这一轮有结果,解除 pending。
|
|
626
|
+
// 无论点击发生在生成前还是生成后,标记都要在当前这一轮结束时复位。
|
|
627
|
+
useEffect(() => {
|
|
628
|
+
if (api.isGenerating) {
|
|
629
|
+
wasGenerating.current = true;
|
|
630
|
+
return;
|
|
631
|
+
}
|
|
632
|
+
if (!wasGenerating.current) return;
|
|
633
|
+
wasGenerating.current = false;
|
|
634
|
+
if (runningRef.current !== null) {
|
|
635
|
+
runningRef.current = null;
|
|
636
|
+
setRunning(null);
|
|
637
|
+
}
|
|
638
|
+
}, [api.isGenerating]);
|
|
639
|
+
|
|
640
|
+
const run = (spec: CardActionSpec, index: number) => {
|
|
641
|
+
// 没有 send / invoke 的动作不进入 pending,否则只会空转。
|
|
642
|
+
if (!spec.invoke && !spec.send) return;
|
|
643
|
+
runningRef.current = index;
|
|
644
|
+
setRunning(index);
|
|
583
645
|
if (spec.invoke) {
|
|
584
646
|
api.invoke(spec.invoke.id, spec.invoke.input);
|
|
585
647
|
return;
|
|
@@ -590,22 +652,58 @@ export function ActionList({ actions, api }: { actions: CardActionSpec[]; api: C
|
|
|
590
652
|
}
|
|
591
653
|
};
|
|
592
654
|
const trigger = (spec: CardActionSpec, index: number) => {
|
|
593
|
-
if (api.disabled) return;
|
|
655
|
+
if (api.disabled || runningRef.current !== null || confirmIndex !== null) return;
|
|
594
656
|
if (spec.confirm) {
|
|
595
|
-
|
|
657
|
+
setConfirmIndex(index);
|
|
596
658
|
return;
|
|
597
659
|
}
|
|
598
|
-
run(spec);
|
|
660
|
+
run(spec, index);
|
|
599
661
|
};
|
|
600
|
-
const pendingSpec =
|
|
662
|
+
const pendingSpec = confirmIndex === null ? undefined : actions[confirmIndex];
|
|
663
|
+
const busy = api.disabled || running !== null || confirmIndex !== null;
|
|
664
|
+
const label = (text: string, index: number) => (
|
|
665
|
+
<span className="inline-flex items-center gap-2">
|
|
666
|
+
{running === index ? (
|
|
667
|
+
<span
|
|
668
|
+
className="tbox-btn__spinner inline-block size-4 animate-spin rounded-full border-2 border-current border-t-transparent"
|
|
669
|
+
aria-hidden="true"
|
|
670
|
+
/>
|
|
671
|
+
) : null}
|
|
672
|
+
{text}
|
|
673
|
+
</span>
|
|
674
|
+
);
|
|
601
675
|
return (
|
|
602
676
|
<>
|
|
603
677
|
<div className="tbox-action-list mt-2.5 flex flex-col gap-2">
|
|
604
|
-
{actions
|
|
605
|
-
<CardButton
|
|
606
|
-
{
|
|
678
|
+
{actions[0] ? (
|
|
679
|
+
<CardButton
|
|
680
|
+
variant={actions[0].style ?? 'primary'}
|
|
681
|
+
disabled={busy}
|
|
682
|
+
busy={running === 0}
|
|
683
|
+
onClick={() => trigger(actions[0], 0)}
|
|
684
|
+
>
|
|
685
|
+
{label(actions[0].label, 0)}
|
|
607
686
|
</CardButton>
|
|
608
|
-
)
|
|
687
|
+
) : null}
|
|
688
|
+
{actions.length > 1 ? (
|
|
689
|
+
<div className="tbox-action-list__row flex gap-2">
|
|
690
|
+
{actions.slice(1).map((spec, index) => {
|
|
691
|
+
const actionIndex = index + 1;
|
|
692
|
+
return (
|
|
693
|
+
<div key={spec.label + '-' + actionIndex} className="min-w-0 flex-1">
|
|
694
|
+
<CardButton
|
|
695
|
+
variant={spec.style ?? 'secondary'}
|
|
696
|
+
disabled={busy}
|
|
697
|
+
busy={running === actionIndex}
|
|
698
|
+
onClick={() => trigger(spec, actionIndex)}
|
|
699
|
+
>
|
|
700
|
+
{label(spec.label, actionIndex)}
|
|
701
|
+
</CardButton>
|
|
702
|
+
</div>
|
|
703
|
+
);
|
|
704
|
+
})}
|
|
705
|
+
</div>
|
|
706
|
+
) : null}
|
|
609
707
|
</div>
|
|
610
708
|
{pendingSpec?.confirm ? (
|
|
611
709
|
<ConfirmSheet
|
|
@@ -616,10 +714,11 @@ export function ActionList({ actions, api }: { actions: CardActionSpec[]; api: C
|
|
|
616
714
|
{...(pendingSpec.confirm.cancelText ? { cancelText: pendingSpec.confirm.cancelText } : {})}
|
|
617
715
|
onConfirm={() => {
|
|
618
716
|
const spec = pendingSpec;
|
|
619
|
-
|
|
620
|
-
|
|
717
|
+
const index = confirmIndex ?? 0;
|
|
718
|
+
setConfirmIndex(null);
|
|
719
|
+
run(spec, index);
|
|
621
720
|
}}
|
|
622
|
-
onCancel={() =>
|
|
721
|
+
onCancel={() => setConfirmIndex(null)}
|
|
623
722
|
/>
|
|
624
723
|
) : null}
|
|
625
724
|
</>
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// @vitest-environment happy-dom
|
|
2
|
+
import { act } from 'react';
|
|
3
|
+
import { createRoot } from 'react-dom/client';
|
|
4
|
+
import { afterEach, describe, expect, it } from 'vitest';
|
|
5
|
+
import type { CardView } from '@tbox.cn/app-contracts';
|
|
6
|
+
import { createSurfaceHarness } from '../surface';
|
|
7
|
+
import { ViewCard } from './view-card';
|
|
8
|
+
|
|
9
|
+
const view: CardView = {
|
|
10
|
+
header: { title: '活动' },
|
|
11
|
+
blocks: [
|
|
12
|
+
{
|
|
13
|
+
kind: 'list',
|
|
14
|
+
items: [{ title: '亲子手作', action: { label: '报名', invoke: { id: 'register' } } }],
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const roots: ReturnType<typeof createRoot>[] = [];
|
|
20
|
+
afterEach(async () => {
|
|
21
|
+
await act(async () => {
|
|
22
|
+
for (const root of roots) root.unmount();
|
|
23
|
+
});
|
|
24
|
+
roots.length = 0;
|
|
25
|
+
document.body.innerHTML = '';
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe('ViewCard 列表内联动作', () => {
|
|
29
|
+
it('点击后进入单飞状态,按钮禁用', async () => {
|
|
30
|
+
const harness = createSurfaceHarness({});
|
|
31
|
+
const el = document.createElement('div');
|
|
32
|
+
document.body.appendChild(el);
|
|
33
|
+
const root = createRoot(el);
|
|
34
|
+
roots.push(root);
|
|
35
|
+
act(() => root.render(harness.wrap(<ViewCard view={view} cardId="c1" actionTokens={{ register: 'token' }} />)));
|
|
36
|
+
const button = [...el.querySelectorAll('button')].find((item) => item.textContent?.includes('报名'))!;
|
|
37
|
+
await act(async () => {
|
|
38
|
+
button.click();
|
|
39
|
+
});
|
|
40
|
+
expect(el.querySelector('button')!.disabled).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { createElement } from 'react';
|
|
3
|
+
import { renderToStaticMarkup } from 'react-dom/server';
|
|
4
|
+
import type { CardView } from '@tbox.cn/app-contracts';
|
|
5
|
+
import { createSurfaceHarness } from '../surface';
|
|
6
|
+
import { ViewCard } from './view-card';
|
|
7
|
+
|
|
8
|
+
const parking: CardView = {
|
|
9
|
+
header: { title: '停车订单', eyebrow: 'PARKING', status: { text: '待支付', tone: 'warning' } },
|
|
10
|
+
blocks: [
|
|
11
|
+
{ kind: 'facts', items: [['车牌', '浙A·12345'], ['停车时长', '2 小时 20 分钟']] },
|
|
12
|
+
{ kind: 'bill', lines: [['停车费', '¥15'], ['停车券', '-¥10', true]], total: ['应付', '¥5'] },
|
|
13
|
+
{ kind: 'notice', text: '已为你选好停车券', tone: 'info' },
|
|
14
|
+
],
|
|
15
|
+
actions: [
|
|
16
|
+
{ label: '确认支付 ¥5', invoke: { id: 'pay-parking', input: { plate: '浙A·12345' } }, confirm: { title: '确认支付?' } },
|
|
17
|
+
{ label: '用积分换停车券', send: '用积分兑换停车券' },
|
|
18
|
+
],
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const activity: CardView = {
|
|
22
|
+
header: { title: '近期活动', status: { text: '2 个推荐', tone: 'info' } },
|
|
23
|
+
blocks: [
|
|
24
|
+
{
|
|
25
|
+
kind: 'list',
|
|
26
|
+
items: [
|
|
27
|
+
{ title: '亲子手作', meta: '今天 15:00 · L2 连廊', tags: ['亲子'], description: '剩 8 组名额', action: { label: '报名', invoke: { id: 'register' } } },
|
|
28
|
+
{ title: '积分加倍', meta: '今天 10:00 · L1 中庭' },
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
actions: [{ label: '看第一个详情', send: '第一个活动的详情', style: 'secondary' }],
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const harness = createSurfaceHarness({});
|
|
36
|
+
const render = (view: CardView, isHistory = false) =>
|
|
37
|
+
renderToStaticMarkup(harness.wrap(createElement(ViewCard, { view, cardId: 'c1', isHistory })));
|
|
38
|
+
|
|
39
|
+
describe('ViewCard', () => {
|
|
40
|
+
it('积木与动作渲染一次,标题/明细/提示都在', () => {
|
|
41
|
+
const html = render(parking);
|
|
42
|
+
expect(html).toContain('停车订单');
|
|
43
|
+
expect(html).toContain('浙A·12345');
|
|
44
|
+
expect(html).toContain('应付');
|
|
45
|
+
expect(html).toContain('已为你选好停车券');
|
|
46
|
+
expect(html.match(/确认支付 ¥5/g)?.length).toBe(1);
|
|
47
|
+
expect(html.match(/用积分换停车券/g)?.length).toBe(1);
|
|
48
|
+
expect(html).toContain('tbox-btn');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('列表积木渲染实体与内联动作', () => {
|
|
52
|
+
const html = render(activity);
|
|
53
|
+
expect(html).toContain('亲子手作');
|
|
54
|
+
expect(html).toContain('报名');
|
|
55
|
+
expect(html).toContain('2 个推荐');
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('渲染前归一化:超预算积木只渲染前 4 个', () => {
|
|
59
|
+
const over = {
|
|
60
|
+
header: { title: '超预算' },
|
|
61
|
+
blocks: Array.from({ length: 5 }, (_, index) => ({ kind: 'tags' as const, tags: ['tag-' + index] })),
|
|
62
|
+
} as unknown as CardView;
|
|
63
|
+
const html = render(over);
|
|
64
|
+
expect(html).toContain('tag-0');
|
|
65
|
+
expect(html).toContain('tag-3');
|
|
66
|
+
expect(html).not.toContain('tag-4');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('坏数据回退为可读提示,不抛错', () => {
|
|
70
|
+
const broken = { header: { title: '坏卡' }, blocks: null } as unknown as CardView;
|
|
71
|
+
const html = render(broken);
|
|
72
|
+
expect(html).toContain('卡片数据不可用');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('历史态禁用动作', () => {
|
|
76
|
+
const html = render(parking, true);
|
|
77
|
+
expect(html).toContain('disabled');
|
|
78
|
+
});
|
|
79
|
+
});
|