Rating

View as Markdown

Stars to give a score, or to show an average.

How did Priya do?

When to use

Stars to give a score, or to show an average. Use Slider instead when the score isn't a few stars.

Usage

import { Rating } from "./ui/uicontext/components/Rating.ts"
const Message = UiSchema.TaggedEnum({ Rated: { stars: Schema.NullOr(Schema.Number) } })export const Review = (h: Html.H<typeof Message.Type>, stars: number | null) =>	Rating(h, { label: "Your rating", name: "rating", showLabel: true, value: stars, onChange: (next) => Message.Rated({ stars: next }) })

Examples

Showing a score

RatingDisplay draws an average, fills part of a star for the fraction, and reads as “4.6 out of 5, from 1,284 ratings”.

  • Walnut desk organiser4.6(1,284)
  • Linen apron, oat4.1(87)
  • Cast iron grill pan, 28 cm3.4(12)
  • Stoneware mug5.0(1)

API reference

RatingView function

Options

Rating options
NameTypeDefaultDescription
labelrequiredstring—The question, as the group's legend: "How was your stay?"
showLabelboolean—Show the label above the stars; it's still read out when hidden.
valuerequirednumber | null—The stars given, or null for none yet. The caller keeps it.
onChangerequired(value: number | null) => Message——
maxnumber5How many stars; 5 by default.
wordsstring[]—Words for each score, shown beside the stars and read with them: ["Poor", …, "Excellent"].
clearableboolean—Pressing the picked star again clears the rating.
disabledboolean——
namerequiredstring—Submits the value under this form field name.
styleStyle——

RatingDisplayView function

Options

RatingDisplay options
NameTypeDescription
valuerequirednumberThe average, such as 4.6.
maxnumber—
countnumberHow many ratings it's from; shown as "(1,284)".
localestringThe reader's locale, for the numbers.
styleStyle—

Keyboard and accessibility

KeysDoes
Right arrow Left arrowGive one star more or less
TabReach the stars once, on the score given

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/rating

Source