import { RatingGroup } from "@chakra-ui/react"
const Demo = () => {
return (
<RatingGroup.Root count={5} defaultValue={3} size="sm">
<RatingGroup.HiddenInput />
<RatingGroup.Control />
</RatingGroup.Root>
)
}
用法
¥Usage
import { RatingGroup } from "@chakra-ui/react"
<RatingGroup.Root>
<RatingGroup.Label />
<RatingGroup.HiddenInput />
<RatingGroup.Control>
<RatingGroup.Item>
<RatingGroup.ItemIndicator />
</RatingGroup.Item>
</RatingGroup.Control>
</RatingGroup.Root>
如果你更喜欢封闭的组件组合,请查看 以下代码片段。
¥If you prefer a closed component composition, check out the snippet below.
快捷键
¥Shortcuts
Rating
组件还提供了一组用于常见用例的快捷方式。
¥The Rating
component also provides a set of shortcuts for common use cases.
RatingControl
此组件渲染 count
属性中指定的评分项目数量。
¥This component renders the number of rating items specified in the count
prop.
有效:
¥This works:
<RatingGroup.Control>
{Array.from({ length: 5 }).map((_, index) => (
<RatingGroup.Item key={index} index={index + 1}>
<RatingGroup.ItemIndicator />
</RatingGroup.Item>
))}
</RatingGroup.Control>
如果你不需要自定义评级图标,这可能会更简洁:
¥This might be more concise, if you don't need to customize the rating icons:
<RatingGroup.Control />
示例
¥Examples
基础
¥Basic
import { RatingGroup } from "@chakra-ui/react"
const Demo = () => {
return (
<RatingGroup.Root count={5} defaultValue={3} size="sm">
<RatingGroup.HiddenInput />
<RatingGroup.Control />
</RatingGroup.Root>
)
}
尺寸
¥Sizes
使用 size
属性更改评级组件的大小。
¥Use the size
prop to change the size of the rating component.
import { For, RatingGroup, Stack } from "@chakra-ui/react"
const Demo = () => {
return (
<Stack>
<For each={["xs", "sm", "md", "lg"]}>
{(size) => (
<RatingGroup.Root key={size} count={5} defaultValue={3} size={size}>
<RatingGroup.HiddenInput />
<RatingGroup.Control />
</RatingGroup.Root>
)}
</For>
</Stack>
)
}
受控
¥Controlled
使用 value
和 onValueChange
属性控制评级值。
¥Use the value
and onValueChange
prop to control the rating value.
"use client"
import { RatingGroup } from "@chakra-ui/react"
import { useState } from "react"
const Demo = () => {
const [value, setValue] = useState(3)
return (
<RatingGroup.Root
count={5}
value={value}
onValueChange={(e) => setValue(e.value)}
>
<RatingGroup.HiddenInput />
<RatingGroup.Control />
</RatingGroup.Root>
)
}
商店
¥Store
控制评分的另一种方法是使用 RootProvider
组件和 useRatingGroup
存储钩子。
¥An alternative way to control the rating is to use the RootProvider
component
and the useRatingGroup
store hook.
这样,你就可以从组件外部访问评级状态和方法。
¥This way you can access the rating state and methods from outside the component.
"use client"
import { RatingGroup, useRatingGroup } from "@chakra-ui/react"
const Demo = () => {
const store = useRatingGroup({ count: 5, defaultValue: 3 })
return (
<RatingGroup.RootProvider value={store} size="sm">
<RatingGroup.HiddenInput />
<RatingGroup.Control />
</RatingGroup.RootProvider>
)
}
ReadOnly
使用 readOnly
属性使评级组件为只读。
¥Use the readOnly
prop to make the rating component read-only.
import { RatingGroup } from "@chakra-ui/react"
const Demo = () => {
return (
<RatingGroup.Root readOnly count={5} defaultValue={3} size="sm">
<RatingGroup.HiddenInput />
<RatingGroup.Control />
</RatingGroup.Root>
)
}
钩子表单
¥Hook Form
以下是如何将评级与 react-hook-form
结合使用的示例。
¥Here's an example of how to use rating with react-hook-form
.
"use client"
import { Button, Field, RatingGroup, Stack } from "@chakra-ui/react"
import { zodResolver } from "@hookform/resolvers/zod"
import { Controller, useForm } from "react-hook-form"
import { z } from "zod"
const formSchema = z.object({
rating: z.number({ required_error: "Rating is required" }).min(1).max(5),
})
type FormValues = z.infer<typeof formSchema>
const Demo = () => {
const {
handleSubmit,
formState: { errors },
control,
} = useForm<FormValues>({
resolver: zodResolver(formSchema),
})
const onSubmit = handleSubmit((data) => console.log(data))
return (
<form onSubmit={onSubmit}>
<Stack gap="4" align="flex-start">
<Field.Root invalid={!!errors.rating}>
<Field.Label>Rating</Field.Label>
<Controller
control={control}
name="rating"
render={({ field }) => (
<RatingGroup.Root
count={5}
name={field.name}
value={field.value}
onValueChange={({ value }) => field.onChange(value)}
>
<RatingGroup.HiddenInput />
<RatingGroup.Control />
</RatingGroup.Root>
)}
/>
<Field.ErrorText>{errors.rating?.message}</Field.ErrorText>
</Field.Root>
<Button size="sm" type="submit">
Submit
</Button>
</Stack>
</form>
)
}
自定义图标
¥Custom Icon
使用 icon
属性将自定义图标传递给评级组件。这将覆盖默认的星形图标。
¥Use the icon
prop to pass a custom icon to the rating component. This will
override the default star icon.
import { RatingGroup } from "@chakra-ui/react"
import { IoHeart } from "react-icons/io5"
const Demo = () => {
return (
<RatingGroup.Root count={5} defaultValue={4} colorPalette="red">
<RatingGroup.HiddenInput />
<RatingGroup.Control>
{Array.from({ length: 5 }).map((_, index) => (
<RatingGroup.Item key={index} index={index + 1}>
<RatingGroup.ItemIndicator icon={<IoHeart />} />
</RatingGroup.Item>
))}
</RatingGroup.Control>
</RatingGroup.Root>
)
}
标签
¥Label
渲染 RatingGroup.Label
组件,为评级组件提供人性化的标签。
¥Render the RatingGroup.Label
component to provide a human-readable label for
the rating component.
import { RatingGroup } from "@chakra-ui/react"
const Demo = () => {
return (
<RatingGroup.Root count={5} defaultValue={3} size="sm" gap="4">
<RatingGroup.HiddenInput />
<RatingGroup.Label>Rating</RatingGroup.Label>
<RatingGroup.Control />
</RatingGroup.Root>
)
}
半星号
¥Half Star
使用 allowHalf
属性可允许半星评分。
¥Use the allowHalf
prop to allow half-star ratings.
import { RatingGroup } from "@chakra-ui/react"
const Demo = () => {
return (
<RatingGroup.Root allowHalf count={5} defaultValue={3.5} size="sm">
<RatingGroup.HiddenInput />
<RatingGroup.Control />
</RatingGroup.Root>
)
}
表情符号
¥Emoji
编写带有表情符号的评级组件。
¥Compose the rating component with emojis.
import { RatingGroup } from "@chakra-ui/react"
const emojiMap: Record<string, string> = {
1: "😡",
2: "😠",
3: "😐",
4: "😊",
5: "😍",
}
const Demo = () => {
return (
<RatingGroup.Root count={5} defaultValue={3}>
<RatingGroup.Control>
{Array.from({ length: 5 }).map((_, index) => (
<RatingGroup.Item
key={index}
index={index + 1}
minW="9"
filter={{ base: "grayscale(1)", _checked: "revert" }}
transition="scale 0.1s"
_hover={{ scale: "1.1" }}
>
{emojiMap[index + 1]}
</RatingGroup.Item>
))}
</RatingGroup.Control>
</RatingGroup.Root>
)
}
颜色
¥Colors
使用 colorPalette
属性更改评级的颜色
¥Use the colorPalette
prop to change the color of the rating
gray
red
green
blue
teal
pink
purple
cyan
orange
yellow
import { RatingGroup, Stack, Text } from "@chakra-ui/react"
import { colorPalettes } from "compositions/lib/color-palettes"
const Demo = () => {
return (
<Stack gap="2" align="flex-start">
{colorPalettes.map((colorPalette) => (
<Stack
align="center"
key={colorPalette}
direction="row"
gap="10"
px="4"
>
<Text minW="8ch">{colorPalette}</Text>
<RatingGroup.Root
count={5}
defaultValue={3}
size="sm"
colorPalette={colorPalette}
>
<RatingGroup.HiddenInput />
<RatingGroup.Control />
</RatingGroup.Root>
</Stack>
))}
</Stack>
)
}
用户评价
¥Testimonial
使用评分组件显示用户评价。
¥Use the rating component to show testimonials.
Sage is a great software engineer. He is very professional and knowledgeable.

