Rating
Rating
is a component used to allow users to provide ratings.
<Rating />
Usage
import { Rating } from "@yamada-ui/react"
import { Rating } from "@/components/ui"
import { Rating } from "@workspaces/ui"
<Rating />
Change Size
<VStack>
<For each={["xs", "sm", "md", "lg", "xl"]}>
{(size) => <Rating key={size} size={size} defaultValue={3} />}
</For>
</VStack>
Change Color Scheme
<VStack>
<For each={["success", "warning"]}>
{(colorScheme) => (
<Rating key={colorScheme} colorScheme={colorScheme} defaultValue={3} />
)}
</For>
</VStack>
Set Default Value
To set a default value, assign a number to defaultValue
.
<Rating defaultValue={5} />
Change Number of Items
To change the number of items, assign a number to count
.
<VStack>
<For each={[4, 5, 6]}>{(count) => <Rating key={count} count={count} />}</For>
</VStack>
Set Decimals
To set decimals, assign a number to fractions
.
For example, to set 0.25
, set 4
.
<VStack>
<For
each={[
{ fractions: 2, defaultValue: 1.5 },
{ fractions: 3, defaultValue: 2.33 },
{ fractions: 4, defaultValue: 3.75 },
]}
>
{({ fractions, defaultValue }) => (
<Rating
key={fractions}
fractions={fractions}
defaultValue={defaultValue}
/>
)}
</For>
</VStack>
Highlight Selected Only
To highlight only the selected rating, set highlightSelectedOnly
to true
.
<Rating defaultValue={3} highlightSelectedOnly />
Change Color
To change the color, assign a string or function to color
.
const getColor = useCallback((value: number) => {
switch (value) {
case 1:
return "red.500"
case 2:
return "orange.500"
case 3:
return "yellow.500"
case 4:
return "green.500"
case 5:
return "blue.500"
default:
return undefined
}
}, [])
return (
<VStack>
<Rating color="green.500" defaultValue={3} />
<Rating color={getColor} defaultValue={3} />
</VStack>
)
Disable
To disable, set disabled
to true
.
<Rating disabled defaultValue={3} />
Read-Only
To make it read-only, set readOnly
to true
.
<Rating readOnly defaultValue={3} />
Customize Icons
To customize icons, assign ReactNode
or a function to emptyIcon
and filledIcon
.
const getColor = useCallback((value: number) => {
switch (value) {
case 1:
return "red.500"
case 2:
return "orange.500"
case 3:
return "yellow.500"
case 4:
return "green.500"
case 5:
return "blue.500"
default:
return undefined
}
}, [])
const getIcon = useCallback((value: number) => {
switch (value) {
case 1:
return <AngryIcon />
case 2:
return <FrownIcon />
case 3:
return <SmileIcon />
case 4:
return <LaughIcon />
case 5:
return <SmilePlusIcon />
default:
return null
}
}, [])
return (
<VStack>
<Rating
defaultValue={3}
emptyIcon={<GhostIcon />}
filledIcon={<GhostIcon />}
/>
<Rating
gap="xs"
color={getColor}
emptyIcon={getIcon}
filledIcon={getIcon}
/>
</VStack>
)
Control
const [value, onChange] = useState(3)
return <Rating value={value} onChange={onChange} />
Props
Accessibility
Currently, this section is being updated due to the migration of v2.