# 评分

- 页面: https://jiguang132.com/zh/resources/material-ui/docs/rating
- 资源: Material UI (https://jiguang132.com/zh/resources/material-ui.md)
- 全部指南: https://jiguang132.com/zh/resources/material-ui/docs.md

选择或展示数值评分，支持精度与只读状态。

本指南覆盖默认 Emotion 环境的 Material UI Core；扩展包与额外示例按清单单独标注。

## 使用场景与业务边界

评分可能为空；控件本身不会提交或保存评分。

## 原生组成与范围

`Rating`

保持 Material UI 原生组合与类型，不替换为 JGuang／Base UI／Radix 同名 API。示例条目不是额外组件。

## 原生安装与初始化

```bash
npm install @mui/material@9.4.0 @emotion/react@11.14.0 @emotion/styled@11.14.1
```

采用原生 npm 包与 Emotion，不提供虚构的 JGuang UI Registry 地址。将示例放在原生 ThemeProvider 内，按项目选择 CssBaseline 或 ScopedCssBaseline。先检查 React、现有 Provider、锁文件与 SSR 配置；不要覆盖用户文件或忽略 peer 冲突。本批实际验收 React 19.2.8；React 18 及以下须匹配 react-is。完整初始化规则见随附的双语 setup 文档。

## 关键原生参数

仅解释本指南选出的关键参数，不是平铺的通用 API。原生继承来源、slot 结构、可空值与泛型仍按固定源码保留。

| 所属组件.属性 | 用途说明 | 声明来源 |
| --- | --- | --- |
| `Rating.value` | 受控原生值，其单值／数组／可空类型取决于所属组件。 | `Rating` |
| `Rating.precision` | Rating 数值步进，包含受支持的小数评分。 | `Rating` |
| `Rating.readOnly` | 原生只读状态，须与 disabled 区分。 | `Rating` |
| `Rating.onChange` | 所属控件的原生回调，保留其准确事件／数值／可空签名。 | `Rating` |
| `Rating.getLabelText` | 格式化原生评分值的可访问标签。 | `Rating` |

## 可运行原生示例

下列机器键只选择示例入口，不会作为通用 variant 属性传入每个组件。原生示例 JSX 与数据保持上游原文；JGuang 编写的验收示例明确标注。

- 基础评分 · `BasicRating` · 上游原生示例
- 评分精度 · `HalfRating` · 上游原生示例
- 悬停反馈 · `HoverRating` · 上游原生示例
- 多个尺寸 · `RatingSize` · 上游原生示例
- 定制样式 · `CustomizedRating` · 上游原生示例
- 单选组 · `RadioGroupRating` · 上游原生示例
- 无障碍 · `TextRating` · 上游原生示例

## 真实使用示例

固定版本的未修改原生示例；复制时同时保留所需的局部辅助文件。

```tsx
import * as React from 'react';
import Box from '@mui/material/Box';
import Rating from '@mui/material/Rating';
import Typography from '@mui/material/Typography';

export default function BasicRating() {
  const [value, setValue] = React.useState<number | null>(2);

  return (
    <Box sx={{ '& > legend': { mt: 2 } }}>
      <Typography component="legend">Controlled</Typography>
      <Rating
        name="simple-controlled"
        value={value}
        onChange={(event, newValue) => {
          setValue(newValue);
        }}
      />
      <Typography component="legend">Uncontrolled</Typography>
      <Rating
        name="simple-uncontrolled"
        onChange={(event, newValue) => {
          console.log(newValue);
        }}
        defaultValue={2}
      />
      <Typography component="legend">Read only</Typography>
      <Rating name="read-only" value={value} readOnly />
      <Typography component="legend">Disabled</Typography>
      <Rating name="disabled" value={value} disabled />
      <Typography component="legend">No rating given</Typography>
      <Rating name="no-value" value={null} />
    </Box>
  );
}

```

## 示例依赖边界

`react`, `@mui/material/Box`, `@mui/material/Rating`, `@mui/material/Typography`, `@mui/material/Stack`, `@mui/icons-material/Star`, `@mui/material/styles`, `@mui/icons-material/Favorite`, `@mui/icons-material/FavoriteBorder`, `@mui/icons-material/SentimentVeryDissatisfied`, `@mui/icons-material/SentimentDissatisfied`, `@mui/icons-material/SentimentSatisfied`, `@mui/icons-material/SentimentSatisfiedAltOutlined`, `@mui/icons-material/SentimentVerySatisfied`

直接导入 Icons／System／Utils 的示例需要匹配 9.4.0 包。局部数据与辅助文件属于示例闭包，不是额外组件。远程图片和模拟操作不提供后端。

## 固定上游 API 元数据参考

以下表格保留上游元数据，不是重新推断的通用类型体系。条件泛型、继承属性与回调细节以以下源码声明为准。主题默认配置可能覆盖元数据默认值。

### Rating

```tsx
import Rating from '@mui/material/Rating';
import { Rating } from '@mui/material';
```

| 属性 | 必需 | 上游元数据类型／约束 | 元数据默认值 |
| --- | --- | --- | --- |
| `classes` | 否 | `object` | — |
| `component` | 否 | `elementType` | — |
| `defaultValue` | 否 | `number` | `null` |
| `disabled` | 否 | `bool` | `false` |
| `emptyIcon` | 否 | `node` | `<StarBorder fontSize="inherit" />` |
| `emptyLabelText` | 否 | `node` | `'Empty'` |
| `getLabelText` | 否 | `func` | `function defaultLabelText(value) { return `${value \|\| '0'} Star${value !== 1 ? 's' : ''}`; }` |
| `highlightSelectedOnly` | 否 | `bool` | `false` |
| `icon` | 否 | `node` | `<Star fontSize="inherit" />` |
| `max` | 否 | `number` | `5` |
| `name` | 否 | `string` | — |
| `onChange` | 否 | `func` | — |
| `onChangeActive` | 否 | `func` | — |
| `precision` | 否 | `number` | `1` |
| `readOnly` | 否 | `bool` | `false` |
| `size` | 否 | `'small' \| 'medium' \| 'large' \| string` | `'medium'` |
| `slotProps` | 否 | `{ decimal?: func \| object, icon?: func \| object, label?: func \| object, root?: func \| object }` | `{}` |
| `slots` | 否 | `{ decimal?: elementType, icon?: elementType, label?: elementType, root?: elementType }` | `{}` |
| `sx` | 否 | `Array<func \| object \| bool> \| func \| object` | — |
| `value` | 否 | `number` | — |


## 原生源码声明

声明与 import 从固定源码提取，不平铺联合类型或外部继承。英文注释／类型属于原始源码参考，不代表上游正文已全文翻译。共享类型仍位于原生包中。

### vendor/src/Rating/Rating.d.ts

```ts
import * as React from 'react';

import { SxProps } from '@mui/system';

import { OverridableStringUnion } from '@mui/types';

import { Theme } from '../styles';

import { RatingClasses } from './ratingClasses';

import { OverridableComponent, OverrideProps } from '../OverridableComponent';

import { CreateSlotsAndSlotProps, SlotProps } from '../utils/types';

export interface IconContainerProps extends React.HTMLAttributes<HTMLSpanElement> {
  value: number;
}

export interface RatingPropsSizeOverrides {}

export interface RatingRootSlotPropsOverrides {}

export interface RatingLabelSlotPropsOverrides {}

export interface RatingIconSlotPropsOverrides {}

export interface RatingDecimalSlotPropsOverrides {}

export interface RatingSlots {
  /**
   * The component used for the root slot.
   * @default 'span'
   */
  root: React.ElementType;
  /**
   * The component used for the label slot.
   * @default 'label'
   */
  label: React.ElementType;
  /**
   * The component used for the icon slot.
   * @default 'span'
   */
  icon: React.ElementType;
  /**
   * The component used for the decimal slot.
   * @default 'span'
   */
  decimal: React.ElementType;
}

export type RatingSlotsAndSlotProps = CreateSlotsAndSlotProps<
  RatingSlots,
  {
    /**
     * Props forwarded to the root slot.
     * By default, the available props are based on the span element.
     */
    root: SlotProps<'span', RatingRootSlotPropsOverrides, RatingOwnerState>;
    /**
     * Props forwarded to the label slot.
     * By default, the available props are based on the label element.
     */
    label: SlotProps<'label', RatingLabelSlotPropsOverrides, RatingOwnerState>;
    /**
     * Props forwarded to the icon slot.
     * By default, the available props are based on the span element.
     */
    icon: SlotProps<'span', RatingIconSlotPropsOverrides, RatingOwnerState>;
    /**
     * Props forwarded to the decimal slot.
     * By default, the available props are based on the span element.
     */
    decimal: SlotProps<'span', RatingDecimalSlotPropsOverrides, RatingOwnerState>;
  }
>;

export interface RatingOwnProps extends RatingSlotsAndSlotProps {
  /**
   * Override or extend the styles applied to the component.
   */
  classes?: Partial<RatingClasses> | undefined;
  /**
   * The default value. Use when the component is not controlled.
   * @default null
   */
  defaultValue?: number | undefined;
  /**
   * If `true`, the component is disabled.
   * @default false
   */
  disabled?: boolean | undefined;
  /**
   * The icon to display when empty.
   * @default <StarBorder fontSize="inherit" />
   */
  emptyIcon?: React.ReactNode;
  /**
   * The label read when the rating input is empty.
   * @default 'Empty'
   */
  emptyLabelText?: React.ReactNode;
  /**
   * Accepts a function which returns a string value that provides a user-friendly name for the current value of the rating.
   * This is important for screen reader users.
   *
   * For localization purposes, you can use the provided [translations](https://mui.com/material-ui/guides/localization/).
   * @param {number} value The rating label's value to format.
   * @returns {string}
   * @default function defaultLabelText(value) {
   *   return `${value || '0'} Star${value !== 1 ? 's' : ''}`;
   * }
   */
  getLabelText?: ((value: number) => string) | undefined;
  /**
   * If `true`, only the selected icon will be highlighted.
   * @default false
   */
  highlightSelectedOnly?: boolean | undefined;
  /**
   * The icon to display.
   * @default <Star fontSize="inherit" />
   */
  icon?: React.ReactNode;
  /**
   * Maximum rating.
   * @default 5
   */
  max?: number | undefined;
  /**
   * The name attribute of the radio `input` elements.
   * This input `name` should be unique within the page.
   * Being unique within a form is insufficient since the `name` is used to generate IDs.
   */
  name?: string | undefined;
  /**
   * Callback fired when the value changes.
   * @param {React.SyntheticEvent} event The event source of the callback.
   * @param {number|null} value The new value.
   */
  onChange?: ((event: React.SyntheticEvent, value: number | null) => void) | undefined;
  /**
   * Callback function that is fired when the hover state changes.
   * @param {React.SyntheticEvent} event The event source of the callback.
   * @param {number} value The new value.
   */
  onChangeActive?: ((event: React.SyntheticEvent, value: number) => void) | undefined;
  /**
   * The minimum increment value change allowed.
   * @default 1
   */
  precision?: number | undefined;
  /**
   * Removes all hover effects and pointer events.
   * @default false
   */
  readOnly?: boolean | undefined;
  /**
   * The size of the component.
   * @default 'medium'
   */
  size?: OverridableStringUnion<'small' | 'medium' | 'large', RatingPropsSizeOverrides> | undefined;
  /**
   * The system prop that allows defining system overrides as well as additional CSS styles.
   */
  sx?: SxProps<Theme> | undefined;
  /**
   * The rating value.
   */
  value?: number | null | undefined;
}

export interface RatingOwnerState extends Omit<RatingProps, 'slots' | 'slotProps'> {}

export type RatingTypeMap<
  AdditionalProps = {},
  RootComponent extends React.ElementType = 'span',
> = {
  props: AdditionalProps & RatingOwnProps;
  defaultComponent: RootComponent;
};

declare const Rating: OverridableComponent<RatingTypeMap>;

export type RatingProps<
  RootComponent extends React.ElementType = RatingTypeMap['defaultComponent'],
  AdditionalProps = {},
> = OverrideProps<RatingTypeMap<AdditionalProps, RootComponent>, RootComponent> & {
  component?: React.ElementType | undefined;
};
```

## 安装后验收

执行严格类型检查、生产构建和实际交互。检查键盘标签／焦点、原生支持的禁用／错误／只读语义、深浅色、窄屏和减少动画。按真实宿主数据检查回调，不把复制提示词当作安装完成。初始化工具还需检查服务端 HTML 与无告警水合。

## 来源与许可

https://github.com/mui/material-ui/tree/809a7717b4c050ba3f69b75300689f07c050a16e

MIT · @mui/material@9.4.0. 保留上游原始内容与许可证。上游完整原文保留于 vendor/docs，不标为全文中文化。

---

JGuang 全部资源索引: https://jiguang132.com/llms.txt
