05 · Forms — dates and values

Rating

variant solid · outline × size sm/md/lg × value (whole and fractional) · show_value · count · interactive · summary by score

A star rating: read-only (accepts half a point) or interactive, with radios underneath.

Pointer over

.heex
<div class="flex flex-wrap items-center gap-2.5"><.rating id="rt-loop-lg" size="lg" loop /><.rating id="rt-loop" loop /></div>

variant × size

smmdlgsolid
outline
.heex
<div class="grid grid-cols-[90px_repeat(3,_minmax(0,1fr))] gap-y-[10px] gap-x-[12px] items-center"><span></span><span class="text-xs text-muted font-mono text-center">sm</span><span class="text-xs text-muted font-mono text-center">md</span><span class="text-xs text-muted font-mono text-center">lg</span><span class="text-xs text-muted font-mono">solid</span><div class="flex justify-center min-w-0"><.rating id="rt-solid-sm" variant="solid" size="sm" value={4} /></div><div class="flex justify-center min-w-0"><.rating id="rt-solid-md" variant="solid" size="md" value={4} /></div><div class="flex justify-center min-w-0"><.rating id="rt-solid-lg" variant="solid" size="lg" value={4} /></div><span class="text-xs text-muted font-mono">outline</span><div class="flex justify-center min-w-0"><.rating id="rt-outline-sm" variant="outline" size="sm" value={4} /></div><div class="flex justify-center min-w-0"><.rating id="rt-outline-md" variant="outline" size="md" value={4} /></div><div class="flex justify-center min-w-0"><.rating id="rt-outline-lg" variant="outline" size="lg" value={4} /></div></div>

value · show_value · count

5,0 (12) 4,5 (383) 3,0 (7) 1,5 (2)
.heex
<div class="flex flex-col gap-[10px]"><.rating id="rt-a" value={5} show_value count={12} /><.rating id="rt-b" value={4.5} show_value count={383} /><.rating id="rt-c" value={3} show_value count={7} /><.rating id="rt-d" value={1.5} show_value count={2} /></div>

summary by score

4,6 383 ratings
5 ★
276 4 ★
69 3 ★
23 2 ★
8 1 ★
7
.heex
<.rating_distribution counts={[276, 69, 23, 8, 7]} />

Try it: choose a rating (name, no server)

Como foi o atendimento?
E a entrega?
.heex
<div class="flex flex-col gap-3">
  <.rating id="rt-live" name="atendimento" label="Como foi o atendimento?" value={3} />
  <.rating id="rt-live-lg" name="entrega" label="E a entrega?" size="lg" variant="outline" />
</div>

Usage

<.rating value={4.5} show_value count={38} />
<.rating field={@form[:grade]} variant="outline" size="lg" />
<.rating_breakdown counts={[276, 69, 23, 8, 7]} />

Rules

  • Interactive: the stars light up to where the pointer is; clicking fixes it.
  • solid amber over gray; outline amber lines with the empty ones dimmed.

API reference

Todos os function components, atributos, valores e slots que este componente cobre.

A star rating: read-only, with a fractional value and the count of reviews, or interactive, as a group of native radios; and rating_distribution/1, the summary with the average and a bar per grade.

<.rating value={4.5} />
<.rating value={4.2} show_value count={38} />
<.rating id="service" name="service" label="Como foi o atendimento?" value={@grade} on_change="rate" />
<.rating field={@form[:grade]} label="Nota" />
<.rating_distribution average={4.5} counts={[800, 310, 110, 40, 24]} />

A fractional value is drawn in half stars (the nearest half). The read-only rating is one role="img" with the text 4,5 de 5 estrelas (aria_label gives another).

Interactive ratings are a radiogroup of max native radios (Tab enters the group, the arrow keys choose, the form submits name). on_change is an event name (pushed with %{"id", "value"}, to target when given) or a %JS{}. The LedgerUI.Rating hook (priv/static/ledger_ui/hooks/rating.js) previews the grade under the pointer.

<.rating>

Renders a rating.

Attr Type Default Values / notes
id :string nil
name :string nil
value :any
field {:struct, Phoenix.HTML.FormField} nil
max :integer 5
variant :string "solid" solid, outline
size :string "md" sm, md, lg
count :integer nil
show_value :boolean false
decimal :string ","
interactive :boolean nil
label :string nil
aria_label :string nil
help :string nil
errors :list []
disabled :boolean false
loop :boolean false Animated demo: every star lights up in turn (read-only, no on/off state).
on_change :any nil
target :any nil
class :any nil

Any other HTML/phx attribute goes to the root element.

<.rating_distribution>

The summary of a rating: the average, its stars, the total and a bar per grade.

Attr Type Default Values / notes
counts (required) :any A list, best grade first, or a map %{grade => count}.
average :any nil
total :integer nil
unit :string "avaliações"
decimal :string ","
class :any nil

Any other HTML/phx attribute goes to the root element.