Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

Rating

Since 0.1.0-alpha.21
Lets the user rate with stars, and shows a rating.

A rating is a radio button for each star in a fieldset. It posts with the form, and hovering fills the stars up to the pointer. For a score on a longer scale, use a Slider or a NumberField.

Saving a rating

A rating binds to int or int?, from 1 to Max (5 by default). The value posts as a number with the form.

Static

Preview

Your rating

Click your rating again to clear it.

Code

Usage.razor
@using System.ComponentModel.DataAnnotations

<Form Model="Model" FormName="rating-usage" OnValidSubmit="Save" MaxWidth="24.Rem" Grow>
    <Rating @bind-Value="Model!.Stars" Hint="Click your rating again to clear it." />
    <Row><Button Submit>Save</Button></Row>
    @if (_saved)
    {
        <Alert Status="Status.Success">Saved: @(_stars is { } stars ? $"{stars} of 5 stars" : "no rating").</Alert>
    }
</Form>

@code {
    [SupplyParameterFromForm(FormName = "rating-usage")]
    private ProductReview? Model { get; set; }

    // What was saved, not the field: on an interactive page it follows each click.
    private bool _saved;
    private int? _stars;

    protected override void OnInitialized() => Model ??= new() { Stars = 4 };

    private void Save() => (_saved, _stars) = (true, Model!.Stars);

    public sealed class ProductReview
    {
        [Display(Name = "Your rating")]
        public int? Stars { get; set; }
    }
}

Static: Save posts the chosen star without a full reload.

Add this example to a page with the izzy CLI:

bash
izzy add component Rating --at <file>:<line>

A form post can send any number, so check the range on the server, with [Range(1, 5)]. Before trusting a posted value, see Values a form post can change.

Clearing a rating

Bound to int?, a rating can be empty. Clicking the chosen star clears it, and the arrow keys reach "No rating" before the first star, which screen readers announce. A required rating has no empty choice, as a checked radio would count as an answer.

Bound to int, a rating starts with no star chosen when the value is 0, and can't be cleared once one is.

Showing a rating

To show a rating someone can't change, such as an average, set ReadOnly. It takes any number, double too, and fills the stars to the exact fraction: 4.3 fills 86%.

Icon changes the star, Accent its color, and Max how many there are.

Static

Preview

Average of 128 reviews
Battery life
Spice

Code

Display.razor
@* Read-only ratings show any value, filled to the exact fraction. *@
<Stack Gap="4">
    <Rating Value="4.3" ReadOnly Label="Average of 128 reviews" />
    <Rating Value="2.5" ReadOnly Label="Battery life" Size="ControlSize.Sm" />
    <Rating Value="3" ReadOnly Label="Spice" Icon="Lucide.Flame" Accent="Accent.Danger" Max="4" />
</Stack>

Screen readers hear one image, "4.3 out of 5 stars". In a Form, a read-only rating posts its value.

Sizes and disabled

To line a rating up with other fields, set Size. A Disabled rating can't be changed and doesn't post. In a read-only Form, a rating shows as a display.

Labels and localization

The names screen readers hear for the stars come from the app's text. To set them for the whole app, see Text and other languages.

Labels and localization
IzzyTextSetsDefault
RatingOneStarThe first star's name"1 star"
RatingStarsThe other stars' names; {0} is the number"{0} stars"
RatingNoneThe empty choice's name"No rating"
RatingOutOfA read-only rating's name; {0} is the value, {1} the number of stars"{0} out of {1} stars"

Mode support

What works in each mode, Rating
FeatureStaticInteractive
A radio for each star: arrow keys, posted with the formYesYes
Clicking the chosen star clears itYesYes
A read-only display filled to any valueYesYes

Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.

Without JavaScript

Every page starts as plain HTML: before IzzyBlazor's script loads on a static page, while an interactive page prerenders, and with JavaScript off. With JavaScript off, clicking the chosen star doesn't clear it; the arrow keys reach "No rating".

Accessibility

  • Screen readers hear a radio group named by its label, each star a radio named "1 star", "2 stars" and so on (IzzyText.RatingOneStar, RatingStars); Tab reaches the group and the arrow keys move the choice.
  • The focus ring is drawn around the focused star, or around them all on "No rating" (IzzyText.RatingNone).
  • A read-only rating is an image named "4.3 out of 5 stars" (IzzyText.RatingOutOf).
  • After a post that fails with an error here, the chosen star (or the first) takes focus, as in RadioGroup.

Parameters

Rating parameters
PropTypeDefault
MaxThe number of stars.int5
IconThe icon for each star.IconData?computed
AccentThe filled stars' color.Accent?computed
ReadOnlyShows the value as stars filled to the exact fraction (4.3 fills 86%), an image to screen readers, and posts it.boolfalse
@bind-ValueThe value, bound with @bind-Value.TValue
DisplayNameThe field's name in validation messages from the input itself, such as a value that can't be parsed.string?

See also

  • RadioGroup: lets the user pick one choice from a list of radio buttons.
  • Slider: lets the user pick a number by dragging along a range.
  • SegmentedControl: lets the user pick one of a few choices from joined buttons.

Connection lost

Rejoining the server…