soma-kit 0.0.0-stage → 0.1.0
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/README.md +539 -2
- package/bin/soma-kit.mjs +27 -0
- package/package.json +66 -4
- package/packs/ai/agent-run-card.ts +64 -0
- package/packs/ai/evaluation-card.ts +51 -0
- package/packs/ai/guardrail-policy-card.ts +48 -0
- package/packs/ai/index.ts +31 -0
- package/packs/ai/model-card.ts +55 -0
- package/packs/ai/model-routing-card.ts +49 -0
- package/packs/ai/prompt-template-card.ts +49 -0
- package/packs/ai/texts.ts +19 -0
- package/packs/commerce/address-card.ts +58 -0
- package/packs/commerce/coupon-card.ts +64 -0
- package/packs/commerce/index.ts +25 -0
- package/packs/commerce/order-card.ts +60 -0
- package/packs/commerce/purchase-order-card.ts +74 -0
- package/packs/commerce/texts.ts +14 -0
- package/packs/finance/index.ts +30 -0
- package/packs/finance/invoice-summary-card.ts +48 -0
- package/packs/finance/reconcile-board.ts +450 -0
- package/packs/finance/reconcile-case-card.ts +70 -0
- package/packs/finance/reconcile-desk/compile.ts +208 -0
- package/packs/finance/reconcile-desk/index.ts +20 -0
- package/packs/finance/reconcile-desk/shell.ts +27 -0
- package/packs/finance/texts.ts +21 -0
- package/packs/ops/change-window-card.ts +54 -0
- package/packs/ops/dependency-status-card.ts +50 -0
- package/packs/ops/error-budget-card.ts +60 -0
- package/packs/ops/execution-task-runner.ts +61 -0
- package/packs/ops/index.ts +36 -0
- package/packs/ops/integration-status-card.ts +45 -0
- package/packs/ops/release-readiness-card.ts +63 -0
- package/packs/ops/runbook-execution-card.ts +81 -0
- package/packs/ops/texts.ts +21 -0
- package/packs/security/control-evidence-card.ts +54 -0
- package/packs/security/finding-card.ts +96 -0
- package/packs/security/index.ts +29 -0
- package/packs/security/texts.ts +31 -0
- package/packs/shared/card-util.ts +60 -0
- package/packs/shared/wrap.ts +13 -0
- package/spec/generated/kit-app.schema.json +84 -0
- package/spec/generated/kit-page.schema.json +2766 -0
- package/spec/kit-grammar.json +1358 -0
- package/spec/kit-grammar.schema.json +716 -0
- package/src/app/app-config.ts +108 -0
- package/src/app/compose-apps.ts +139 -0
- package/src/app/define-kit-actions.ts +89 -0
- package/src/app/define-kit-app.ts +362 -0
- package/src/app/define-kit-pack.ts +114 -0
- package/src/app/envelope.ts +117 -0
- package/src/app/index.ts +50 -0
- package/src/app/notify-renderer.ts +114 -0
- package/src/app/paged-list-adapter.ts +246 -0
- package/src/app/server-pages.ts +303 -0
- package/src/app/server.ts +244 -0
- package/src/app/submit.types.ts +87 -0
- package/src/base/action-busy.ts +159 -0
- package/src/base/action-gate.ts +267 -0
- package/src/base/action-status.ts +31 -0
- package/src/base/alert.ts +136 -0
- package/src/base/avatar.ts +271 -0
- package/src/base/button.ts +139 -0
- package/src/base/cascader.ts +523 -0
- package/src/base/check-core.ts +214 -0
- package/src/base/checkbox.ts +35 -0
- package/src/base/choice.ts +183 -0
- package/src/base/color-picker.ts +316 -0
- package/src/base/combobox.ts +61 -0
- package/src/base/command-palette.ts +371 -0
- package/src/base/context-menu.ts +103 -0
- package/src/base/countdown.ts +168 -0
- package/src/base/date-range.ts +444 -0
- package/src/base/date.ts +899 -0
- package/src/base/divider.ts +156 -0
- package/src/base/fab.ts +192 -0
- package/src/base/icons.ts +106 -0
- package/src/base/image.ts +177 -0
- package/src/base/index.ts +214 -0
- package/src/base/input.ts +316 -0
- package/src/base/kbd.ts +99 -0
- package/src/base/link.ts +167 -0
- package/src/base/listbox-core.ts +965 -0
- package/src/base/mentions.ts +323 -0
- package/src/base/menu-button.ts +240 -0
- package/src/base/menu-core.ts +341 -0
- package/src/base/month-grid.ts +171 -0
- package/src/base/motion/aura.ts +94 -0
- package/src/base/motion/hover-3d.ts +154 -0
- package/src/base/motion/hover-gallery.ts +185 -0
- package/src/base/motion/index.ts +27 -0
- package/src/base/motion/limits.ts +75 -0
- package/src/base/motion/marquee.ts +157 -0
- package/src/base/motion/text-rotate.ts +195 -0
- package/src/base/number.ts +121 -0
- package/src/base/otp.ts +259 -0
- package/src/base/permission-gate.ts +99 -0
- package/src/base/popover.ts +352 -0
- package/src/base/popup.ts +542 -0
- package/src/base/portal-core.ts +297 -0
- package/src/base/price-text.ts +126 -0
- package/src/base/progress.ts +216 -0
- package/src/base/qr-code.ts +446 -0
- package/src/base/qty-stepper.ts +241 -0
- package/src/base/radio.ts +33 -0
- package/src/base/range.ts +444 -0
- package/src/base/rating.ts +249 -0
- package/src/base/segmented.ts +286 -0
- package/src/base/select.ts +57 -0
- package/src/base/shared.ts +708 -0
- package/src/base/swap.ts +86 -0
- package/src/base/switch.ts +111 -0
- package/src/base/tag-input.ts +464 -0
- package/src/base/tag.ts +250 -0
- package/src/base/text.ts +126 -0
- package/src/base/textarea.ts +129 -0
- package/src/base/time-picker.ts +606 -0
- package/src/base/tree-select.ts +629 -0
- package/src/base/upload.ts +548 -0
- package/src/base/virtual-list.ts +286 -0
- package/src/biz/approval-desk/compile.ts +179 -0
- package/src/biz/approval-desk/index.ts +21 -0
- package/src/biz/approval-desk/shell.ts +50 -0
- package/src/biz/auth-panel/compile.ts +196 -0
- package/src/biz/auth-panel/index.ts +24 -0
- package/src/biz/auth-panel/shell.ts +46 -0
- package/src/biz/category-crud/compile.ts +173 -0
- package/src/biz/category-crud/index.ts +19 -0
- package/src/biz/category-crud/shell.ts +29 -0
- package/src/biz/checkout-page/compile.ts +111 -0
- package/src/biz/checkout-page/index.ts +20 -0
- package/src/biz/checkout-page/shell.ts +50 -0
- package/src/biz/compose.ts +313 -0
- package/src/biz/data-components.ts +26 -0
- package/src/biz/define.ts +23 -0
- package/src/biz/import-wizard/compile.ts +204 -0
- package/src/biz/import-wizard/hub.ts +540 -0
- package/src/biz/import-wizard/index.ts +24 -0
- package/src/biz/import-wizard/shell.ts +161 -0
- package/src/biz/index.ts +80 -0
- package/src/biz/landing-page/compile.ts +67 -0
- package/src/biz/landing-page/index.ts +20 -0
- package/src/biz/landing-page/shell.ts +48 -0
- package/src/biz/link/hub.ts +255 -0
- package/src/biz/link/shared.ts +493 -0
- package/src/biz/master-sub/compile.ts +232 -0
- package/src/biz/master-sub/index.ts +20 -0
- package/src/biz/master-sub/shell.ts +56 -0
- package/src/biz/multi-board/compile.ts +234 -0
- package/src/biz/multi-board/index.ts +19 -0
- package/src/biz/multi-board/shell.ts +70 -0
- package/src/biz/names.ts +30 -0
- package/src/biz/notification-center/compile.ts +138 -0
- package/src/biz/notification-center/index.ts +29 -0
- package/src/biz/notification-center/shell.ts +42 -0
- package/src/biz/page-templates/result-page/compile.ts +46 -0
- package/src/biz/page-templates/result-page/index.ts +19 -0
- package/src/biz/page-templates/result-page/shell.ts +31 -0
- package/src/biz/page-templates/shared.ts +121 -0
- package/src/biz/page-templates/sign-in-page/compile.ts +176 -0
- package/src/biz/page-templates/sign-in-page/index.ts +18 -0
- package/src/biz/page-templates/sign-in-page/shell.ts +116 -0
- package/src/biz/record-detail/compile.ts +290 -0
- package/src/biz/record-detail/index.ts +22 -0
- package/src/biz/record-detail/shell.ts +64 -0
- package/src/biz/settings-center/compile.ts +100 -0
- package/src/biz/settings-center/index.ts +18 -0
- package/src/biz/settings-center/shell.ts +51 -0
- package/src/biz/shared/action-host.ts +469 -0
- package/src/biz/shared/coordinator.ts +516 -0
- package/src/biz/shared/index.ts +9 -0
- package/src/biz/shared/pipes.ts +58 -0
- package/src/biz/shared/runtime-hub.ts +112 -0
- package/src/biz/shared/step-values.ts +25 -0
- package/src/biz/single-form/compile.ts +146 -0
- package/src/biz/single-form/index.ts +18 -0
- package/src/biz/single-form/shell.ts +38 -0
- package/src/biz/standard-crud/bridge.ts +11 -0
- package/src/biz/standard-crud/compile.ts +546 -0
- package/src/biz/standard-crud/index.ts +18 -0
- package/src/biz/standard-crud/shell.ts +157 -0
- package/src/biz/step-form/compile.ts +400 -0
- package/src/biz/step-form/hub.ts +412 -0
- package/src/biz/step-form/index.ts +23 -0
- package/src/biz/step-form/shell.ts +96 -0
- package/src/biz/step-form/steps-ref.ts +112 -0
- package/src/biz/types.ts +96 -0
- package/src/cli/kit-cli.ts +102 -0
- package/src/compile/action-host-attach.ts +76 -0
- package/src/compile/compile-page.ts +472 -0
- package/src/compile/component-candidates.ts +147 -0
- package/src/compile/fragment.ts +104 -0
- package/src/compile/hints.ts +255 -0
- package/src/compile/index.ts +16 -0
- package/src/compile/locate.ts +166 -0
- package/src/compile/page-params.ts +80 -0
- package/src/compile/parse.ts +355 -0
- package/src/compile/redirect.ts +90 -0
- package/src/compile/registry.ts +30 -0
- package/src/compile/remove-guard.ts +99 -0
- package/src/compile/styles.ts +244 -0
- package/src/compile/types.ts +106 -0
- package/src/data/amount-breakdown.ts +132 -0
- package/src/data/anchor-nav.ts +225 -0
- package/src/data/approval-flow.ts +269 -0
- package/src/data/assignment-picker.ts +652 -0
- package/src/data/async-result-panel.ts +308 -0
- package/src/data/breadcrumbs.ts +197 -0
- package/src/data/calendar.ts +363 -0
- package/src/data/carousel.ts +408 -0
- package/src/data/chart.ts +726 -0
- package/src/data/checklist.ts +215 -0
- package/src/data/code-view.ts +415 -0
- package/src/data/comment-thread.ts +342 -0
- package/src/data/confirm-dialog.ts +144 -0
- package/src/data/context-menu.ts +216 -0
- package/src/data/d9-limits.ts +42 -0
- package/src/data/d9-util.ts +81 -0
- package/src/data/diff.ts +239 -0
- package/src/data/display-util.ts +135 -0
- package/src/data/dock.ts +152 -0
- package/src/data/embed.ts +200 -0
- package/src/data/field-mapper.ts +544 -0
- package/src/data/flow-editor-contract.ts +41 -0
- package/src/data/flow-editor.ts +2622 -0
- package/src/data/flow-remote-options.ts +257 -0
- package/src/data/footer.ts +157 -0
- package/src/data/form-draft-registry.ts +76 -0
- package/src/data/gantt.ts +430 -0
- package/src/data/hero.ts +149 -0
- package/src/data/image-viewer.ts +333 -0
- package/src/data/index.ts +268 -0
- package/src/data/item-list.ts +335 -0
- package/src/data/kanban.ts +667 -0
- package/src/data/media-grid.ts +246 -0
- package/src/data/mega-menu.ts +257 -0
- package/src/data/menu-dom.ts +169 -0
- package/src/data/menu.ts +464 -0
- package/src/data/mockup.ts +176 -0
- package/src/data/modal-dialog.ts +182 -0
- package/src/data/navbar.ts +292 -0
- package/src/data/object-form.ts +1981 -0
- package/src/data/options-bus.ts +24 -0
- package/src/data/outcome-watch.ts +40 -0
- package/src/data/overlay-portal.ts +6 -0
- package/src/data/pager.ts +473 -0
- package/src/data/permission-matrix.ts +432 -0
- package/src/data/permission-tree.ts +916 -0
- package/src/data/pricing-table.ts +269 -0
- package/src/data/prompt-dialog.ts +235 -0
- package/src/data/reaction-bar.ts +236 -0
- package/src/data/record-card.ts +413 -0
- package/src/data/record-picker.ts +489 -0
- package/src/data/redirect.ts +109 -0
- package/src/data/remote-options.ts +54 -0
- package/src/data/request-console.ts +399 -0
- package/src/data/resident.ts +58 -0
- package/src/data/result-panel.ts +160 -0
- package/src/data/rich-text.ts +626 -0
- package/src/data/row-editor.ts +1066 -0
- package/src/data/rule-builder.ts +1137 -0
- package/src/data/shape-util.ts +221 -0
- package/src/data/staged-rows.ts +69 -0
- package/src/data/stat-group.ts +231 -0
- package/src/data/static-lazy.ts +63 -0
- package/src/data/static-registry.ts +227 -0
- package/src/data/steps.ts +195 -0
- package/src/data/survey.ts +455 -0
- package/src/data/system-switcher.ts +243 -0
- package/src/data/timeline.ts +204 -0
- package/src/data/tour-guide.ts +392 -0
- package/src/data/transfer.ts +597 -0
- package/src/data/tree-check.ts +30 -0
- package/src/data/tree-table.ts +674 -0
- package/src/data/tree.ts +656 -0
- package/src/data/trend-line.ts +147 -0
- package/src/data/virtual-rows.ts +556 -0
- package/src/data/watermark.ts +140 -0
- package/src/data/wrap.ts +267 -0
- package/src/data/wrapped-candidates.ts +96 -0
- package/src/frames/blank-frame.ts +19 -0
- package/src/frames/index.ts +44 -0
- package/src/frames/page-bar.ts +185 -0
- package/src/frames/page-header.ts +185 -0
- package/src/frames/page-status.ts +54 -0
- package/src/frames/resize-handle.ts +147 -0
- package/src/frames/shared.ts +242 -0
- package/src/frames/single-frame.ts +16 -0
- package/src/frames/split-frame/index.ts +4 -0
- package/src/frames/split-frame/shell.ts +219 -0
- package/src/frames/split-frame/span.ts +64 -0
- package/src/frames/split-frame/wiring.ts +164 -0
- package/src/frames/tabs-frame/index.ts +3 -0
- package/src/frames/tabs-frame/shell.ts +112 -0
- package/src/frames/tabs-frame/wiring.ts +69 -0
- package/src/frames/top-main-frame.ts +20 -0
- package/src/frames/types.ts +72 -0
- package/src/frames/workspace-frame/extras.ts +197 -0
- package/src/frames/workspace-frame/index.ts +13 -0
- package/src/frames/workspace-frame/shell.ts +282 -0
- package/src/frames/workspace-frame/state.ts +152 -0
- package/src/frames/workspace-frame/view.ts +1013 -0
- package/src/generated/kit-grammar-runtime.ts +2822 -0
- package/src/generated/page-origin.ts +57 -0
- package/src/i18n/index.ts +22 -0
- package/src/i18n/kit-text.ts +692 -0
- package/src/i18n/resolve-text-refs.ts +128 -0
- package/src/index.ts +6 -0
- package/src/object-renderer/action-gate.ts +216 -0
- package/src/object-renderer/adopt-gate.ts +333 -0
- package/src/object-renderer/arrange.ts +411 -0
- package/src/object-renderer/candidates.ts +68 -0
- package/src/object-renderer/collapse-wiring.ts +99 -0
- package/src/object-renderer/collection-expand.ts +185 -0
- package/src/object-renderer/collection-parts.ts +653 -0
- package/src/object-renderer/collection-table.ts +503 -0
- package/src/object-renderer/collection.ts +628 -0
- package/src/object-renderer/compile-data.ts +549 -0
- package/src/object-renderer/defaults.ts +252 -0
- package/src/object-renderer/display-checks.ts +153 -0
- package/src/object-renderer/emit-events.ts +498 -0
- package/src/object-renderer/emit-object.ts +98 -0
- package/src/object-renderer/emit-static.ts +1036 -0
- package/src/object-renderer/field-policy.ts +89 -0
- package/src/object-renderer/field-value.ts +581 -0
- package/src/object-renderer/form-detail.ts +116 -0
- package/src/object-renderer/index.ts +13 -0
- package/src/object-renderer/linkage.ts +371 -0
- package/src/object-renderer/merge-class.ts +126 -0
- package/src/object-renderer/mode.ts +25 -0
- package/src/object-renderer/outcome-draft.ts +61 -0
- package/src/object-renderer/parts.ts +136 -0
- package/src/object-renderer/pick-component.ts +36 -0
- package/src/object-renderer/render-object.ts +724 -0
- package/src/object-renderer/request-actions.ts +581 -0
- package/src/object-renderer/skeleton.ts +101 -0
- package/src/object-renderer/table-actions.ts +438 -0
- package/src/object-renderer/table-summary.ts +105 -0
- package/src/object-renderer/table-tools.ts +1072 -0
- package/src/object-renderer/types.ts +936 -0
- package/src/page-target/ai-surface.ts +154 -0
- package/src/page-target/compile.ts +354 -0
- package/src/page-target/drift.ts +186 -0
- package/src/page-target/index.ts +25 -0
- package/src/page-target/intent-map.ts +137 -0
- package/src/page-target/intent-schema.ts +711 -0
- package/src/page-target/origin.ts +17 -0
- package/src/page-target/target.ts +450 -0
- package/src/page-target/writer-policy.ts +315 -0
- package/src/theme/block-theme.ts +202 -0
- package/src/theme/kit-theme-text.ts +2 -0
- package/src/theme/kit-theme.css +275 -0
- package/src/theme/kit-theme.tpl +275 -0
- package/src/theme/theme-tokens.ts +135 -0
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// PricingTable:定价表(layer: data,写死形状,accept: single)。
|
|
2
|
+
//
|
|
3
|
+
// 数据:`data` 是 { cycles?, plans, current_key? }(也可写在 `view.props`,data 优先)。
|
|
4
|
+
// - cycles:[{ key, label, note? }],如月付 / 年付(note 放「省 20%」角标);少于 2 项不出切换条。
|
|
5
|
+
// - plans:[{ key, name, desc?, price, period?, badge?, recommended?, disabled?, action_text?, features? }]。
|
|
6
|
+
// price 是当前周期下的价格文案;也可以是 { [周期 key]: 文案 },组件只按当前周期挑文案,不计算价格、折扣、税(价格是业务事实)。
|
|
7
|
+
// period 同理(字符串或按周期的对象)。features:字符串(对勾)或 { text, value },value 为 true 对勾 / false 叉 / 其它文字原样显示。
|
|
8
|
+
// - current_key:用户所在方案,该卡标「当前方案」、动作钮不可点(优先于 badge)。
|
|
9
|
+
// 周期由组件内部持有(`cycle` prop 变化时覆盖),切换抛 `cycle_change { key }`;点动作钮抛 `select { key, cycle, item }`。
|
|
10
|
+
// 推荐方案(recommended)加主色描边与「推荐」角标;窄容器 1 列,md 档 2 列,xl 档 3 列(容器查询)。
|
|
11
|
+
|
|
12
|
+
import type { SomaComponentDefinition } from 'soma-core';
|
|
13
|
+
import { createKitController, joinClass, str, type KitView } from '../base/shared';
|
|
14
|
+
import { kitIcon } from '../base/icons';
|
|
15
|
+
import { kitText } from '../i18n/kit-text';
|
|
16
|
+
import { classTokens, emptyNode, mk, recOf, rowsOf, textOf, type Rec } from './shape-util';
|
|
17
|
+
import { singleOf } from './display-util';
|
|
18
|
+
import { attrEsc } from './d9-util';
|
|
19
|
+
|
|
20
|
+
const KEYS = ['cycles', 'plans', 'current_key'] as const;
|
|
21
|
+
|
|
22
|
+
const ROOT_CLASS = '@container flex flex-col gap-5 text-fg';
|
|
23
|
+
const CYCLES_CLASS = 'mx-auto inline-flex items-center gap-1 rounded-lg bg-surface-sunken p-1';
|
|
24
|
+
const CYCLE_CLASS = 'inline-flex h-8 items-center gap-1 rounded-md px-3 text-sm font-medium text-fg-muted transition-colors hover:text-fg focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring data-kit-selected:bg-surface data-kit-selected:text-fg data-kit-selected:shadow-sm';
|
|
25
|
+
const NOTE_CLASS = 'rounded-full bg-success-soft px-1.5 text-xs font-medium text-success-on-soft';
|
|
26
|
+
const GRID_CLASS = 'grid grid-cols-1 gap-4 @md:grid-cols-2 @xl:grid-cols-3';
|
|
27
|
+
const CARD_CLASS = 'relative flex flex-col gap-4 rounded-lg border border-border-subtle bg-surface p-5';
|
|
28
|
+
const CARD_RECOMMENDED_CLASS = 'relative flex flex-col gap-4 rounded-lg border border-primary bg-surface p-5 shadow-md ring-1 ring-primary';
|
|
29
|
+
const BADGE_CLASS = 'absolute end-4 top-4 rounded-full bg-primary-soft px-2 py-0.5 text-xs font-medium text-primary-on-soft';
|
|
30
|
+
const NAME_CLASS = 'text-lg font-semibold text-fg';
|
|
31
|
+
const DESC_CLASS = 'text-sm text-fg-muted';
|
|
32
|
+
const PRICE_ROW_CLASS = 'flex items-baseline gap-1';
|
|
33
|
+
const PRICE_CLASS = 'text-2xl font-semibold text-fg';
|
|
34
|
+
const PERIOD_CLASS = 'text-sm text-fg-muted';
|
|
35
|
+
const FEATURES_CLASS = 'flex flex-1 flex-col gap-2 text-sm';
|
|
36
|
+
const FEATURE_CLASS = 'flex items-start gap-2 text-fg';
|
|
37
|
+
const FEATURE_OFF_CLASS = 'flex items-start gap-2 text-fg-subtle';
|
|
38
|
+
const TICK_CLASS = 'inline-flex size-5 shrink-0 items-center justify-center rounded-full bg-success-soft text-xs text-success-on-soft';
|
|
39
|
+
const CROSS_CLASS = 'inline-flex size-5 shrink-0 items-center justify-center rounded-full bg-surface-sunken text-xs text-fg-subtle';
|
|
40
|
+
const VALUE_CLASS = 'ms-auto shrink-0 text-fg-muted';
|
|
41
|
+
const ACTION_CLASS = 'inline-flex h-10 items-center justify-center rounded-md bg-primary px-4 text-sm font-medium text-primary-fg transition-colors hover:bg-primary-hover active:bg-primary-active focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-60';
|
|
42
|
+
const ACTION_PLAIN_CLASS = 'inline-flex h-10 items-center justify-center rounded-md border border-border bg-surface px-4 text-sm font-medium text-fg transition-colors hover:bg-surface-sunken focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-60';
|
|
43
|
+
|
|
44
|
+
export const KIT_PRICING_TABLE_CLASS_CANDIDATES: readonly string[] = classTokens(
|
|
45
|
+
ROOT_CLASS, CYCLES_CLASS, CYCLE_CLASS, NOTE_CLASS, GRID_CLASS, CARD_CLASS, CARD_RECOMMENDED_CLASS, BADGE_CLASS, NAME_CLASS,
|
|
46
|
+
DESC_CLASS, PRICE_ROW_CLASS, PRICE_CLASS, PERIOD_CLASS, FEATURES_CLASS, FEATURE_CLASS, FEATURE_OFF_CLASS, TICK_CLASS,
|
|
47
|
+
CROSS_CLASS, VALUE_CLASS, ACTION_CLASS, ACTION_PLAIN_CLASS,
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
export interface KitPricingCycle { key: string; label: string; note: string }
|
|
51
|
+
export interface KitPricingFeature { text: string; kind: 'on' | 'off' | 'value'; value: string }
|
|
52
|
+
export interface KitPricingPlan {
|
|
53
|
+
key: string;
|
|
54
|
+
name: string;
|
|
55
|
+
desc: string;
|
|
56
|
+
prices: Rec | null;
|
|
57
|
+
price: string;
|
|
58
|
+
periods: Rec | null;
|
|
59
|
+
period: string;
|
|
60
|
+
badge: string;
|
|
61
|
+
recommended: boolean;
|
|
62
|
+
disabled: boolean;
|
|
63
|
+
actionText: string;
|
|
64
|
+
features: KitPricingFeature[];
|
|
65
|
+
raw: Rec;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function pricingCyclesOf(raw: unknown): KitPricingCycle[] {
|
|
69
|
+
const out: KitPricingCycle[] = [];
|
|
70
|
+
const seen = new Set<string>();
|
|
71
|
+
for (const row of rowsOf(raw)) {
|
|
72
|
+
const key = textOf(row.key);
|
|
73
|
+
if (key === '' || seen.has(key)) continue;
|
|
74
|
+
seen.add(key);
|
|
75
|
+
out.push({ key, label: textOf(row.label) || key, note: textOf(row.note) });
|
|
76
|
+
}
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** 文案 / 按周期对象 -> 当前周期的文案(没有该周期的键则空串,不猜) */
|
|
81
|
+
export function pricingTextFor(raw: unknown, cycle: string): string {
|
|
82
|
+
if (raw !== null && typeof raw === 'object' && !Array.isArray(raw)) return textOf((raw as Rec)[cycle]);
|
|
83
|
+
return textOf(raw);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function pricingFeaturesOf(raw: unknown): KitPricingFeature[] {
|
|
87
|
+
if (!Array.isArray(raw)) return [];
|
|
88
|
+
const out: KitPricingFeature[] = [];
|
|
89
|
+
for (const f of raw) {
|
|
90
|
+
if (typeof f === 'string' || typeof f === 'number') {
|
|
91
|
+
out.push({ text: String(f), kind: 'on', value: '' });
|
|
92
|
+
} else if (f !== null && typeof f === 'object' && !Array.isArray(f)) {
|
|
93
|
+
const r = f as Rec;
|
|
94
|
+
const text = textOf(r.text);
|
|
95
|
+
if (text === '') continue;
|
|
96
|
+
if (r.value === false) out.push({ text, kind: 'off', value: '' });
|
|
97
|
+
else if (r.value === undefined || r.value === true || r.value === null || r.value === '') out.push({ text, kind: 'on', value: '' });
|
|
98
|
+
else out.push({ text, kind: 'value', value: textOf(r.value) });
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function pricingPlansOf(raw: unknown): KitPricingPlan[] {
|
|
105
|
+
const out: KitPricingPlan[] = [];
|
|
106
|
+
rowsOf(raw).forEach((r, i) => {
|
|
107
|
+
const key = textOf(r.key) || `p${i}`;
|
|
108
|
+
const price = r.price;
|
|
109
|
+
const period = r.period;
|
|
110
|
+
out.push({
|
|
111
|
+
key,
|
|
112
|
+
name: textOf(r.name),
|
|
113
|
+
desc: textOf(r.desc),
|
|
114
|
+
prices: price !== null && typeof price === 'object' && !Array.isArray(price) ? recOf(price) : null,
|
|
115
|
+
price: typeof price === 'object' ? '' : textOf(price),
|
|
116
|
+
periods: period !== null && typeof period === 'object' && !Array.isArray(period) ? recOf(period) : null,
|
|
117
|
+
period: typeof period === 'object' ? '' : textOf(period),
|
|
118
|
+
badge: textOf(r.badge),
|
|
119
|
+
recommended: r.recommended === true,
|
|
120
|
+
disabled: r.disabled === true,
|
|
121
|
+
actionText: textOf(r.action_text),
|
|
122
|
+
features: pricingFeaturesOf(r.features),
|
|
123
|
+
raw: r,
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
return out;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
interface State { cycle: string; given: string; }
|
|
130
|
+
const STATES = new WeakMap<HTMLElement, State>();
|
|
131
|
+
const stateOf = (root: HTMLElement): State => {
|
|
132
|
+
let s = STATES.get(root);
|
|
133
|
+
if (!s) { s = { cycle: '', given: '' }; STATES.set(root, s); }
|
|
134
|
+
return s;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export function createPricingTable(): SomaComponentDefinition {
|
|
138
|
+
let self: KitView;
|
|
139
|
+
return {
|
|
140
|
+
contract: {
|
|
141
|
+
name: 'PricingTable',
|
|
142
|
+
layer: 'data',
|
|
143
|
+
props: {
|
|
144
|
+
data: { type: 'object', default: {}, desc: '{ cycles?, plans, current_key? }' },
|
|
145
|
+
cycles: { type: 'array', default: [], desc: '计费周期 [{ key, label, note? }];少于 2 项不出切换条' },
|
|
146
|
+
plans: { type: 'array', default: [], desc: '方案 [{ key, name, desc?, price, period?, badge?, recommended?, disabled?, action_text?, features? }]' },
|
|
147
|
+
current_key: { type: 'string', default: '', desc: '用户当前所在方案的 key' },
|
|
148
|
+
cycle: { type: 'string', default: '', desc: '当前周期 key;变化时覆盖内部选择,空则取 cycles 首项' },
|
|
149
|
+
action_text: { type: 'string', default: '', desc: '动作钮默认文案;方案自己的 action_text 优先' },
|
|
150
|
+
current_text: { type: 'string', default: '', desc: '当前方案的标记文案' },
|
|
151
|
+
empty_text: { type: 'string', default: '', desc: '无方案时的文案' },
|
|
152
|
+
class: { type: 'string', default: '', desc: '外部传入的类,追加在根节点类串末尾' },
|
|
153
|
+
},
|
|
154
|
+
emits: {
|
|
155
|
+
cycle_change: { payload: { key: { type: 'string' } }, desc: '切换计费周期' },
|
|
156
|
+
select: { payload: { key: { type: 'string' }, cycle: { type: 'string' }, item: { type: 'object' } }, desc: '点方案动作钮;item 是方案源对象' },
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
templateHtml: `<section data-soma-id="root" class="${ROOT_CLASS}"></section>`,
|
|
160
|
+
controllerFactory: createKitController(self = {
|
|
161
|
+
attach(root, api) {
|
|
162
|
+
const s = stateOf(root);
|
|
163
|
+
const onClick = (event: Event): void => {
|
|
164
|
+
const t = event.target as HTMLElement | null;
|
|
165
|
+
const cyc = t?.closest<HTMLElement>('[data-kit-cycle]');
|
|
166
|
+
if (cyc && root.contains(cyc)) {
|
|
167
|
+
const key = cyc.getAttribute('data-kit-cycle') ?? '';
|
|
168
|
+
if (key !== '' && key !== s.cycle) {
|
|
169
|
+
s.cycle = key;
|
|
170
|
+
api.emit('cycle_change', { key });
|
|
171
|
+
for (const el of Array.from(root.querySelectorAll<HTMLElement>('[data-kit-cycle]'))) {
|
|
172
|
+
const on = el.getAttribute('data-kit-cycle') === key;
|
|
173
|
+
if (on) el.setAttribute('data-kit-selected', ''); else el.removeAttribute('data-kit-selected');
|
|
174
|
+
el.setAttribute('aria-pressed', on ? 'true' : 'false');
|
|
175
|
+
}
|
|
176
|
+
// 价格文案随周期换:整体重绘(周期条与卡片都在 render 里画)
|
|
177
|
+
s.given = '\u0000repaint';
|
|
178
|
+
const hadFocus = root.contains(document.activeElement);
|
|
179
|
+
self.render(root, api.props(), api);
|
|
180
|
+
// 重绘会重建切换条,键盘用户的焦点要回到新的同周期按钮上
|
|
181
|
+
if (hadFocus) root.querySelector<HTMLElement>(`[data-kit-cycle="${attrEsc(key)}"]`)?.focus();
|
|
182
|
+
}
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const act = t?.closest<HTMLElement>('[data-kit-plan-action]');
|
|
186
|
+
if (act && root.contains(act) && !act.hasAttribute('disabled')) {
|
|
187
|
+
const key = act.getAttribute('data-kit-plan-action') ?? '';
|
|
188
|
+
const p = api.props();
|
|
189
|
+
const src = singleOf(p.data, p, KEYS);
|
|
190
|
+
const plan = pricingPlansOf(src.plans).find((x) => x.key === key);
|
|
191
|
+
if (plan) api.emit('select', { key, cycle: s.cycle, item: plan.raw });
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
root.addEventListener('click', onClick);
|
|
195
|
+
return () => { root.removeEventListener('click', onClick); STATES.delete(root); };
|
|
196
|
+
},
|
|
197
|
+
render(root, p) {
|
|
198
|
+
const s = stateOf(root);
|
|
199
|
+
root.className = joinClass(ROOT_CLASS, p.class);
|
|
200
|
+
root.textContent = '';
|
|
201
|
+
const src = singleOf(p.data, p, KEYS);
|
|
202
|
+
const cycles = pricingCyclesOf(src.cycles);
|
|
203
|
+
const plans = pricingPlansOf(src.plans);
|
|
204
|
+
const givenCycle = str(p.cycle);
|
|
205
|
+
if (s.given !== givenCycle && s.given !== '\u0000repaint') { s.given = givenCycle; if (givenCycle !== '') s.cycle = givenCycle; }
|
|
206
|
+
if (s.given === '\u0000repaint') s.given = givenCycle;
|
|
207
|
+
if (!cycles.some((c) => c.key === s.cycle)) s.cycle = cycles[0]?.key ?? '';
|
|
208
|
+
if (plans.length === 0) {
|
|
209
|
+
root.appendChild(emptyNode(str(p.empty_text, kitText('soma.kit.pt_empty', undefined, root))));
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if (cycles.length >= 2) {
|
|
213
|
+
const bar = mk('div', CYCLES_CLASS);
|
|
214
|
+
bar.setAttribute('role', 'group');
|
|
215
|
+
bar.setAttribute('aria-label', kitText('soma.kit.pt_period', undefined, root));
|
|
216
|
+
for (const c of cycles) {
|
|
217
|
+
const btn = mk('button', CYCLE_CLASS);
|
|
218
|
+
btn.type = 'button';
|
|
219
|
+
btn.setAttribute('data-kit-cycle', c.key);
|
|
220
|
+
const on = c.key === s.cycle;
|
|
221
|
+
btn.setAttribute('aria-pressed', on ? 'true' : 'false');
|
|
222
|
+
if (on) btn.setAttribute('data-kit-selected', '');
|
|
223
|
+
btn.appendChild(mk('span', '', c.label));
|
|
224
|
+
if (c.note !== '') btn.appendChild(mk('span', NOTE_CLASS, c.note));
|
|
225
|
+
bar.appendChild(btn);
|
|
226
|
+
}
|
|
227
|
+
root.appendChild(bar);
|
|
228
|
+
}
|
|
229
|
+
const current = textOf(src.current_key);
|
|
230
|
+
const grid = mk('div', GRID_CLASS);
|
|
231
|
+
for (const plan of plans) {
|
|
232
|
+
const isCurrent = current !== '' && plan.key === current;
|
|
233
|
+
const card = mk('article', plan.recommended ? CARD_RECOMMENDED_CLASS : CARD_CLASS);
|
|
234
|
+
card.setAttribute('data-kit-plan', plan.key);
|
|
235
|
+
const badge = isCurrent ? str(p.current_text, kitText('soma.kit.pt_current', undefined, root)) : plan.badge;
|
|
236
|
+
if (badge !== '') card.appendChild(mk('span', BADGE_CLASS, badge));
|
|
237
|
+
card.appendChild(mk('h3', NAME_CLASS, plan.name));
|
|
238
|
+
if (plan.desc !== '') card.appendChild(mk('p', DESC_CLASS, plan.desc));
|
|
239
|
+
const price = plan.prices ? pricingTextFor(plan.prices, s.cycle) : plan.price;
|
|
240
|
+
const period = plan.periods ? pricingTextFor(plan.periods, s.cycle) : plan.period;
|
|
241
|
+
const row = mk('div', PRICE_ROW_CLASS);
|
|
242
|
+
row.appendChild(mk('span', PRICE_CLASS, price));
|
|
243
|
+
if (period !== '') row.appendChild(mk('span', PERIOD_CLASS, period));
|
|
244
|
+
card.appendChild(row);
|
|
245
|
+
const ul = mk('ul', FEATURES_CLASS);
|
|
246
|
+
for (const f of plan.features) {
|
|
247
|
+
const li = mk('li', f.kind === 'off' ? FEATURE_OFF_CLASS : FEATURE_CLASS);
|
|
248
|
+
const mark = mk('span', f.kind === 'off' ? CROSS_CLASS : TICK_CLASS, '');
|
|
249
|
+
mark.appendChild(kitIcon(f.kind === 'off' ? 'close' : 'check', 'xs'));
|
|
250
|
+
mark.setAttribute('aria-hidden', 'true');
|
|
251
|
+
li.appendChild(mark);
|
|
252
|
+
li.appendChild(mk('span', '', f.text));
|
|
253
|
+
if (f.kind === 'off') li.appendChild(mk('span', 'sr-only', kitText('soma.kit.pt_excluded', undefined, root)));
|
|
254
|
+
if (f.kind === 'value') li.appendChild(mk('span', VALUE_CLASS, f.value));
|
|
255
|
+
ul.appendChild(li);
|
|
256
|
+
}
|
|
257
|
+
card.appendChild(ul);
|
|
258
|
+
const btn = mk('button', plan.recommended ? ACTION_CLASS : ACTION_PLAIN_CLASS, plan.actionText || str(p.action_text, kitText('soma.kit.pt_choose', undefined, root)));
|
|
259
|
+
btn.type = 'button';
|
|
260
|
+
btn.setAttribute('data-kit-plan-action', plan.key);
|
|
261
|
+
if (isCurrent || plan.disabled) { btn.disabled = true; btn.setAttribute('aria-disabled', 'true'); }
|
|
262
|
+
card.appendChild(btn);
|
|
263
|
+
grid.appendChild(card);
|
|
264
|
+
}
|
|
265
|
+
root.appendChild(grid);
|
|
266
|
+
},
|
|
267
|
+
}),
|
|
268
|
+
};
|
|
269
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
// askKitPrompt:命令式的自绘「请求前输入」对话框(补能力 GA4,request.prompt 的运行端)。
|
|
2
|
+
//
|
|
3
|
+
// 壳用 mountKitModal(经 core 浮层端口挂出:Esc 走层栈只关栈顶、Tab 焦点陷阱、关闭后回焦);本文件只补输入项与按钮:
|
|
4
|
+
// - 返回 Promise<Record | null>:确认 = 输入值对象(键 = 输入项 name);取消 / Esc / 关闭钮 = null(调用方不发请求);
|
|
5
|
+
// - 输入项 type:text / textarea / number(原生 input 即可)、select(自绘单选列表,role=radiogroup,方向键 / Home / End 移动并选中)、
|
|
6
|
+
// switch(自绘 role=switch 开关)。不用原生 select / 原生对话框;
|
|
7
|
+
// - 必填项为空不能确认,错误写在该项下面(role=alert),不静默吞;number 只收有限数字;
|
|
8
|
+
// - 文案只用 textContent 写入,不经 innerHTML;无 DOM 环境(SSR / 测试)直接 resolve null。
|
|
9
|
+
// 输入项形状与限值见 object-renderer/request-actions.ts 的 promptOf(编译期已核,这里信任但不盲信:非法项忽略)。
|
|
10
|
+
|
|
11
|
+
import { kitText } from '../i18n/kit-text';
|
|
12
|
+
import { KIT_MODAL_CLASS_CANDIDATES, mountKitModal, watchKitModalDetached } from './modal-dialog';
|
|
13
|
+
import { BUTTON_CLASS_TOKENS, classTokens } from './shape-util';
|
|
14
|
+
import type { KitPromptSpec } from '../object-renderer/request-actions';
|
|
15
|
+
|
|
16
|
+
const BUTTON_BASE = 'inline-flex h-9 cursor-pointer items-center justify-center rounded-md px-4 text-sm font-medium transition-colors active:opacity-80 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring';
|
|
17
|
+
const BUTTON_PRIMARY = 'border-0 bg-primary text-primary-fg hover:opacity-90';
|
|
18
|
+
const BUTTON_NEUTRAL = 'border border-border bg-surface text-fg hover:bg-surface-sunken';
|
|
19
|
+
const FIELD_CLASS = 'flex flex-col gap-1';
|
|
20
|
+
const LABEL_CLASS = 'text-sm font-medium text-fg';
|
|
21
|
+
const STAR_CLASS = 'text-danger-on-soft';
|
|
22
|
+
const INPUT_CLASS = 'h-9 w-full rounded-md border border-border bg-surface px-3 text-sm text-fg placeholder:text-fg-subtle focus-visible:outline-none focus-visible:border-primary focus-visible:ring-2 focus-visible:ring-ring';
|
|
23
|
+
const AREA_CLASS = 'block w-full resize-none rounded-md border border-border bg-surface px-3 py-2 text-sm text-fg placeholder:text-fg-subtle focus-visible:outline-none focus-visible:border-primary focus-visible:ring-2 focus-visible:ring-ring';
|
|
24
|
+
const ERROR_CLASS = 'm-0 text-xs text-danger-on-soft';
|
|
25
|
+
const GROUP_CLASS = 'flex max-h-48 flex-col gap-1 overflow-y-auto rounded-md border border-border bg-surface p-1';
|
|
26
|
+
// 选中态 / 开关态用整串字面量切换(不拼接类名,候选类一次收齐)
|
|
27
|
+
const RADIO_OFF = 'flex h-8 w-full border-0 bg-transparent cursor-pointer items-center rounded-md px-3 text-start text-sm text-fg transition-colors hover:bg-surface-sunken focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring';
|
|
28
|
+
const RADIO_ON = 'flex h-8 w-full border-0 cursor-pointer items-center rounded-md bg-primary-soft px-3 text-start text-sm text-primary-on-soft transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring';
|
|
29
|
+
const SWITCH_OFF = 'inline-flex h-6 w-10 cursor-pointer items-center rounded-full border border-border bg-surface-sunken px-0.5 transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring';
|
|
30
|
+
const SWITCH_ON = 'inline-flex h-6 w-10 cursor-pointer items-center rounded-full border border-primary bg-primary px-0.5 transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring';
|
|
31
|
+
const KNOB_OFF = 'block size-4 translate-x-0 rounded-full bg-surface shadow-sm transition-transform motion-safe:duration-150';
|
|
32
|
+
const KNOB_ON = 'block size-4 translate-x-4 rounded-full bg-surface shadow-sm transition-transform motion-safe:duration-150';
|
|
33
|
+
const HIDDEN = 'hidden';
|
|
34
|
+
|
|
35
|
+
export const KIT_PROMPT_CLASS_CANDIDATES: readonly string[] = classTokens(
|
|
36
|
+
BUTTON_BASE, BUTTON_PRIMARY, BUTTON_NEUTRAL, FIELD_CLASS, LABEL_CLASS, STAR_CLASS, INPUT_CLASS, AREA_CLASS, ERROR_CLASS, GROUP_CLASS, RADIO_OFF, RADIO_ON,
|
|
37
|
+
SWITCH_OFF, SWITCH_ON, KNOB_OFF, KNOB_ON, HIDDEN, BUTTON_CLASS_TOKENS, KIT_MODAL_CLASS_CANDIDATES,
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
export interface KitPromptOptions extends KitPromptSpec {
|
|
41
|
+
/** 输入项初值(name -> 值;调用方已把 $filter / $row 取值路径解析成值) */
|
|
42
|
+
defaults?: Readonly<Record<string, unknown>>;
|
|
43
|
+
/** 调用方元素:取页面语言与书写方向,也是回焦参照 */
|
|
44
|
+
at?: Element | null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
type Field = KitPromptSpec['fields'][number];
|
|
48
|
+
|
|
49
|
+
interface Reader {
|
|
50
|
+
/** 当前值;number 空 / 非有限数返回 undefined */
|
|
51
|
+
read(): unknown;
|
|
52
|
+
/** 是否为空(必填判断用) */
|
|
53
|
+
blank(): boolean;
|
|
54
|
+
focus(): void;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function mk<K extends keyof HTMLElementTagNameMap>(doc: Document, tag: K, cls: string, text?: string): HTMLElementTagNameMap[K] {
|
|
58
|
+
const node = doc.createElement(tag);
|
|
59
|
+
if (cls !== '') node.className = cls;
|
|
60
|
+
if (text !== undefined) node.textContent = text;
|
|
61
|
+
return node;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function askKitPrompt(opts: KitPromptOptions): Promise<Record<string, unknown> | null> {
|
|
65
|
+
if (typeof document === 'undefined' || document.body === null) return Promise.resolve(null);
|
|
66
|
+
const doc = opts.at?.ownerDocument ?? document;
|
|
67
|
+
return new Promise((resolve) => {
|
|
68
|
+
let settled = false;
|
|
69
|
+
let unwatch: () => void = () => {};
|
|
70
|
+
const finish = (value: Record<string, unknown> | null): void => {
|
|
71
|
+
if (settled) return;
|
|
72
|
+
settled = true;
|
|
73
|
+
unwatch();
|
|
74
|
+
modal.close();
|
|
75
|
+
modal.destroy();
|
|
76
|
+
resolve(value);
|
|
77
|
+
};
|
|
78
|
+
const modal = mountKitModal({
|
|
79
|
+
title: opts.title ?? kitText('soma.kit.prompt_title', undefined, opts.at),
|
|
80
|
+
size: 'sm',
|
|
81
|
+
at: opts.at ?? null,
|
|
82
|
+
showClose: false,
|
|
83
|
+
onClose: () => finish(null),
|
|
84
|
+
}, doc);
|
|
85
|
+
|
|
86
|
+
const readers = new Map<string, Reader>();
|
|
87
|
+
const errors = new Map<string, HTMLElement>();
|
|
88
|
+
const defaults = opts.defaults ?? {};
|
|
89
|
+
let uid = 0;
|
|
90
|
+
|
|
91
|
+
for (const f of opts.fields as Field[]) {
|
|
92
|
+
if (typeof f.name !== 'string' || f.name === '') continue;
|
|
93
|
+
uid += 1;
|
|
94
|
+
const id = `kit-prompt-${uid}-${f.name}`;
|
|
95
|
+
const wrap = mk(doc, 'div', FIELD_CLASS);
|
|
96
|
+
const label = mk(doc, 'label', LABEL_CLASS, f.label ?? f.name);
|
|
97
|
+
label.id = `${id}-label`;
|
|
98
|
+
if (f.required === true) {
|
|
99
|
+
const star = mk(doc, 'span', STAR_CLASS, ' *');
|
|
100
|
+
star.setAttribute('aria-hidden', 'true');
|
|
101
|
+
label.appendChild(star);
|
|
102
|
+
}
|
|
103
|
+
wrap.appendChild(label);
|
|
104
|
+
const initial = defaults[f.name];
|
|
105
|
+
const err = mk(doc, 'p', HIDDEN);
|
|
106
|
+
err.setAttribute('role', 'alert');
|
|
107
|
+
errors.set(f.name, err);
|
|
108
|
+
|
|
109
|
+
if (f.type === 'select') {
|
|
110
|
+
const options = f.options ?? [];
|
|
111
|
+
let current: unknown = initial !== undefined && options.some((o) => o.value === initial) ? initial : undefined;
|
|
112
|
+
const group = mk(doc, 'div', GROUP_CLASS);
|
|
113
|
+
group.setAttribute('role', 'radiogroup');
|
|
114
|
+
group.setAttribute('aria-labelledby', label.id);
|
|
115
|
+
const buttons: HTMLButtonElement[] = [];
|
|
116
|
+
const paint = (): void => {
|
|
117
|
+
buttons.forEach((b, i) => {
|
|
118
|
+
const on = options[i]!.value === current;
|
|
119
|
+
b.setAttribute('aria-checked', on ? 'true' : 'false');
|
|
120
|
+
b.className = on ? RADIO_ON : RADIO_OFF;
|
|
121
|
+
// roving tabindex:已选项(没有选中则第一项)进 Tab 序
|
|
122
|
+
b.tabIndex = on || (current === undefined && i === 0) ? 0 : -1;
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
options.forEach((o, i) => {
|
|
126
|
+
const b = mk(doc, 'button', RADIO_OFF, o.label);
|
|
127
|
+
b.type = 'button';
|
|
128
|
+
b.setAttribute('role', 'radio');
|
|
129
|
+
b.addEventListener('click', () => { current = o.value; paint(); });
|
|
130
|
+
b.addEventListener('keydown', (e: KeyboardEvent) => {
|
|
131
|
+
const last = options.length - 1;
|
|
132
|
+
let next = -1;
|
|
133
|
+
if (e.key === 'ArrowDown' || e.key === 'ArrowRight') next = i === last ? 0 : i + 1;
|
|
134
|
+
else if (e.key === 'ArrowUp' || e.key === 'ArrowLeft') next = i === 0 ? last : i - 1;
|
|
135
|
+
else if (e.key === 'Home') next = 0;
|
|
136
|
+
else if (e.key === 'End') next = last;
|
|
137
|
+
if (next < 0) return;
|
|
138
|
+
e.preventDefault();
|
|
139
|
+
current = options[next]!.value;
|
|
140
|
+
paint();
|
|
141
|
+
buttons[next]!.focus();
|
|
142
|
+
});
|
|
143
|
+
buttons.push(b);
|
|
144
|
+
group.appendChild(b);
|
|
145
|
+
});
|
|
146
|
+
paint();
|
|
147
|
+
wrap.appendChild(group);
|
|
148
|
+
readers.set(f.name, { read: () => current, blank: () => current === undefined, focus: () => (buttons.find((b) => b.tabIndex === 0) ?? buttons[0])?.focus() });
|
|
149
|
+
} else if (f.type === 'switch') {
|
|
150
|
+
let on = initial === true;
|
|
151
|
+
const sw = mk(doc, 'button', SWITCH_OFF);
|
|
152
|
+
sw.type = 'button';
|
|
153
|
+
sw.setAttribute('role', 'switch');
|
|
154
|
+
sw.setAttribute('aria-labelledby', label.id);
|
|
155
|
+
const knob = mk(doc, 'span', KNOB_OFF);
|
|
156
|
+
sw.appendChild(knob);
|
|
157
|
+
const paint = (): void => {
|
|
158
|
+
sw.setAttribute('aria-checked', on ? 'true' : 'false');
|
|
159
|
+
sw.className = on ? SWITCH_ON : SWITCH_OFF;
|
|
160
|
+
knob.className = on ? KNOB_ON : KNOB_OFF;
|
|
161
|
+
};
|
|
162
|
+
paint();
|
|
163
|
+
sw.addEventListener('click', () => { on = !on; paint(); });
|
|
164
|
+
wrap.appendChild(sw);
|
|
165
|
+
readers.set(f.name, { read: () => on, blank: () => false, focus: () => sw.focus() });
|
|
166
|
+
} else if (f.type === 'textarea') {
|
|
167
|
+
const area = mk(doc, 'textarea', AREA_CLASS);
|
|
168
|
+
area.id = id;
|
|
169
|
+
area.rows = 4;
|
|
170
|
+
area.value = typeof initial === 'string' || typeof initial === 'number' ? String(initial) : '';
|
|
171
|
+
area.placeholder = f.placeholder ?? '';
|
|
172
|
+
label.htmlFor = id;
|
|
173
|
+
wrap.appendChild(area);
|
|
174
|
+
readers.set(f.name, { read: () => area.value, blank: () => area.value.trim() === '', focus: () => area.focus() });
|
|
175
|
+
} else {
|
|
176
|
+
const input = mk(doc, 'input', INPUT_CLASS);
|
|
177
|
+
input.id = id;
|
|
178
|
+
input.type = f.type === 'number' ? 'number' : 'text';
|
|
179
|
+
input.autocomplete = 'off';
|
|
180
|
+
input.value = typeof initial === 'string' || typeof initial === 'number' ? String(initial) : '';
|
|
181
|
+
input.placeholder = f.placeholder ?? '';
|
|
182
|
+
label.htmlFor = id;
|
|
183
|
+
wrap.appendChild(input);
|
|
184
|
+
readers.set(f.name, {
|
|
185
|
+
read: () => {
|
|
186
|
+
if (f.type !== 'number') return input.value;
|
|
187
|
+
const n = Number(input.value);
|
|
188
|
+
return input.value.trim() === '' || !Number.isFinite(n) ? undefined : n;
|
|
189
|
+
},
|
|
190
|
+
blank: () => (f.type === 'number' ? input.value.trim() === '' || !Number.isFinite(Number(input.value)) : input.value.trim() === ''),
|
|
191
|
+
focus: () => input.focus(),
|
|
192
|
+
});
|
|
193
|
+
input.addEventListener('keydown', (e: KeyboardEvent) => {
|
|
194
|
+
if (e.key === 'Enter' && !e.isComposing) { e.preventDefault(); submit(); }
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
wrap.appendChild(err);
|
|
198
|
+
modal.body.appendChild(wrap);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const submit = (): void => {
|
|
202
|
+
let firstBad: Reader | null = null;
|
|
203
|
+
for (const f of opts.fields as Field[]) {
|
|
204
|
+
const r = readers.get(f.name);
|
|
205
|
+
const err = errors.get(f.name);
|
|
206
|
+
if (r === undefined || err === undefined) continue;
|
|
207
|
+
const bad = f.required === true && r.blank();
|
|
208
|
+
err.textContent = bad ? kitText('soma.kit.prompt_required', undefined, opts.at) : '';
|
|
209
|
+
err.className = bad ? ERROR_CLASS : HIDDEN;
|
|
210
|
+
if (bad && firstBad === null) firstBad = r;
|
|
211
|
+
}
|
|
212
|
+
if (firstBad !== null) { (firstBad as Reader).focus(); return; }
|
|
213
|
+
const out: Record<string, unknown> = {};
|
|
214
|
+
for (const f of opts.fields as Field[]) {
|
|
215
|
+
const v = readers.get(f.name)?.read();
|
|
216
|
+
if (v !== undefined) out[f.name] = v;
|
|
217
|
+
}
|
|
218
|
+
finish(out);
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
const cancel = mk(doc, 'button', `${BUTTON_BASE} ${BUTTON_NEUTRAL}`, kitText('soma.kit.cancel', undefined, opts.at));
|
|
222
|
+
cancel.type = 'button';
|
|
223
|
+
cancel.addEventListener('click', () => finish(null));
|
|
224
|
+
const ok = mk(doc, 'button', `${BUTTON_BASE} ${BUTTON_PRIMARY}`, opts.submitLabel ?? kitText('soma.kit.confirm', undefined, opts.at));
|
|
225
|
+
ok.type = 'button';
|
|
226
|
+
ok.addEventListener('click', submit);
|
|
227
|
+
modal.footer.append(cancel, ok);
|
|
228
|
+
|
|
229
|
+
(opts.at?.isConnected === true ? opts.at.parentNode ?? doc.body : doc.body).appendChild(modal.layer);
|
|
230
|
+
modal.open();
|
|
231
|
+
// 兜底:打开期间宿主区域被卸载(层锚点脱离文档)时按取消收口,调用方的 await 不会永久挂起
|
|
232
|
+
unwatch = watchKitModalDetached(modal.layer, doc, () => finish(null));
|
|
233
|
+
(readers.values().next().value as Reader | undefined)?.focus();
|
|
234
|
+
});
|
|
235
|
+
}
|