使用高级Chakra UI组件更快地构建 💎

了解更多
跳至内容
文档演示指南博客
赞助

评分

用于以可视化形式显示评论和评分。

StorybookRecipeArk

用法

import { RatingGroup } from "@chakra-ui/react"
<RatingGroup.Root>
  <RatingGroup.Label />
  <RatingGroup.HiddenInput />
  <RatingGroup.Control>
    <RatingGroup.Item>
      <RatingGroup.ItemIndicator />
    </RatingGroup.Item>
  </RatingGroup.Control>
</RatingGroup.Root>
信息
如果您偏好封闭的组件组合,请查看下面的代码片段

快捷方式

Rating 组件还为常见用例提供了一组快捷方式。

RatingControl

此组件渲染在 count prop 中指定的评分项数量。

这有效

<RatingGroup.Control>
  {Array.from({ length: 5 }).map((_, index) => (
    <RatingGroup.Item key={index} index={index + 1}>
      <RatingGroup.ItemIndicator />
    </RatingGroup.Item>
  ))}
</RatingGroup.Control>

如果您不需要自定义评分图标,这可能更简洁

<RatingGroup.Control />

示例

基本

尺寸

使用 size prop 来改变评分组件的尺寸。

受控

使用 valueonValueChange prop 来控制评分值。

状态管理

控制评分的另一种方法是使用 RootProvider 组件和 useRatingGroup 状态管理钩子。

这样你就可以在组件外部访问评分状态和方法。

只读

使用 readOnly prop 使评分组件变为只读。

Hook 表单

以下是评分组件与 react-hook-form 一起使用的示例。

自定义图标

使用 icon prop 将自定义图标传递给评分组件。这会覆盖默认的星形图标。

标签

渲染 RatingGroup.Label 组件,为评分组件提供一个人类可读的标签。

半星

使用 allowHalf prop 允许半星评分。

表情符号

使用表情符号组合评分组件。

😡😠😐😊😍

颜色

使用 colorPalette prop 来改变评分的颜色

灰色

红色

绿色

蓝色

青色

粉色

紫色

青色

橙色

黄色

用户评价

使用评分组件来显示用户评价。

Sage 是一位优秀的软件工程师。他非常专业且知识渊博。

MJ

Matthew Jones

首席技术官,公司

封闭组件

以下是为封闭组件组合设置评分组件的方法。

import { RatingGroup } from "@chakra-ui/react"
import * as React from "react"

export interface RatingProps extends RatingGroup.RootProps {
  icon?: React.ReactElement
  count?: number
  label?: React.ReactNode
}

export const Rating = React.forwardRef<HTMLDivElement, RatingProps>(
  function Rating(props, ref) {
    const { icon, count = 5, label, ...rest } = props
    return (
      <RatingGroup.Root ref={ref} count={count} {...rest}>
        {label && <RatingGroup.Label>{label}</RatingGroup.Label>}
        <RatingGroup.HiddenInput />
        <RatingGroup.Control>
          {Array.from({ length: count }).map((_, index) => (
            <RatingGroup.Item key={index} index={index + 1}>
              <RatingGroup.ItemIndicator icon={icon} />
            </RatingGroup.Item>
          ))}
        </RatingGroup.Control>
      </RatingGroup.Root>
    )
  },
)

以下是如何使用它

<Rating defaultValue={3} size="sm" />

属性

属性默认类型
count '5'
数字

评分的总数。

colorPalette 'gray'
'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink'

组件的颜色调色板

size 'md'
'xs' | 'sm' | 'md' | 'lg'

组件的尺寸

allowHalf
布尔值

是否允许半星。

asChild
布尔值

使用提供的子元素作为默认渲染元素,结合它们的属性和行为。

更多详情,请阅读我们的 组合 指南。
autoFocus
布尔值

是否自动聚焦评分。

defaultValue
数字

评分组首次渲染时的初始值。在您不需要控制评分组状态时使用。

disabled
布尔值

评分是否禁用。

form
字符串

关联的底层输入元素表单。

id
字符串

机器的唯一标识符。

ids
Partial<{ root: string label: string hiddenInput: string control: string item(id: string): string }>

评分中元素的ID。用于组合时很有用。

name
字符串

评分元素的名称属性(在表单中使用)。

onHoverChange
(details: HoverChangeDetails) => void

当评分值被悬停时调用的函数。

onValueChange
(details: ValueChangeDetails) => void

当评分值改变时调用的函数。

readOnly
布尔值

评分是否只读。

required
布尔值

评分是否为必填项。

translations
IntlTranslations

指定用于标识无障碍元素及其状态的本地化字符串

value
数字

当前评分值。

as
React.ElementType

要渲染的底层元素。

unstyled
布尔值

是否移除组件的样式。

属性默认类型
index *
数字

asChild
布尔值

使用提供的子元素作为默认渲染元素,结合它们的属性和行为。

更多详情,请阅读我们的 组合 指南。

上一个

单选框

下一个

分段控制