radio.tsx


Overview

This file implements a customizable and accessible Radio Button component for React applications, including support for radio button groups. It provides two main components:

The components support controlled and uncontrolled modes, disabled state, and flexible layout directions (horizontal or vertical). The file also uses React Context to share state and behavior between the group and individual radios, and integrates iconography from the lucide-react library for checked indicators.


Components and Types

1. RadioGroupContext


2. Radio

Description

A single radio button component that renders a clickable label with a custom styled radio indicator, supporting both controlled and uncontrolled usage. It integrates with RadioGroupContext to synchronize selection state when used within a group.

Props

Prop

Type

Description

value

`string \

number`

checked

boolean (optional)

If provided, controls checked state externally (controlled).

disabled

boolean (optional)

Disables the radio button when true.

onChange

(checked: boolean) => void (optional)

Callback triggered when the checked state changes.

children

React.ReactNode (optional)

Label content rendered next to the radio button.

Behavior

Usage Example

<Radio value="option1" onChange={(checked) => console.log('Checked:', checked)}>
  Option 1
</Radio>

When used inside a group:

<Radio.Group value={selectedValue} onChange={setSelectedValue}>
  <Radio value="option1">Option 1</Radio>
  <Radio value="option2">Option 2</Radio>
</Radio.Group>

3. Radio.Group

Description

A container component that manages a group of radio buttons, controlling their selection state and disabling behavior. It uses React Context to provide group state and handlers to its child radios.

Props

Prop

Type

Description

value

`string \

number` (optional)

defaultValue

`string \

number` (optional)

onChange

`(value: string \

number) => void` (optional)

disabled

boolean (optional)

Disables all radios in the group when true.

children

React.ReactNode

The Radio components to render within the group.

className

string (optional)

Additional CSS classes for the container element.

direction

`'horizontal' \

'vertical'(optional, default'horizontal'`)

Behavior

Usage Example

<Radio.Group defaultValue="option1" onChange={(val) => console.log(val)} direction="vertical">
  <Radio value="option1">Option 1</Radio>
  <Radio value="option2">Option 2</Radio>
  <Radio value="option3" disabled>Option 3 (disabled)</Radio>
</Radio.Group>

Implementation Details

Controlled vs Uncontrolled

Context API

Styling and Accessibility

Commented Out Code


Interactions with Other Parts of the System


Component Structure Diagram

componentDiagram
  component Radio {
    +value: string | number
    +checked?: boolean
    +disabled?: boolean
    +onChange?: (checked: boolean) => void
    +children?: ReactNode
    +handleClick()
  }
  component RadioGroup {
    +value?: string | number
    +defaultValue?: string | number
    +onChange?: (value: string | number) => void
    +disabled?: boolean
    +children: ReactNode
    +className?: string
    +direction?: 'horizontal' | 'vertical'
  }
  component RadioGroupContext {
    +value: string | number
    +onChange: (value: string | number) => void
    +disabled?: boolean
  }
  RadioGroupContext <.. Radio : uses
  RadioGroup o-- RadioGroupContext : provides context
  RadioGroup "1" *-- "*" Radio : contains

Summary

The radio.tsx file provides a flexible, accessible, and composable radio button system for React, with support for controlled/uncontrolled usage and grouped selection management. It leverages React Context to coordinate shared state and behavior between the group and individual radios, ensuring consistent UX and easy integration in larger forms or UI modules. The components are styled with utility classes and include keyboard accessibility considerations.


Exported API

import { Radio } from 'radio.tsx';

// Usage:
<Radio value="1" checked onChange={...}>Option 1</Radio>

<Radio.Group value={selected} onChange={setSelected} direction="vertical" disabled={false}>
  <Radio value="1">Option 1</Radio>
  <Radio value="2">Option 2</Radio>
</Radio.Group>