Vai al contenuto principale

Panoramica

Radio rappresenta un’opzione all’interno di RadioGroup. Assegna a ogni opzione un value distinto e un’etichetta visibile. Il gruppo possiede il valore selezionato.

Utilizzo

La prop size accetta sm o md e il valore predefinito è sm. Utilizza la stessa dimensione per le opzioni di un gruppo.

Scelte visive

Confronta gli stati selezionato e disabilitato, le dimensioni e gli aspetti predefinito e a scheda. Utilizza un gruppo di opzioni etichettato per una scelta effettiva.

Parti complete

Radio.Root e Radio.Indicator espongono le parti Base UI installate senza l’assemblaggio con stili. Componili quando hai bisogno di contenuti personalizzati o di un posizionamento personalizzato dell’indicatore. Il componente richiamabile Radio fornisce l’aspetto e l’indicatore radio di Twenty per le opzioni comuni.
I radio standard e Radio.Root eseguono il rendering di uno span con semantica radio e di un input nativo nascosto adiacente. I radio a scheda eseguono il rendering di un div. ref punta alla radice sottoposta a rendering; inputRef punta all’input. Radio.Indicator esegue il rendering di uno span e il relativo ref punta a quell’elemento. render, className e style basati sullo stato, gli attributi nativi e i gestori di eventi mantengono i rispettivi contratti upstream. I tipi pubblici includono RadioProps, RadioRootProps, RadioRootState, RadioIndicatorProps e RadioIndicatorState.

Opzioni della scheda

Usa variant="card" per opzioni a larghezza intera con un indicatore radio inserito. Per impostazione predefinita, i radio a scheda mantengono la propria radice div. Usa render per scegliere esplicitamente un altro elemento. Quando tale elemento è un pulsante nativo, imposta anche nativeButton. Mantieni i controlli interattivi al di fuori del contenuto della scheda. Assegna al gruppo una larghezza sufficiente per il relativo contenuto e lascia spazio per l’indicatore in alto a destra. Un titolo, un’icona e una breve descrizione aiutano a spiegare ciascuna scelta.

Composizione nativa esplicita

Il ref punta al pulsante composto. Per le radici personalizzate che non sono pulsanti, lascia nativeButton su false e inoltra le props di render fornite e il ref all’elemento effettivo.

Tastiera e selezione

Tab consente di entrare o uscire dal gruppo. I tasti freccia consentono di spostarsi tra le opzioni abilitate e di selezionare l’opzione attiva. Spazio seleziona il radio attivo. Il radio Base UI installato sopprime la selezione con Invio sia per le radici predefinite sia per i pulsanti nativi composti. I gestori di selezione e modifica appartengono a RadioGroup; consulta il relativo esempio non controllato e esempio controllato. Le frecce del radio modificano la selezione mentre spostano lo stato attivo. Dropdown.SelectionGroup sposta lo stato attivo con le frecce e conferma una scelta con Invio o Spazio. Radio e Radio.Root sono generici rispetto al tipo di valore. Mantieni tale tipo coerente con il relativo gruppo. Gli attributi nativi come aria-describedby possono associare un’opzione a testo di assistenza aggiuntivo.

Props

Il riferimento viene generato dai tipi di componenti pubblici. Gli attributi nativi, inclusi i nomi accessibili e i gestori di eventi, sono accettati anche nelle parti che eseguono il rendering degli elementi.
ReactNode
Option content. Card options wrap it in a padded full-width content area.
string | ((state: RadioRootState) => string | undefined)
CSS class applied to the element, or a function that returns a class based on the component’s state.
boolean
Whether the component should ignore user interaction.
Ref<HTMLInputElement>
A ref to access the hidden input element.
boolean
predefinito:"false"
Whether the component renders a native <button> element when replacing it via the render prop. Set to true if the rendered element is a native button.
boolean
Whether the user should be unable to select the radio button.
ReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, RadioRootState>
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.Accepts a ReactElement or a function that returns the element to render.
boolean
Whether the user must choose a value before submitting a form.
"md" | "sm"
predefinito:"sm"
Visual size of the radio.
CSSProperties | ((state: RadioRootState) => CSSProperties | undefined)
Style applied to the element, or a function that returns a style object based on the component’s state.
TValue
obbligatorio
The unique identifying value of the radio in a group.
"card" | "default"
predefinito:"default"
Appearance of the styled assembly: a standard radio or a full-width card. Standard radios use a span and card radios use a div unless render replaces the root.

Radio.Root

string | ((state: RadioRootState) => string | undefined)
CSS class applied to the element, or a function that returns a class based on the component’s state.
boolean
Whether the component should ignore user interaction.
Ref<HTMLInputElement>
A ref to access the hidden input element.
boolean
predefinito:"false"
Whether the component renders a native <button> element when replacing it via the render prop. Set to true if the rendered element is a native button.
boolean
Whether the user should be unable to select the radio button.
ReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, RadioRootState>
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.Accepts a ReactElement or a function that returns the element to render.
boolean
Whether the user must choose a value before submitting a form.
CSSProperties | ((state: RadioRootState) => CSSProperties | undefined)
Style applied to the element, or a function that returns a style object based on the component’s state.
Value
obbligatorio
The unique identifying value of the radio in a group.

Radio.Indicator

string | ((state: RadioIndicatorState) => string | undefined)
CSS class applied to the element, or a function that returns a class based on the component’s state.
boolean
predefinito:"false"
Whether to keep the HTML element in the DOM when the radio button is inactive.
ReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, RadioIndicatorState>
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.Accepts a ReactElement or a function that returns the element to render.
CSSProperties | ((state: RadioIndicatorState) => CSSProperties | undefined)
Style applied to the element, or a function that returns a style object based on the component’s state.