Synertech Web/ui v0.57.5 Site Storybook

Documentation / Forms

RadioButton

Client@synertech/uiOpen in Storybook

The radio button: a native <input type="radio"> under a styled circle, with a label, a description and an error message. There is no group component: the buttons of one choice share a name, which gives the browser the group and arrow-key navigation.

Two ways to hold a group. Controlled: checked computed from the value, onCheckedChange to update it. Uncontrolled: defaultChecked on the initial choice and nothing else — the state stays in the native radio, so the browser holds the group, a form reset included. onCheckedChange only tells the button that was just checked: a radio unchecked by another one receives no event.

Unlike Checkbox, the colours (checkedColor, dotColor, labelColor…) are CSS values ("#059669"), not Tailwind classes. A client component ("use client").

Examples

Controlled group

A shared name and a value in state: each button is checked when its value matches.

Open in a tab
import { useState } from "react";
import { RadioButton } from "@synertech/ui";

const deliveryOptions = [
  { value: "standard", label: "Standard", description: "3 à 5 jours ouvrés — gratuit" },
  { value: "express", label: "Express", description: "Sous 24 h — 9,90 €" },
  { value: "relay", label: "Point relais", description: "2 à 4 jours — 3,50 €" },
];

export function Basic() {
  const [delivery, setDelivery] = useState("standard");

  return (
    <fieldset className="flex flex-col gap-3">
      <legend className="mb-2 text-sm font-medium text-gray-900 dark:text-white">
        Mode de livraison
      </legend>
      {deliveryOptions.map((option) => (
        <RadioButton
          key={option.value}
          name="delivery"
          value={option.value}
          label={option.label}
          description={option.description}
          checked={delivery === option.value}
          onCheckedChange={(checked) => checked && setDelivery(option.value)}
        />
      ))}
    </fieldset>
  );
}

Sizes and variants

size sets the diameter, variant the style: thin outline, solid fill or thick outline.

Open in a tab
import { useState } from "react";
import { RadioButton } from "@synertech/ui";

export function Variants() {
  const [variant, setVariant] = useState("default");
  const variants = [
    { value: "default", size: "sm" },
    { value: "filled", size: "md" },
    { value: "outlined", size: "lg" },
  ] as const;

  return (
    <div className="flex flex-col gap-3">
      {variants.map((v) => (
        <RadioButton
          key={v.value}
          name="variant-demo"
          value={v.value}
          variant={v.value}
          size={v.size}
          label={`variant="${v.value}", size="${v.size}"`}
          checked={variant === v.value}
          onCheckedChange={(checked) => checked && setVariant(v.value)}
        />
      ))}
    </div>
  );
}

Custom colours and label on the left

The colours are CSS values; labelPosition="left" puts the label before the circle.

Open in a tab
import { useState } from "react";
import { RadioButton } from "@synertech/ui";

export function CustomColors() {
  const [theme, setTheme] = useState("light");

  return (
    <div className="flex w-64 flex-col gap-3">
      {[
        { value: "light", label: "Thème clair" },
        { value: "dark", label: "Thème sombre" },
      ].map((option) => (
        <RadioButton
          key={option.value}
          name="theme"
          value={option.value}
          label={option.label}
          labelPosition="left"
          checkedColor="#059669"
          dotColor="#ffffff"
          variant="filled"
          checked={theme === option.value}
          onCheckedChange={(checked) => checked && setTheme(option.value)}
        />
      ))}
    </div>
  );
}

Validation and states

A string passed to error is shown below the button and colours the circle; disabled greys it out.

Open in a tab
import { useState } from "react";
import { RadioButton } from "@synertech/ui";

export function States() {
  const [answer, setAnswer] = useState("");

  return (
    <div className="flex flex-col gap-3">
      <RadioButton
        name="contract"
        value="cdi"
        label="CDI"
        required
        checked={answer === "cdi"}
        onCheckedChange={(checked) => checked && setAnswer("cdi")}
        error={answer ? false : "Choisissez un type de contrat."}
      />
      <RadioButton
        name="contract"
        value="cdd"
        label="CDD"
        checked={answer === "cdd"}
        onCheckedChange={(checked) => checked && setAnswer("cdd")}
        error={!answer}
      />
      <RadioButton name="contract" value="interim" label="Intérim (bientôt disponible)" disabled />
    </div>
  );
}

In a form, without state

defaultChecked marks the initial choice: the browser holds the group, the checked value is submitted with the form and "Reset" goes back to it.

Open in a tab
import { Button, RadioButton } from "@synertech/ui";

export function InForm() {
  return (
    <form
      className="flex flex-col gap-3"
      onSubmit={(event) => {
        event.preventDefault();
        console.log(new FormData(event.currentTarget).get("billing"));
      }}
    >
      <RadioButton name="billing" value="monthly" label="Mensuel — 12 € / mois" defaultChecked />
      <RadioButton name="billing" value="yearly" label="Annuel — 120 € / an (2 mois offerts)" />
      <div className="flex gap-2">
        <Button type="submit">Continuer</Button>
        <Button type="reset" bgColor="bg-gray-100 hover:bg-gray-200" textColor="text-gray-800">
          Réinitialiser
        </Button>
      </div>
    </form>
  );
}

API reference

Components

RadioButton

ComponentClient
import { RadioButton } from "@synertech/ui";

Props — RadioButtonProps

PropTypeDefaultDescription
value• string —
borderColor string —
checked boolean —
checkedColor string —
containerClassName string —
defaultChecked boolean false
description string —

Written under the label, and describes the button.

disabled boolean false
dotColor string —
error boolean | string —

true marks the button invalid; a message also does, and is written under it. The button is then aria-invalid and described by the message. false and "" are no error.

errorColor string —

Colour of the message, and of the circle's border while in error. Any CSS colour.

focusRingColor string —

Colour of the focus ring. Any CSS colour.

hoverColor string —

Border colour of the circle under the pointer. Any CSS colour.

label string —
labelColor string —
labelPosition "left" | "right" "right"
onCheckedChange (checked: boolean) => void —
readOnly boolean false
required boolean false
showFocusRing boolean true
size "sm" | "md" | "lg" "md"
uncheckedColor string —
variant "default" | "filled" | "outlined" "default"

+ 301 inherited native attributes from HTMLAttributes, AriaAttributes, DOMAttributes (className, id, aria-*, event handlers…).