Rating

import Rating from 'frui/field/Rating';
Copy

Props

The Rating component wraps visually hidden radio inputs for accessibility and form integration. It accepts the following specific props:

NameTypeRequiredNotes
namestringNoName attribute for the radio inputs (form submission). Auto-generated if not provided.
valuenumber | nullNoThe rating value for controlled mode.
defaultValuenumber | nullNo (Defaults to null)The initial rating value for uncontrolled mode.
maxnumberNo (Defaults to 5)The maximum rating value (number of icons).
onChangeFunctionNoCallback fired when the value changes. `(event, value) => void`
onChangeActiveFunctionNoCallback fired when the mouse hovers over a rating icon. `(event, value) => void`
readOnlybooleanNo (Defaults to false)If true, the rating cannot be changed.
disabledbooleanNo (Defaults to false)If true, the rating is disabled (visual state and interaction).
size'small' | 'medium' | 'large'No (Defaults to medium)The size of the rating icons.
iconReactNodeNoThe icon to display as the filled state.
emptyIconReactNodeNoThe icon to display as the empty state (defaults to faded filled icon).
getLabelTextFunctionNoGenerates aria-label text for accessibility. `(value) => string`
highlightSelectedOnlybooleanNo (Defaults to false)If true, only the selected icon will be highlighted, not the preceding ones.
classNamestringNoStandard HTML class names for the root span element.
styleCSS ObjectNoStandard CSS object for inline styles on the root span element.

Basics

By default, the Rating component is uncontrolled. Use defaultValue to set an initial value. It renders 5 stars.

<Rating name="basic-rating" defaultValue={3} />
Copy

Controlled

For a controlled component, use the value, and onChange  props, typically with React state.

Current Value: 2
function ControlledRatingExample() {
  const [controlledValue, setControlledValue] = useState<number | null>(2);

  return (
    <Rating
      name="controlled-rating"
      value={controlledValue}
      onChange={(event, newValue) => {
        setControlledValue(newValue);
        console.log('Controlled Change:', newValue);
      }}
    />
  );
}
Copy

Sizes

Use the size prop to adjust the icon size. The sizes correspond to CSS classes  .frui-rating-sizeSmall,  .frui-rating-sizeMedium, and  .frui-rating-sizeLarge.

small:
medium (default):
large:
<Rating defaultValue={3} size="small" />
<Rating defaultValue={3} size="medium" />
<Rating defaultValue={3} size="large" />
Copy

Custom Icons

Provide custom React nodes to the icon (filled) and emptyIcon props. If emptyIcon is not provided, a faded version of the icon is used. Styles target  .frui-rating-icon-filled, and  .frui-rating-icon-empty.

// Define custom icons (examples)
const HeartIcon = (props) => (/* SVG code */);
const CircleIcon = (props) => (/* SVG code */);

// Use in Rating
<Rating
  defaultValue={3}
  icon={<HeartIcon />}
  emptyIcon={<HeartIcon style={{ opacity: 0.3 }} />}
  max={5}
/>

<Rating
  defaultValue={4}
  icon={<CircleIcon />}
  emptyIcon={<CircleIcon style={{ opacity: 0.3 }} />}
  max={6}
/>
Copy

Highlighting

By default, all icons up to the selected/hovered value are filled. Use highlightSelectedOnly to only fill the single selected/hovered icon.

Default:
Highlight Selected Only:
{/* Default: Icons 1, 2, 3 are filled */}
<Rating defaultValue={3} />

{/* highlightSelectedOnly: Only icon 3 is filled */}
<Rating defaultValue={3} highlightSelectedOnly />
Copy

Read Only & Disabled

Use readOnly to display a rating that cannot be changed by the user ( .frui-rating-readOnly class). Use disabled to prevent interaction and apply disabled styling ( .frui-rating-disabled class).

Read Only:
Disabled:
<Rating value={4} readOnly />
<Rating value={2} disabled />
Copy

Events

The onChange callback fires when a rating is selected. The onChangeActive  callback fires when the mouse enters or leaves an icon, providing the hovered value (or null). Hover styles are mainly handled via CSS ( .frui-rating-icon-hover).

Hovered Value (via onChangeActive): null
<Rating
  name="event-rating"
  defaultValue={1}
  onChange={(event, value) => alert(`onChange: Value ${value} selected!`)}
  onChangeActive={(event, value) => console.log(`onChangeActive: Hovered ${value}`)}
/>
Copy

onChange

The onChange event is triggered when the user clicks an icon to change the rating. The following arguments are passed to the event handler:

NameTypeDescription
eventChangeEvent<HTMLInputElement>The change event on the underlying radio input.
valuenumber | nullThe newly selected rating value.

onChangeActive

The onChangeActive event is triggered when the mouse pointer enters or leaves an icon. The following arguments are passed to the event handler:

NameTypeDescription
eventMouseEventThe native mouse event.
valuenumber | nullThe value of the icon being hovered, or null if the mouse leaves the component.

You can add custom CSS classes via the className prop or inline styles via the style prop to the root  .frui-rating-root element. Component-specific classes like  .frui-rating-icon,  .frui-rating-icon-filled,  .frui-rating-icon-empty,  .frui-rating-icon-hover,  .frui-rating-icon-active, and size/state classes are available for more targeted styling.