Ant Design Svelte

Radio

Single selection from several visible options.

When to use

Radio selects one option from a small set: tariff, publication mode or delivery method. All options are visible at the same time, so it is convenient to compare them. For a long list with search use Select, for independent marks - Checkbox.

Name the options so that the choice is clear without additional action. Clicking the selected Radio again does not uncheck it. If the response “not selected” is acceptable, provide it in the options or a separate reset command.

Radio CSS Prefixes show global ConfigProvider.prefixCls and local prefixCls without resetting state. RadioButton gets the base prefix without -button. Group passes it options; arbitrary children choose their prefixCls independently, so here it is also set for the buttons.

How to select a group

It is easier to describe a regular group using options. Child Radios or RadioButtons come in handy when an option needs its own markup.

Problem Setting Example
Select one value RadioGroup, bind:value, options Group Radio
Show additional field for option Subsidiary Radio and snippet Vertical groups
Design options with buttons optionType="button" or RadioButton Button design
Occupy available width block Full width band

Non-empty options takes precedence over children. The explicit optionType of the group determines the appearance of the child RadioButtons. buttonStyle selects the outline or fill, and size changes the size of the button design. A regular round Radio does not increase with the size of the group.

Meaning, events and form of

For RadioGroup, bind:value binds the selected value. string, number and boolean are supported: 0, '0' and false - different options. Don't check for choice through truthiness. Typed Example shows the original type, and form stores the numeric rate and publication boolean.

defaultValue sets the initial selection of the group, defaultChecked — a single Radio. For subsequent changes, use managed props or binding. Outside the group, bind:checked links the mark of one Radio; within a group, the state is determined by the common value.

Group onChange receives event with target.value and nativeEvent. Then onValueChange gets the selected value itself. For the normal state, bind:value; callbacks are needed, for example, to process selection by the application.

In Form, pass value from the model and update it via onValueChange. FormItem binds the signature and errors, but does not substitute the value into the child snippet. Native FormData sends the string value of the selected available input with name; the typed Form model retains the original type. These submission methods are shown separately.

Keyboard and design

The common name combines native input into a group. If not passed, RadioGroup will create its own name. Specify name explicitly if you are reading data via FormData. Example of a native form also shows reset and software focus.

Tab moves focus in and out of the group; arrows change the selection, skipping unavailable options. Space selects the option in focus. Give the group an accessible name using aria-label or aria-labelledby. Do not replace the native input with a decorative button when designing options.

orientation specifies horizontal or vertical orientation and takes precedence over vertical. block stretches the options across the width of the container. disabled of the entire group also blocks those options that have disabled=false specified locally.

Semantic DOM of a separate Radio provides root, icon and label: classNames specifies classes, styles – CSS strings. Semantic slots RadioGroup, introduced in Ant Design 6.7, are not yet available in the public Svelte API. The usual class and style are available for the container; don't pass it group classNames/styles from the React examples.

Radio uses design tokens from the current theme for its appearance.

Examples

checked: false

Single Radio

bind:checked binds the state. Pressing again does not clear the selection; the button resets it from the outside.

Unavailable options

Selected and unselected Radios retain their state when disabled. The button changes accessibility.

value: 1; onChange: —

Group Radio

The group chooses one number. bind:value updates the model, onChange receives an event with target.value.

Option 1

Vertical groups

orientation places regular and push-button options vertically. Svelte snippet adds a field for its variant.

Full width group

block stretches a regular group, outline and filled buttons across the width of the container.

["Яблоко","Яблоко","Яблоко","Яблоко"]

Groups from options

Simple values ​​and RadioOption objects, unavailable options, optionType and buttonStyle.

Hangzhou

Button design

RadioButton inside RadioGroup: regular options, separate inaccessible button and inaccessible group.

Form has not been submitted yet

Group name and native form

name combines native keyboard radio and FormData. The example also shows reset and focus().

Button sizes

large, medium and small change the size of the button design. Narrow containers allow horizontal scrolling.

Solid fill

buttonStyle="solid" highlights the selected button with a fill. The unavailable state is shown separately.

Semantic styles

The styles object and classNames function from { props } style root, icon and label.

number: 0

Typed values

The model retains the original string, number or boolean type, including 0 and false.

Location options

Selecting and checking in Form

Tariff and publication are associated with Form. Validate, submit, and reset store the values ​​0, false, and undefined.

draft
General: suite; local: project-radio

