Skip to content
Forms, switch product

Rating

Rating widgets are Input Fields that score something in stars, such as a review. Their value is the number of stars picked, a whole number from 1 to the number of stars, which is 5 by default.

rating.ts
import { gui } from '@golemui/gui-shared';
export default [
gui.inputs.rating('service', {
label: 'Service',
}),
];
rating.json
{
"$schema": "https://golemui.com/schemas/form.schema.json",
"form": [
{
"kind": "input",
"type": "rating",
"path": "service",
"label": "Service"
}
]
}

The stars are native radio buttons: the arrow keys move between them and pick one, and screen readers read “1 star” to “5 stars”. The pointer previews a rating as it moves over the stars.

Until the user picks a star, or the form sets a defaultValue, the rating has no value. A click on the picked star clears it, and so do Delete and Backspace. For a score on a longer or finer scale, use a Slider.

Use these props to customize your Rating widget.

proptypedescription
hintstringDescription text displayed below the label

Use the property hint to provide instructions for the user.

rating.ts
import { gui } from '@golemui/gui-shared';
export default [
gui.inputs.rating('food', {
hint: 'Click the picked star again, or press Delete, to clear it.',
label: 'Food',
}),
];
rating.json
{
"$schema": "https://golemui.com/schemas/form.schema.json",
"form": [
{
"kind": "input",
"type": "rating",
"path": "food",
"label": "Food",
"props": {
"hint": "Click the picked star again, or press Delete, to clear it."
}
}
]
}

A rating has no max prop: the maximum of its integer validator is the number of stars. A defaultValue picks a rating from the start.

rating.ts
import { gui } from '@golemui/gui-shared';
export default [
gui.inputs.rating('stay', {
label: 'Your stay',
validator: {
maximum: 10,
},
defaultValue: 7,
}),
];
rating.json
{
"$schema": "https://golemui.com/schemas/form.schema.json",
"form": [
{
"kind": "input",
"type": "rating",
"path": "stay",
"label": "Your stay",
"defaultValue": 7,
"validator": {
"type": "integer",
"maximum": 10
}
}
]
}

A Rating takes an integer validator. With required, the user can still clear the rating: a cleared required rating shows its error, as an emptied text field does.

A rating with no value fails the integer type check before required, so word the missing value with messages.invalid.

rating.ts
import { gui } from '@golemui/gui-shared';
export default [
gui.inputs.rating('overall', {
label: 'Overall',
validator: {
required: true,
messages: {
invalid: 'Rate your experience',
},
},
}),
];
rating.json
{
"$schema": "https://golemui.com/schemas/form.schema.json",
"form": [
{
"kind": "input",
"type": "rating",
"path": "overall",
"label": "Overall",
"validator": {
"type": "integer",
"required": true,
"messages": {
"invalid": "Rate your experience"
}
}
}
]
}

Ratings can be styled as explained in the Styling Guide.

Following you will find a list with the CSS Variables and a quick description of what you will style.

CSS VariableDescription
--gui-rating-sizeWidth and height of each star. Defaults to 1.5rem
--gui-rating-gapSpace between two stars. Defaults to 0.125rem
--gui-rating-fillFill and outline of a picked star. A warning tone that reaches 3:1 contrast
--gui-border-strongOutline of an empty star
--gui-border-focusFocus ring around the focused star
--gui-intent-errorOutline of the empty stars while the rating shows an error

This is the anatomy of the Rating Widget in case you want to use your CSS styles.

rating-anatomy.html
<gui-rating-input class="gui-rating gui-field">
<gui-rating class="gui-field">
<span class="gui-label" id="fieldUid_label">
<span class="gui-label__text">Label</span>
<div class="gui-widget-hint" id="fieldUid_hint" aria-hidden="true">Hint</div>
</span>
<div class="gui-widget">
<div
role="radiogroup"
class="gui-rating__stars"
id="fieldUid"
aria-labelledby="fieldUid_label"
aria-describedby="fieldUid_hint"
>
<label class="gui-rating__star gui-rating__star--filled" for="fieldUid_1">
<input
type="radio"
class="gui-rating__input gui-visually-hidden"
id="fieldUid_1"
name="fieldUid"
form="fieldUid_none"
value="1"
tabindex="-1"
aria-label="1 star"
aria-describedby="fieldUid_hint"
/>
<svg viewBox="0 0 24 24" class="gui-rating__glyph" aria-hidden="true">
<polygon points="12,2 14.49,9.17 …"></polygon>
</svg>
</label>
<label class="gui-rating__star gui-rating__star--filled" for="fieldUid_2">
<input
type="radio"
class="gui-rating__input gui-visually-hidden"
id="fieldUid_2"
name="fieldUid"
form="fieldUid_none"
value="2"
tabindex="0"
aria-label="2 stars"
aria-describedby="fieldUid_hint"
/>
<svg viewBox="0 0 24 24" class="gui-rating__glyph" aria-hidden="true">
<polygon points="12,2 14.49,9.17 …"></polygon>
</svg>
</label>
<!-- More stars, up to the validator's maximum (5 by default) -->
</div>
</div>
<div class="gui-validator gui-validator--empty" id="fieldUid_errors" role="alert"></div>
</gui-rating>
</gui-rating-input>