Matthew Jones
CTO, Company
import { Avatar, HStack, RatingGroup, Stack, Text } from "@chakra-ui/react"
const Demo = () => {
return (
<Stack maxW="320px" gap="4">
<RatingGroup.Root
colorPalette="orange"
readOnly
count={5}
defaultValue={5}
size="xs"
>
<RatingGroup.HiddenInput />
<RatingGroup.Control />
</RatingGroup.Root>
<Text>
Sage is a great software engineer. He is very professional and
knowledgeable.
</Text>
<HStack gap="4">
<Avatar.Root>
<Avatar.Fallback name="Matthew Jones" />
<Avatar.Image src="https://randomuser.me/api/portraits/men/70.jpg" />
</Avatar.Root>
<Stack textStyle="sm" gap="0">
<Text fontWeight="medium">Matthew Jones</Text>
<Text color="fg.muted">CTO, Company</Text>
</Stack>
</HStack>
</Stack>
)
}
已关闭组件
¥Closed Component
以下是如何设置封闭组件组合的评级。
¥Here's how to setup the Rating for a closed component composition.
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>
)
},
)
以下是如何使用它
¥Here's how to use the it
<Rating defaultValue={3} size="sm" />
属性
¥Props
根元素
¥Root
Prop | Default | Type |
---|---|---|
count | '5' | number The total number of ratings. |
colorPalette | 'gray' | 'gray' | 'red' | 'orange' | 'yellow' | 'green' | 'teal' | 'blue' | 'cyan' | 'purple' | 'pink' The color palette of the component |
size | 'md' | 'xs' | 'sm' | 'md' | 'lg' The size of the component |
as | React.ElementType The underlying element to render. | |
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
unstyled | boolean Whether to remove the component's style. | |
allowHalf | boolean Whether to allow half stars. | |
autoFocus | boolean Whether to autofocus the rating. | |
defaultValue | number The initial value of the rating when rendered. Use when you don't need to control the value of the rating. | |
disabled | boolean Whether the rating is disabled. | |
form | string The associate form of the underlying input element. | |
id | string The unique identifier of the machine. | |
ids | Partial<{
root: string
label: string
hiddenInput: string
control: string
item: (id: string) => string
}> The ids of the elements in the rating. Useful for composition. | |
name | string The name attribute of the rating element (used in forms). | |
onHoverChange | (details: HoverChangeDetails) => void Function to be called when the rating value is hovered. | |
onValueChange | (details: ValueChangeDetails) => void Function to be called when the rating value changes. | |
readOnly | boolean Whether the rating is readonly. | |
required | boolean Whether the rating is required. | |
translations | IntlTranslations Specifies the localized strings that identifies the accessibility elements and their states | |
value | number The controlled value of the rating |
项目
¥Item
Prop | Default | Type |
---|---|---|
index * | number | |
as | React.ElementType The underlying element to render. | |
asChild | boolean Use the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |