# Rating

> 以星级收集评分，或展示可读取的评价摘要，支持半星、清除与表单组合。

网页来源：/components/rating

版本：@quirekit/ui 0.3.1；源码修订：d79a6b48d00e+worktree。此文档对应当前源码构建，接入方式以开始使用为准。

接入前置：[开始使用](/raw/components/getting-started.md)（Vue、样式、主题与图标 Provider）。

用星级收集评分或展示评价摘要。`v-model` 保存数字，`0` 表示尚未评分；评分统计和保存由调用方处理。

## 基本用法

`label` 提供始终可读的评分名称，默认显示五颗星和当前分数。`clearable` 添加独立清除按钮；`defaultValue` 设置非受控初始值。

### 示例：rating/basic

文件：`basic.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireRating } from '@quirekit/ui/vue';
const score = ref(0);
</script>

<template>
  <div data-rating-basic class="grid min-w-0 gap-3 p-qk-surface">
    <p class="m-0 text-qk-helper text-qk-text-secondary">这份会议纪要模板是否容易使用？</p>
    <QuireRating v-model="score" label="模板体验" clearable />
    <p role="status" class="m-0 text-qk-helper text-qk-text-tertiary">{{ score ? `当前评分：${score} 分` : '选择星级记录你的体验。' }}</p>
  </div>
</template>
```

## 半星与评分数量

`allowHalf` 将鼠标左右半区和方向键步长改为半分。`count` 设置评分总数，默认 `5`；建议使用容易理解的五分或十分制。

### 示例：rating/half

文件：`half.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireRating } from '@quirekit/ui/vue';
const halfScore = ref(3.5);
const tenPointScore = ref(7);
</script>

<template>
  <div data-rating-half class="grid min-w-0 gap-5 p-qk-surface">
    <QuireRating v-model="halfScore" label="版式清晰度" allow-half clearable />
    <QuireRating v-model="tenPointScore" label="推荐意愿" :count="10" size="sm" />
  </div>
</template>
```

## 只读摘要与禁用

`readOnly` 保留当前评分、键盘阅读和表单提交。`disabled` 禁止修改并从原生提交中排除；`hideLabel` 隐藏独立标题，仍保留名称关联。`value` 插槽可组合评价数等内容。

### 示例：rating/states

文件：`states.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireRating, QuireSwitch } from '@quirekit/ui/vue';
const locked = ref(true);
const score = ref(3);
</script>

<template>
  <div data-rating-states class="grid min-w-0 gap-5 p-qk-surface">
    <div class="grid min-w-0 gap-2">
      <h3 class="m-0 text-qk-body font-semibold">项目周报模板</h3>
      <QuireRating label="模板平均评分" hide-label :default-value="4.5" allow-half read-only size="sm">
        <template #value="{ value }">{{ value }} 分 · 28 条评价</template>
      </QuireRating>
      <p class="m-0 text-qk-helper text-qk-text-secondary">标题、工作进度和风险事项清楚分区，适合每周同步。</p>
    </div>
    <QuireRating v-model="score" label="你的评分" :disabled="locked" clearable />
    <QuireSwitch v-model:checked="locked" label="评价已截止" description="关闭后可以继续修改你的评分。" />
  </div>
</template>
```

## 图形尺寸

`size="sm | md | lg"` 调整星形大小，默认 `md`。`sm` 适合摘要，`lg` 适合需要强调的评分入口；`:show-value="false"` 可隐藏旁边分数，仍保留可访问描述。

### 示例：rating/sizes

文件：`sizes.vue`

```vue
<script setup lang="ts">
import { QuireRating } from '@quirekit/ui/vue';
</script>

<template>
  <div data-rating-sizes class="grid min-w-0 gap-5 p-qk-surface">
    <QuireRating label="紧凑评分 · sm" :default-value="3" size="sm" />
    <QuireRating label="标准评分 · md" :default-value="3" size="md" />
    <QuireRating label="强调评分 · lg" :default-value="3" size="lg" />
  </div>
</template>
```

