Rating

Rating component allows users to pick and display a rating value using a visual star-based interface.

Import

import { Rating } from '@tidbcloud/uikit'

Basic Usage

import { Rating } from '@tidbcloud/uikit'
 
function Demo() {
  return <Rating defaultValue={2} />
}

Controlled

import { useState } from 'react'
import { Rating } from '@tidbcloud/uikit'
 
function Demo() {
  const [value, setValue] = useState(0)
  return <Rating value={value} onChange={setValue} />
}

Read Only

import { Rating } from '@tidbcloud/uikit'
 
function Demo() {
  return <Rating value={3.5} fractions={2} readOnly />
}

Fractions

Use fractions prop to allow fractional ratings:

import { Rating, Stack, Group } from '@tidbcloud/uikit'
 
function Demo() {
  return (
    <Stack>
      <Group>
        <span>Fractions: 2</span>
        <Rating fractions={2} defaultValue={1.5} />
      </Group>
      <Group>
        <span>Fractions: 3</span>
        <Rating fractions={3} defaultValue={2.33} />
      </Group>
      <Group>
        <span>Fractions: 4</span>
        <Rating fractions={4} defaultValue={3.75} />
      </Group>
    </Stack>
  )
}

Custom Symbol

import { Rating } from '@tidbcloud/uikit'
import { IconSun, IconMoon } from '@tabler/icons-react'
 
function Demo() {
  return <Rating emptySymbol={<IconSun size={16} />} fullSymbol={<IconMoon size={16} />} />
}

Custom Symbols Per Item

import { Rating } from '@tidbcloud/uikit'
import { IconMoodCry, IconMoodSad, IconMoodSmile, IconMoodHappy, IconMoodCrazyHappy } from '@tabler/icons-react'
 
function Demo() {
  const getEmptyIcon = (value: number) => {
    const icons = [IconMoodCry, IconMoodSad, IconMoodSmile, IconMoodHappy, IconMoodCrazyHappy]
    const Icon = icons[value - 1]
    return <Icon size={24} />
  }
 
  const getFullIcon = (value: number) => {
    const colors = ['red', 'orange', 'yellow', 'lime', 'green']
    const icons = [IconMoodCry, IconMoodSad, IconMoodSmile, IconMoodHappy, IconMoodCrazyHappy]
    const Icon = icons[value - 1]
    return <Icon size={24} color={`var(--mantine-color-${colors[value - 1]}-7)`} />
  }
 
  return <Rating emptySymbol={getEmptyIcon} fullSymbol={getFullIcon} highlightSelectedOnly />
}

Key Props

PropTypeDefaultDescription
valuenumber-Controlled component value
defaultValuenumber-Uncontrolled component default value
onChange(value: number) => void-Called when value changes
countnumber5Number of rating items
fractionsnumber1Number of fractions each item can be divided into
sizeMantineSize'sm'Controls component size
colorMantineColor-Color of filled symbols
readOnlybooleanfalseIf set, user cannot interact with the component
highlightSelectedOnlybooleanfalseIf set, only the selected symbol changes to full
emptySymbolReactNode | (value) => ReactNode-Icon displayed when symbol is empty
fullSymbolReactNode | (value) => ReactNode-Icon displayed when symbol is full