RadioButton gets the base prefix without -button. Group passes it options; arbitrary children choose their prefixCls independently, so here it is also set for the buttons.

CSS prefixes Radio

Common namespace ConfigProvider and local prefixCls; the state of the component is preserved when the name is changed.

API

The main parameters are checked against the public types RadioProps, RadioGroupProps and RadioOption of the package @woodger/antd. Radio and RadioButton use the same state and event contract.

Basic parameters of Radio and RadioButton
PropertyDescriptionTypeDefault
prefixClsBasic CSS prefix without the -group/-button suffix. The empty string and undefined inherit ConfigProvider. The group applies to options; children choose it independently.stringConfigProvider.prefixCls + "-radio"
checkedSelected state; supports bind:checked outside of RadioGroup.booleanfalse
defaultCheckedInitial state without binding. Native reset restores it without callbacks.booleanfalse
valueVariant value for group or FormData; does not replace checked for a single Radio.RadioValueon
disabledLocks selection. Inherited from Group, Form or ConfigProvider.booleanFrom context
onCheckedChangeGets a boolean when user selects.(checked: boolean) => void—
onChangeGets target.checked, typed target.value and nativeEvent. Native onchange receives a DOM event.(event: RadioChangeEvent) => void—
autoFocusFocuses input when mount; native autofocus is also preserved.booleanfalse
classNamesClasses root, icon and label; object or function from { props }.RadioSemantic—
stylesCSS lines of the same semantic slots; object or function from { props }.RadioSemantic—
inputElementbind:inputElement returns the native input.HTMLInputElement—
nativeElementbind:nativeElement returns the inner span of the icon.HTMLSpanElement—
refbind:ref returns input, nativeElement, focus() and blur(). Links are cleared when unmounted.RadioRef—
Basic parameters of RadioGroup
PropertyDescriptionTypeDefault
prefixClsBasic CSS prefix without the -group/-button suffix. The empty string and undefined inherit ConfigProvider. The group applies to options; children choose it independently.stringConfigProvider.prefixCls + "-radio"
valueSelected value; supports bind:value. value — string, number or boolean.Valueundefined
defaultValueInitial local value; native reset restores it.Valueundefined
optionsSimple values ​​or objects with value, label, disabled, id, title, className, style, required and onChange.readonly (Value | RadioOption<Value>)[][]
childrenChild Radio or RadioButton. Non-empty options takes precedence.Snippet—
nameCommon name for native input. Involved in keyboard selection and FormData.stringGenerated name
orientationLocation options. Takes precedence over vertical.horizontal | verticalhorizontal
verticalCompatible way to enable vertical layout.booleanfalse
blockStretches the group and distributes the options evenly.booleanfalse
optionTypeSets the appearance of options and overrides the type of child RadioButtons.default | buttondefault
sizeButton design size; regular Radio retains its icon size.ComponentSizeFrom ConfigProvider
buttonStyleOutline or filled design for the selected button.outline | solidoutline
disabledNative fieldset blocks the entire group. disabled=false for the option does not remove this lock.booleanFrom context
onChangeReceives event with target.value; called when the selected value changes.(event: RadioChangeEvent<Value>) => void—
onValueChangeGets the selected value itself after onChange.(value: Value) => void—
nativeElementbind:nativeElement returns the container of the group.HTMLFieldSetElement—

ConfigProvider.radio

classstyleclassNamesstyles

The appearance is applied to the Radio, RadioButton and options options. Button accessibility and size are inherited from componentDisabled and componentSize.

onchange, onfocus, onblur and onkeydown preserve native Svelte events. Compatible names with a capital letter are also supported.

Methods

Via bind:this and bind:ref available focus(options?: FocusOptions) and blur(). bind:inputElement returns input, bind:nativeElement - internal span. For RadioGroup, nativeElement points to fieldset.

Named group shows soft focus, send and native reset.

Semantic DOM

Radio

Select an element to highlight. Names are used in classNames and styles; styles accepts CSS strings.

root: External label with text and icon.

RadioGroup

The semantic classNames and styles groups added in Ant Design 6.7.0 are not yet supported by the package. Available for container class and style, for options - ConfigProvider.radio and options properties.

Registration tokens

Component tokens are specified in ConfigProvider.theme.components.Radio. Separate radioColor and radioBgColor - package compatibility parameters, the rest correspond to the main Radio tokens on ant.design.