## 原生表单

`name` 提交数字分数，未评分提交 `0`。`required` 要求正分数，`form` 可关联外部表单；原生重置恢复初始值。在 [Field](/raw/components/field.md) 内绑定 `controlProps`，使用字段的标签和反馈。

### 示例：rating/native-form

文件：`native-form.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireButton, QuireField, QuireRating } from '@quirekit/ui/vue';
const output = ref('尚未提交');

/** Reads real native form data after required validation succeeds.
 * @param event - Native form submission.
 */
function submit(event: Event): void {
  const data = new FormData(event.currentTarget as HTMLFormElement);
  output.value = `体验 ${data.get('experience')} 分；摘要 ${data.get('summary')} 分；禁用项${data.has('locked') ? '已提交' : '未提交'}`;
}
</script>

<template>
  <form data-rating-native class="grid min-w-0 gap-5 p-qk-surface" @submit.prevent="submit">
    <QuireField v-slot="{ controlProps }" label="使用体验" description="至少选择半分后提交。" required width="100%">
      <QuireRating v-bind="controlProps" name="experience" allow-half clearable />
    </QuireField>
    <QuireRating label="已发布的评分摘要" name="summary" :default-value="4.5" allow-half read-only />
    <QuireRating label="暂不可用的评价项" name="locked" :default-value="2" disabled />
    <div class="flex flex-wrap gap-2">
      <QuireButton type="submit" variant="solid" color="primary">提交评分</QuireButton>
      <QuireButton type="reset" variant="outline">重置评分</QuireButton>
    </div>
    <p role="status" class="m-0 text-qk-helper text-qk-text-secondary">{{ output }}</p>
  </form>
</template>
```

## Form 与校验反馈

在 [Form](/raw/components/form.md) 中通过 FormField 接入模型和 `invalid` 反馈。因为 `0` 表示未评分，必填业务规则应明确校验 `value > 0`。

### 示例：rating/form

文件：`form.vue`

```vue
<script setup lang="ts">
import { ref } from 'vue';
import { QuireButton, QuireForm, QuireFormField, QuireRating, QuireTextarea } from '@quirekit/ui/vue';
import type { QuireKitFormRule, QuireKitFormValues } from '@quirekit/ui/vue';
const initialValues = { score: 0, comment: '' };
const saved = ref('尚未保存评价');

/** Zero is an explicit unanswered rating, so the score rule requires a positive number.
 * @param value - Form-owned score.
 * @returns A valid result or useful feedback.
 */
const positiveScore: QuireKitFormRule = value => Number(value) > 0 || '请选择至少半分。';

/** Stores a local feedback snapshot; applications can replace this with their own request.
 * @param values - Validated form snapshot.
 */
function save(values: QuireKitFormValues): void {
  saved.value = `已保存 ${values.score} 分评价${values.comment ? `：${values.comment}` : ''}`;
}
</script>

<template>
  <div data-rating-form class="grid min-w-0 gap-4 p-qk-surface">
    <QuireForm :initial-values="initialValues" :on-submit="save" aria-label="模板评价表单">
      <QuireFormField v-slot="{ controlProps, value }" name="score" label="整体体验" required
        description="评价只保存在当前示例中。" :rules="positiveScore">
        <QuireRating v-bind="controlProps" :model-value="Number(value ?? 0)" allow-half clearable />
      </QuireFormField>
      <QuireFormField v-slot="{ controlProps, value }" name="comment" label="补充建议">
        <QuireTextarea v-bind="controlProps" :model-value="String(value ?? '')" :rows="2" width="100%" placeholder="哪些细节值得改进？" />
      </QuireFormField>
      <template #actions="{ resetForm }">
        <QuireButton type="submit" color="primary" variant="solid">保存评价</QuireButton>
        <QuireButton type="button" variant="outline" @click="resetForm()">恢复初始值</QuireButton>
      </template>
    </QuireForm>
    <p role="status" class="m-0 text-qk-helper text-qk-text-secondary">{{ saved }}</p>
  </div>
</template>
```

