HextaUI

Rating (UI SDK): Controlled

Pass value and onValueChange, where null means no score yet. A controller from createRatingController() sets the score from anywhere.

No score

"use client"

import * as React from "react"

import { Button } from "@/components/ui/button"
import {
  createRatingController,
  Rating,
  RatingItem,
} from "@/components/ui/ui-sdk-rating"

function UiSdkRatingControlled() {
  const [rating] = React.useState(createRatingController)
  const [value, setValue] = React.useState<number | null>(null)

  return (
    <div className="flex flex-col items-center gap-3">
      <Rating
        aria-label="Rating"
        controller={rating}
        value={value}
        onValueChange={setValue}
      >
        <RatingItem />
      </Rating>
      <div className="flex gap-2">
        <Button variant="outline" size="sm" onClick={() => rating.setValue(5)}>
          Rate 5
        </Button>
        <Button
          variant="ghost"
          size="sm"
          disabled={value === null}
          onClick={() => setValue(null)}
        >
          Clear
        </Button>
      </div>
      <p className="text-sm text-muted-foreground tabular-nums">
        {value === null ? "No score" : "Score: " + value}
      </p>
    </div>
  )
}

export { UiSdkRatingControlled }

This adds the example as components/examples/ui-sdk-rating/controlled.tsx, together with the Rating component and the HextaUI theme tokens it needs.

pnpm dlx shadcn@latest add https://hextaui.com/r/ui-sdk-rating-controlled.json

To add only the component, follow the Rating installation.

Props in this example

The Rating props this example sets, from the API reference.

PropTypeDefault
value
number | null–
onValueChange
(value: number | null, details) => void–
controllerFrom createRatingController().
RatingController–

Every prop is in the Rating API reference.

See them all, with keyboard support and accessibility notes, on the Rating page.