Cette page n'est pas disponible dans la langue souhaitée. Langues disponibles: Deutsch English Radio-Button-Group. Übersicht

Aperçu.

Option one Option two Option three

Was macht die Komponente?

Die Radio-Button-Group fasst Radiobuttons (auch Radio-Button-Panels oder Selection-Expansion-Panels) zu einer Gruppe zusammen. Es kann nur ein Eintrag gleichzeitig ausgewählt sein. Sie legt Anordnung, Name und Zustände (z.B. Pflichtfeld, deaktiviert) für alle Einträge fest.

Wann soll die Komponente eingesetzt werden?

  • Immer, wenn Radiobuttons verwendet werden.
  • Für mehrere Auswahlen gleichzeitig ist stattdessen die Checkbox-Group zu verwenden.
Anwendung

Regeln.

  • Eine Radio-Button-Group fasst nur Radiobuttons zusammen, die zur selben Frage gehören. Die Gruppe braucht eine verständliche Beschriftung, auch für Screenreader (aria-label).
  • Die Radiobuttons stehen bei vielen oder langen Optionen untereinander (orientation="vertical"). Nebeneinander nur bei wenigen, kurzen Optionen. Mit horizontalFrom lässt sich festlegen, ab welcher Breite sie nebeneinander stehen.
  • Alle Radiobuttons einer Gruppe haben dieselbe Grösse.
  • Ist eine Auswahl erforderlich, wird bei fehlender Auswahl eine Fehlermeldung zur ganzen Gruppe angezeigt.

Spécification.

Beispiele Horizontal.
Vertical.
Vertical To Horizontal.
Horizontal Size S.
Vertical Size S.
Size XS.
Disabled.
Allow Empty Selection.
Compare With.
Error Message.
Error Message Vertical.
Horizontal Panels.
Vertical Panels.
Vertical To Horizontal Panels.
HTML in Zwischenablage kopiert.
Implementation

Web Components.

Le web component se trouve dans le paquet @sbb-esta/lyne-elements.

Storybook avec exemples interactifs Code source sur GitHub

Documentation.

The <sbb-radio-button-group> is a component which can be used as a wrapper for
a collection of either sbb-radio-buttons, sbb-radio-button-panels,
or sbb-selection-expansion-panels.
Individual radio-buttons inside a radio-group will inherit the name of the group.

<!-- The first option will be selected by default -->
<sbb-radio-button-group name="radio-group" value="Value one" aria-label="Radio group label">
  <sbb-radio-button value="Value one">Option one</sbb-radio-button>
  <sbb-radio-button value="Value two">Option two</sbb-radio-button>
  <sbb-radio-button value="Value three">Option three</sbb-radio-button>
</sbb-radio-button-group>

Pressing a <sbb-radio-button> checks it and unchecks the previously selected one, if any.
They can also be controlled programmatically by setting the value property of the parent radio group to the value of the radio.

Please note that within a <sbb-radio-button-group>, only one <sbb-radio-button> can be selected at a time;
if you need to select more than one item, it is recommended to use the <sbb-checkbox-group> component.

States.

The radio group can have different states:

  • can be completely disabled by setting the property disabled;
  • can be required by setting the property required.
<!-- All child radio buttons will be disabled -->
<sbb-radio-button-group value="Value one" aria-label="Radio group label" disabled>
  ...
</sbb-radio-button-group>

<!-- The radio group and all child radio buttons will be marked as required -->
<sbb-radio-button-group value="Value one" aria-label="Radio group label" required>
  ...
</sbb-radio-button-group>

In order to deselect a <sbb-radio-button> inside the <sbb-radio-button-group>,
you can use the allowEmptySelection property, which will be proxied to the inner <sbb-radio-button>
enabling their deselection (by default, a selected <sbb-radio-button> cannot be deselected).

<sbb-radio-button-group allow-empty-selection> ... </sbb-radio-button-group>
Style.

The orientation property is used to set item orientation. Possible values are horizontal (default) and vertical.
The optional property horizontalFrom can be used in combination with orientation='vertical' to
indicate the minimum breakpoint from which the orientation changes to horizontal.

<sbb-radio-button-group orientation="vertical" horizontal-from="large">
  ...
</sbb-radio-button-group>
Events.

Consumers can listen to the native change/input event on the <sbb-radio-button-group> component
to intercept the selection's change; the current value can be read from event.detail.value.

Accessibility.

In order to ensure readability for screen-readers, please provide an aria-label attribute for the <sbb-radio-button-group>.

Complex Values.

This component supports any types of values, including complex objects.
The type can be specified using the generic type parameter T of SbbRadioButtonGroup<T>.

const values = [
  { value: 'value1', name: 'Option 1' },
  { value: 'value2', name: 'Option 2' },
];
<sbb-radio-button-group .value="${values[0]}" name="name">
  <sbb-radio-button .value="${values[0]}">Option 1</sbb-radio-button>
  <sbb-radio-button .value="${values[1]}">Option 2</sbb-radio-button>
</sbb-radio-button-group>

When using complex objects, it is possible to provide a custom compareWith function to control how values are matched:

const complexValues = [
  { id: 1, name: 'Option 1' },
  { id: 2, name: 'Option 2' },
  { id: 3, name: 'Option 3' },
];
<sbb-radio-button-group
  .compareWith="${(v1, v2) => v1?.id === v2?.id}"
  .value="${complexValues[0]}"
>
  <sbb-radio-button .value="${complexValues[0]}">${complexValues[0].name}</sbb-radio-button>
  <sbb-radio-button .value="${complexValues[1]}">${complexValues[1].name}</sbb-radio-button>
  <sbb-radio-button .value="${complexValues[2]}">${complexValues[2].name}</sbb-radio-button>
</sbb-radio-button-group>

Angular.

Ce composant est également disponible pour Angular dans le paquet @sbb-esta/lyne-angular.

Aperçu Angular API Angular Exemples Angular
API

class: SbbRadioButtonGroupElement, sbb-radio-button-group.

Properties.

Name Attribute Privacy Type Default Description
allowEmptySelection allow-empty-selection public boolean false Whether the radios can be deselected.
compareWith - public (v1: T | null, v2: T | null) => boolean Function used to compare values.
disabled disabled public boolean false Whether the component is disabled.
horizontalFrom horizontal-from public 'zero' | 'small' | 'large' | 'ultra' | null null Overrides the behavior of orientation property.
name name public string `sbb-radio-button-group-${++nextId}` Name for the group. Will be propagated to the child radio buttons. Must be unique if multiple groups are used on the same page.
orientation orientation public 'horizontal' | 'vertical' 'horizontal' Indicates the orientation of the checkboxes or panels inside the group.
radioButtons - public (SbbRadioButtonElement<T> | SbbRadioButtonPanelElement<T>)[] List of contained radio buttons.
required required public boolean false Whether the component is required.
size size public 'xs' | 's' | 'm' | null null Size variant, either xs (lean theme default), s or m (standard theme default).
value value public (T = string) | null The value of the radio group.

Events.

Name Type Description Inherited From
didChange Event Deprecated. Mirrors change event for React. Will be removed once React properly supports change events.

Slots.

Name Description
Use the unnamed slot to add sbb-radio-button, sbb-radio-button-panel, sbb-selection-action-panel and sbb-selection-expansion-panel elements to the sbb-radio-button-group.
error Use this to provide a sbb-error to show an error message.