<script lang="ts">
  import { ConfigProvider, Radio, RadioGroup } from '@woodger/antd';
</script>

<style>
  .example {
    display: grid;
    justify-items: start;
    gap: 20px;
  }
</style>

<ConfigProvider theme={{
  cssVar: {},
  token: { colorPrimary: '#389e0d' },
  components: { Radio: { radioSize: 20, dotSize: 10, buttonPaddingInline: 20 } }
}}>
  <div class="example">
    <Radio defaultChecked>{("Own theme Radio")}</Radio>
    <RadioGroup aria-label={("Custom RadioButton theme")} options={([
      "Day",
      "Week",
      "Month"
    ])}
      optionType="button" buttonStyle="solid" defaultValue="Неделя" />
  </div>
</ConfigProvider>
Component Radio Tokens
TokenPurpose
buttonBgButton background.
buttonCheckedBgBackground of the selected outline button.
buttonCheckedBgDisabledBackground of the selected inaccessible button.
buttonCheckedColorDisabledText of the selected inaccessible button.
buttonColorButton text color.
buttonPaddingInlineHorizontal padding of the button.
buttonSolidCheckedActiveBgBackground of the pressed selected button with fill.
buttonSolidCheckedBgBackground of the selected button with fill.
buttonSolidCheckedColorText of the selected button with fill.
buttonSolidCheckedHoverBgBackground of the selected button with fill on hover.
dotColorDisabledColor of the mark for unavailable Radio.
dotSizeMark size.
radioSizeRadio icon size.
wrapperMarginInlineEndIndentation between options.
radioColorMark color taking into account wireframe; package compatibility parameter.
radioBgColorIcon background taking into account wireframe; package compatibility parameter.

Common tokens of the current topic

Values are read through theme.useToken() and react to the settings in the header.

Common tokens of the current topic
TokenPurposeCurrent value
colorBgContainerContainer background.#ffffff
colorBgContainerDisabledBackground of an inaccessible element.rgba(0,0,0,0.04)
colorBorderFrame color.#d9d9d9
colorPrimaryPrimary color.#2f54eb
colorPrimaryActivePrimary color when pressed.#1d39c4
colorPrimaryBorderLight outline of the main color.#adc6ff
colorPrimaryHoverPrimary hover color.#597ef7
colorTextText color.rgba(0,0,0,0.88)
colorTextDisabledColor of inaccessible text.rgba(0,0,0,0.25)
borderRadiusNormal rounding.6
borderRadiusLGLarge rounding.8
borderRadiusSMSmall rounding.4
controlHeightNormal element height.32
controlHeightLGHeight of a large element.40
controlHeightSMHeight of the small element.24
controlOutlineFocus outline color.rgba(5,88,255,0.06)
controlOutlineWidthThickness of the element outline.2
fontFamilyFont family.-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, 'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'
fontSizeNormal text size.14
fontSizeLGLarge text size.16
lineHeightRow height.1.5714285714285714
lineTypeFrame line type.solid
lineWidthFrame thickness.1
lineWidthFocusFocus stroke thickness.3
marginXSSmall external indent.8
motionDurationMidDuration of main transitions.0.2s
motionDurationSlowDuration of slow transitions.0.3s
motionEaseInOutCircTransition curve.cubic-bezier(0.78, 0.14, 0.15, 0.86)
paddingXSSmall internal indent.8

FAQ

Why doesn't pressing it again clear the selection?

Radio saves the selected option. To reset, change the model outside or add a separate answer. If you need to turn the mark on and off independently, use Checkbox.

What is the difference between onChange and onValueChange?

onChange receives a RadioChangeEvent with target.value and nativeEvent. onValueChange gets the selected value itself after onChange. For two-way state communication in Svelte, use bind:value.

How to save 0 and false in a form?

Leave the original types in options and the Form model. Don't use boolean conversion to check for selection. Example form retains both values; native FormData contains their string representation.

Why do you need a name?

It combines native input for keyboard and form submission. Without name, RadioGroup creates its own name. For a predictable key in FormData, set it explicitly, as in native form.

Why are children not shown along with options?

Non-empty options shows own options instead of children. Use options for the standard list, and the child Radio or RadioButton for your own markup.

Is it possible to design the Semantic DOM of the entire RadioGroup?

The new group classNames/styles from Ant Design 6.7 have not yet been carried over. A separate Radio has root, icon and label available; For the group container, use class and style. Example of semantic styles shows available settings.