## 键盘与可访问名称

| 按键 | 行为 |
| --- | --- |
| `Tab` | 进入选中星或未评分时的首星，随后离开星组。 |
| `←` / `↑` | 减少一分，启用半星时减少半分；最低为零。 |
| `→` / `↓` | 增加一分，启用半星时增加半分；最高为 `count`。 |
| `Home` / `End` | 选择一分或满分。 |
| `Space` | 为尚未评分的聚焦项赋分。 |

清除按钮支持常规按钮键盘操作，清除后焦点回到首星。单星名称、分数说明与清除按钮文案跟随 [Locale Provider](/raw/components/locale-provider.md) 的语言；独立使用时需提供 `label` 或有效的外部 `aria-labelledby`。

## API

### QuireRating

#### Props

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| label | 否 | — | Visible accessible label; optional inside QuireField or QuireFormField. |
| hideLabel | 否 | false | Visually hide the standalone label while retaining its association. |
| defaultValue | 否 | 0 | Uncontrolled initial score; zero means no rating. |
| count | 否 | 5 | Total number of stars; normalized to a positive integer. |
| allowHalf | 否 | false | Allow pointer and keyboard selection in half-point increments. |
| size | 否 | "md" | Optical star scale, 16 / 20 / 24px; defaults to md. |
| clearable | 否 | false | Show a separate clear action for editable scores. |
| showValue | 否 | true | Show the committed score beside the stars. |
| id | 否 | — | Native form control identity, including Field label association. |
| name | 否 | — | Native submitted field name; unnamed ratings do not submit a value. |
| form | 否 | — | ID of an external associated native form. |
| disabled | 否 | undefined | Disable input and omit the score from native submission. |
| readOnly | 否 | undefined | Preserve reading, focus and submission while blocking changes. |
| required | 否 | undefined | Require a positive score during native form validation. |
| invalid | 否 | undefined | Reflect external validation failure. |
| modelValue | 否 | — |  |

**label**

```ts
string | undefined
```

```ts
undefined | string
```

**hideLabel**

```ts
boolean | undefined
```

**defaultValue**

```ts
number | undefined
```

```ts
undefined | number
```

**count**

```ts
number | undefined
```

```ts
undefined | number
```

**allowHalf**

```ts
boolean | undefined
```

**size**

```ts
QuireKitRatingSize | undefined
```

```ts
undefined | "md" | "sm" | "lg"
```

**clearable**

```ts
boolean | undefined
```

**showValue**

```ts
boolean | undefined
```

**id**

```ts
string | undefined
```

```ts
undefined | string
```

**name**

```ts
string | undefined
```

```ts
undefined | string
```

**form**

```ts
string | undefined
```

```ts
undefined | string
```

**disabled**

```ts
boolean | undefined
```

**readOnly**

```ts
boolean | undefined
```

**required**

```ts
boolean | undefined
```

**invalid**

```ts
boolean | undefined
```

**modelValue**

```ts
number | undefined
```

```ts
undefined | number
```

#### Events

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| update\:modelValue | 否 | — |  |

**update:modelValue**

```ts
[value: number | undefined]
```

#### Slots

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| value | 否 | — | Replace the visible score summary without changing accessible state. |

**value**

```ts
QuireKitRatingValueSlotProps
```

```ts
{
  /** Current committed score; zero means no rating. */
  value: number;
  /** Maximum score and number of stars. */
  count: number;
}
```

#### 公开实例成员

| 名称 | 必填 | 默认值 | 说明 |
| --- | --- | --- | --- |
| focus | 否 | — |  |

**focus**

```ts
() => void
```

```ts
{ (): void; }
```

#### 相关类型

```ts
type QuireKitRatingValueSlotProps = {
  /** Current committed score; zero means no rating. */
  value: number;
  /** Maximum score and number of stars. */
  count: number;
};
```
