Leave Yamada UI a star

Star
Yamada UIYamada UIv1.7.2

Rating

Rating is a component used to allow users to provide ratings.

Source@yamada-ui/rating

Import

import { Rating } from "@yamada-ui/react"
Copied!

Usage

Editable example

<Rating />
Copied!

Change Size

Editable example

<VStack>
  <For each={["xs", "sm", "md", "lg", "xl"]}>
    {(size, index) => <Rating key={index} size={size} defaultValue={3} />}
  </For>
</VStack>
Copied!

Change Color Scheme

Editable example

<VStack>
  <For each={["purple", "pink"]}>
    {(colorScheme, index) => (
      <Rating key={index} colorScheme={colorScheme} defaultValue={3} />
    )}
  </For>
</VStack>
Copied!

Set Default Value

To set a default value, assign a number to defaultValue.

Editable example

<Rating defaultValue={3} />
Copied!

Change Number of Items

To change the number of items, assign a number to items.

Editable example

<VStack>
  <For each={[4, 5, 6]}>
    {(items, index) => <Rating key={index} items={items} />}
  </For>
</VStack>
Copied!

Allow Decimals

To allow decimals, assign a number to fractions.

For example, to allow 0.25, set 4.

Editable example

<VStack>
  <For
    each={[
      {
        fractions: 2,
        defaultValue: 1.5,
      },
      {
        fractions: 3,
        defaultValue: 2.33,
      },
      {
        fractions: 4,
        defaultValue: 3.75,
      },
    ]}
  >
    {({ fractions, defaultValue }, index) => (
      <Rating key={index} fractions={fractions} defaultValue={defaultValue} />
    )}
  </For>
</VStack>
Copied!

Change Color

To change the color, assign a string or function to color. color provides the numerical value of the item.

Editable example

const getColor = (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>
)
Copied!

Customize Icons

To customize icons, assign RectNode or a function to emptyIcon and filledIcon. emptyIcon and filledIcon provide the numerical value of the item.

  • emptyIcon: Used for unselected icons.
  • filledIcon: Used for selected icons.

Editable example

const getColor = (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 = (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>
)
Copied!

Disable

To disable, set isDisabled to true.

Editable example

<VStack>
  <Rating isDisabled defaultValue={3} />

  <Fieldset isDisabled legend="How satisfied are you with Yamada UI?">
    <Rating defaultValue={3} />
  </Fieldset>
</VStack>
Copied!

Make Read-Only

To make read-only, set isReadOnly to true.

Editable example

<VStack>
  <Rating isReadOnly defaultValue={3} />

  <Fieldset isReadOnly legend="How satisfied are you with Yamada UI?">
    <Rating defaultValue={3} />
  </Fieldset>
</VStack>
Copied!

Control

Editable example

const [value, onChange] = useState<number>(3)

return <Rating value={value} onChange={onChange} />
Copied!

Use React Hook Form

Editable example

type Data = { rating: boolean }

const {
  control,
  handleSubmit,
  watch,
  formState: { errors },
} = useForm<Data>()

const onSubmit: SubmitHandler<Data> = (data) => console.log("submit:", data)

console.log("watch:", watch())

return (
  <VStack as="form" onSubmit={handleSubmit(onSubmit)}>
    <Fieldset
      isInvalid={!!errors.rating}
      legend="How satisfied are you with Yamada UI?"
      errorMessage={errors.rating ? errors.rating.message : undefined}
    >
      <Controller
        name="rating"
        control={control}
        render={({ field }) => <Rating {...field} />}
      />
    </Fieldset>

    <Button type="submit" alignSelf="flex-end">
      Submit
    </Button>
  </VStack>
)
Copied!

Edit this page on GitHub

PreviousEditableNextSegmentedControl