ChoiceCard 选项卡片
以图标、标题和说明行作答、可分步翻页的问题卡片
安装
pnpm add @talex-touch/tuffex
import { TxChoiceCard } from '@talex-touch/tuffex/choice-card'
import '@talex-touch/tuffex/choice-card/style.css'
// 卡片内部渲染 TxIcon 与 TxSkeleton,它们的样式表需单独引入
import '@talex-touch/tuffex/icon/style.css'
import '@talex-touch/tuffex/skeleton/style.css'
import '@talex-touch/tuffex/base.css' // 设计令牌与重置样式,全应用引入一次
用法
单个问题
selected 标出已选项;设置 disabled 的选项不可选。
示例加载中...
分步引导
宿主在 select 中更新 v-model:step 翻页;columns="2" 让选项两个一行。
示例加载中...
加载中
loading 在选项行自己的盒子里绘制骨架行,选项到达时页面不跳动。
示例加载中...
最佳实践
- 每步只问一个问题,给三到六个答案。
- 在
select中更新v-model:step翻页;selected传当前页的答案,回到上一步时仍能看到。 loading经useDeferredLoading绑定,loadingRows设为即将到来的答案数。- 需要前置条件的选项保留为禁用,并在说明里写明条件。
- 按页面语言传入
prevLabel、nextLabel、stepLabel;卡片只带英文默认值。
API 参考
属性
| 属性名 | 类型 | 默认值 | 说明 |
|---|---|---|---|
steps | ChoiceStep[] | - | 各页,必填;为空且不在加载时不渲染。 |
step | number | undefined | 当前页(从 0 起),配合 v-model:step;越界时取最近的有效页。 |
selected | string | undefined | 当前页要标出的答案 id。 |
loading | boolean | false | 把选项换成骨架行,卡片加 aria-busy;标题与分页保留。 |
loadingRows | number | 3 | loading 时绘制的骨架行数。 |
columns | 1 | 2 | 1 | 2 时选项两个一行;卡片窄于 480px 时退回一列。 |
appear | boolean | true | 首次渲染与 loading 结束时选项逐行浮现。 |
prevLabel | string | 'Previous' | 后退箭头的可访问名称。 |
nextLabel | string | 'Next' | 前进箭头的可访问名称。 |
stepLabel | (current: number, total: number) => string | `${current} / ${total}` | 分页计数文字,两个数字都从 1 开始。 |
事件
| 事件名 | 参数 | 说明 |
|---|---|---|
select | (payload: ChoiceSelectPayload) | 点击或用 Enter / 空格激活可用选项时触发;卡片不翻页。 |
update:step | (index: number) | 箭头翻到第 index 页(从 0 起)时触发。 |
插槽
| 插槽 | 参数 | 说明 |
|---|---|---|
header | { step: ChoiceStep, stepIndex: number, total: number } | 替换默认 <h3> 标题;其内容即卡片的名称,需保留问题。 |
类型
ChoiceStep,steps 中的一项:
| 字段 | 类型 | 说明 |
|---|---|---|
id | string | 页的标识;当前页换成新 id 时重放翻页入场。必填。 |
title | string | 问题,即卡片标题与答案列表的名称。必填。 |
options | ChoiceOption[] | 各答案。必填。 |
ChoiceOption,一个答案:
| 字段 | 类型 | 默认值 | 说明 |
|---|---|---|---|
id | string | - | 页内唯一标识,也是 selected 比对的值。必填。 |
label | string | - | 选项标题与可访问名称。必填。 |
description | string | - | 标题下的一行说明,作为选项描述朗读。 |
icon | TxIconSource | string | - | TxIcon 图标源,或图标类名如 'i-carbon-edit'。 |
disabled | boolean | false | 暗淡显示;方向键跳过,点击不派发事件。 |
import type {
ChoiceCardColumns, // 1 | 2
ChoiceCardEmits,
ChoiceCardProps,
ChoiceOption,
ChoiceSelectPayload, // { step, stepIndex, option }
ChoiceStep,
ChoiceStepLabelFormatter, // (current, total) => string
TxChoiceCardInstance,
} from '@talex-touch/tuffex/choice-card'
CSS 变量
| 变量 | 说明 |
|---|---|
--tx-choice-card-pad | 卡片边缘到选项的内距,默认 8px;卡片圆角随之变大。 |
--tx-choice-card-option-radius | 选项圆角,默认 10px;卡片圆角为它加内距,保持同心。 |
--tx-choice-card-option-pad-x | 选项与标题的水平内距,默认 12px。 |
--tx-choice-card-label-line | 标题行高与图标框高度,默认 20px。 |
--tx-choice-card-desc-line | 说明行高,默认 18px。 |
这些变量可设在卡片或任意祖先元素上;组件只在每行写入 --tx-choice-card-index(入场错落序号)。
概述
- 语义:以问题命名的
<section>,选项是<ul role="list">中的原生<button>,名称取自标题、描述取自说明;已选项打勾并带aria-current="true",颜色不是唯一标记。 select从不翻页。不传step时卡片自行记页并派发update:step;至少两页才渲染分页,计数是role="status"区域。- 键盘:列表只占一个 Tab 落点(已选项,否则第一个可用项);上下键循环移动,两列时左右键按阅读顺序移动,Home / End 到首尾,跳过禁用项。
- 翻页时焦点若在列表内,移到新页的落点;正在使用的箭头变为禁用时,焦点移到另一个箭头。
- 两列在卡片窄于 480px 时退回一列(容器查询),方向键按实际渲染的列数移动。
- 入场与翻页动画都是新节点上的 CSS 动画,选项第一帧即可点击;减少动态效果时不播放。
技术实现
- 骨架行沿用选项行自己的容器,内放
TxSkeleton骨架条。 - 源码:
packages/tuffex/packages/components/src/choice-card/。
查看源码
packages/tuffex/packages/components/src/choice-card/index.ts
使用场景
- 助手的开场引导:「我们先从哪件事开始?」,每步一个问题。
- 卡片出现后才到达的「为你准备」推荐列表。
- 回看已回答的问题:
selected标出当时的选择。
相关组件
| 组件 | 适用于 |
|---|---|
| SuggestionChips | 单行追问建议 |
| RecommendationCard | 单个推荐答案与备选 |
| ApprovalCard | 多问题作答后统一发送 |