Fractional ratings with native radio values
Choose half-star scores with the pointer or keyboard, preserve read-only values and reset through an external native form.
Keyboard
- Arrow keys change by half a star; Home and End reach the bounds.
- RTL reverses horizontal arrow direction. Clear rating restores an unrated value.
States covered
- half stars
- RTL
- native form ownership
- required
- prevented reset
- read only and disabled
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-rating-fractional-form.jsonComplete source
/*!
MIT License
Copyright (c) 2026 Jaison
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import {useId,useState} from 'react';
import {Button,Checkbox,Rating,UIProvider} from '@bld8/ui';
export default function RatingForm(){const id=useId(),[rtl,setRtl]=useState(false),[prevent,setPrevent]=useState(false),[result,setResult]=useState('No rating submitted.');return <UIProvider direction={rtl?'rtl':'ltr'}><div style={{display:'grid',gap:24}}><Button variant="secondary" onClick={()=>setRtl(value=>!value)}>{rtl?'Use LTR rating':'Use RTL rating'}</Button>
<Rating label="Review score" name="score" form={id} defaultValue={3.5} step={.5} required hint="Use arrow keys for half-star steps."/>
<Rating label="Published score" name="published" form={id} value={4.5} step={.5} readOnly/>
<Rating label="Disabled score" name="disabled" form={id} defaultValue={2} disabled/>
<Rating label="Ten point summary" max={10} step={.5} value={8.5} readOnly/>
<Checkbox label="Prevent rating reset" checked={prevent} onCheckedChange={setPrevent}/>
<form id={id} aria-label="Review form" onReset={event=>{if(prevent)event.preventDefault();}} onSubmit={event=>{event.preventDefault();const data=new FormData(event.currentTarget);setResult('Score: '+data.get('score')+' | Published: '+data.get('published')+' | Disabled: '+data.has('disabled'));}}><div style={{display:'flex',gap:12,flexWrap:'wrap'}}><Button type="submit">Read ratings</Button><Button type="reset" variant="secondary">Reset ratings</Button></div></form><p role="status">{result}</p></div></UIProvider>;}