Rating Group
Allows users to rate items using a set of icons.
Anatomy
To set up the rating correctly, you'll need to understand its anatomy and how we name its parts.
Each part includes a
data-partattribute to help identify them in the DOM.
Examples
Learn how to use the RatingGroup component in your project. Let's take a look at the most basic example:
Example not foundExample not foundExample not foundExample not foundUsing half ratings
Allow 0.5 value steps by setting the allowHalf prop to true. Ensure to render the correct icon if the isHalf
value is set in the Rating components render callback.
Example not foundExample not foundExample not foundExample not foundUsing a default value
Example not foundExample not foundExample not foundExample not foundControlled
When using the RatingGroup component, you can use the value and onValueChange props to control the state.
Example not foundExample not foundExample not foundExample not foundDisabling the rating group
To make the rating group disabled, set the disabled prop to true.
Example not foundExample not foundExample not foundExample not foundReadonly rating group
To make the rating group readonly, set the readOnly prop to true.
Example not foundExample not foundExample not foundExample not foundUsage within forms
To use the rating group within forms, pass the prop name. It will render a hidden input and ensure the value changes
get propagated to the form correctly.
Example not foundExample not foundExample not foundExample not foundUsing the Field Component
The Field component helps manage form-related state and accessibility attributes of a rating group. It includes
handling ARIA labels, helper text, and error text to ensure proper accessibility.
Example not foundExample not foundExample not foundExample not foundUsing the Root Provider
The RootProvider component provides a context for the rating-group. It accepts the value of the useRating-group
hook. You can leverage it to access the component state and methods from outside the rating-group.
Example not foundExample not foundExample not foundExample not foundIf you're using the
RootProvidercomponent, you don't need to use theRootcomponent.
API Reference
Props
Root
| Prop | Default | Type |
|---|---|---|
allowHalf | booleanWhether to allow half stars. | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. | |
autoFocus | booleanWhether to autofocus the rating. | |
count | 5 | numberThe total number of ratings. |
defaultValue | numberThe initial value of the rating when rendered. Use when you don't need to control the value of the rating. | |
disabled | booleanWhether the rating is disabled. | |
form | stringThe associate form of the underlying input element. | |
id | stringThe 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 | stringThe name attribute of the rating element (used in forms). | |
onHoverChange | (details: HoverChangeDetails) => voidFunction to be called when the rating value is hovered. | |
onValueChange | (details: ValueChangeDetails) => voidFunction to be called when the rating value changes. | |
readOnly | booleanWhether the rating is readonly. | |
required | booleanWhether the rating is required. | |
translations | IntlTranslationsSpecifies the localized strings that identifies the accessibility elements and their states | |
value | numberThe controlled value of the rating |
Control
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | rating-group |
[data-part] | control |
[data-readonly] | Present when read-only |
[data-disabled] | Present when disabled |
HiddenInput
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Item
| Prop | Default | Type |
|---|---|---|
index | number | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | rating-group |
[data-part] | item |
[data-disabled] | Present when disabled |
[data-readonly] | Present when read-only |
[data-checked] | Present when checked |
[data-highlighted] | Present when highlighted |
[data-half] |
Label
| Prop | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
| Data Attribute | Value |
|---|---|
[data-scope] | rating-group |
[data-part] | label |
[data-disabled] | Present when disabled |
[data-required] | Present when required |
RootProvider
| Prop | Default | Type |
|---|---|---|
value | UseRatingGroupReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. For more details, read our Composition guide. |
Context
These are the properties available when using UratingUgroup.Context, useUratingUgroupContext hook or useUratingUgroup hook.
API
| Property | Type |
|---|---|
setValue | (value: number) => voidSets the value of the rating group |
clearValue | VoidFunctionClears the value of the rating group |
hovering | booleanWhether the rating group is being hovered |
value | numberThe current value of the rating group |
hoveredValue | numberThe value of the currently hovered rating |
count | numberThe total number of ratings |
items | number[]The array of rating values. Returns an array of numbers from 1 to the max value. |
getItemState | (props: ItemProps) => ItemStateReturns the state of a rating item |
Accessibility
Keyboard Support
| Key | Description |
|---|---|
ArrowRight | Moves focus to the next star, increasing the rating value based on the `allowHalf` property. |
ArrowLeft | Moves focus to the previous star, decreasing the rating value based on the `allowHalf` property. |
Enter | Selects the focused star in the rating group. |