Rating

A row of icons the user sets as a rating - or, read-only, a display of one.

Demos

For usage examples, visit the component demo page: Rating

Import

import { Rating } from '@soroush.tech/design-system/Rating'

Props

PropTypeDefaultDescription
valuenumber | null-The rating - null for none. Controlled with onChange.
defaultValuenumber | nullnullThe rating at first, when uncontrolled.
onChange(event, value: number | null) => void-Fired when the rating changes - null when it is cleared.
onChangeActive(event, value: number) => void-Fired as the hovered value changes - -1 when the pointer leaves.
maxnumber5How many items.
precisionnumber1The least step - 0.5 for halves. The rating is rounded to it.
highlightSelectedOnlybooleanfalseOnly the chosen item is filled, not every one up to it.
iconReactNodethe star iconThe filled item.
emptyIconReactNodethe star_border iconThe empty item.
getLabelText(value: number) => stringits numberThe accessible name of an item, and of a read-only rating.
emptyLabelTextstring-Names the "no rating" radio, which clears the rating from the keys. None is drawn without it.
readOnlybooleanfalseA display: role="img", named by getLabelText, not focusable, taking no pointer.
sizekeyof Theme['sizes']its FormControl's, 'md'The icons' size - theme.icon.
colorkeyof Theme['palette']its FormControl's, 'warning'The filled items' color.
disabledbooleanits FormControl'sDraws the radios off: nothing is submitted, the pointer does nothing.
requiredbooleanits FormControl'sMarks the radios required, and the group aria-required.
errorbooleanits FormControl'sThe empty items take the error color, and the group is aria-invalid.
namestringgeneratedThe radios' name - what a form submits the rating as.
idstringits FormControl'sOn the group.
aria-labelstring-Names the group - or its FormLabel does, in a FormControl.

It takes the styled-system space props, className and data-testid too.

Precision

With precision below 1 each item holds the steps in it - halves at 0.5 - and the pointer picks the nearest one. A rating is rounded to its precision: 2.3 at 0.5 is 2.5.

Keys and the pointer

Tab focuses the current rating (or the first item when there is none); the arrow keys move between items, as native radios do. While the pointer is over the rating it sets the value, and a key and a press at once is the press. Pressing the chosen item again clears it - onChange(event, null) - but a key doesn't: give emptyLabelText for the keys' way to no rating.

In a FormControl

Its FormLabel names the group (aria-labelledby - a for would turn a click on the label into a press on an item), its FormHelperText describes it, and the field's size, color, error, disabled and required apply, an own prop winning:

<FormControl error>
  <FormLabel>Quality</FormLabel>
  <Rating emptyLabelText="No rating" />
  <FormHelperText>Choose how it was.</FormHelperText>
</FormControl>

Customization

theme.components.Rating styles the root, each item (the label of an icon), the icon, the decimal holding a fractional item's steps, the visually hidden input and text, and the emptyLabel. Its defaultProps set max, precision, highlightSelectedOnly, size and color.

Source code

If this page does not answer your question, have a look at the implementation of the component for more detail.