Socket
Socket
Sign inDemoInstall

@react-aria/radio

Package Overview
Dependencies
Maintainers
2
Versions
724
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@react-aria/radio

Spectrum UI components in React


Version published
Maintainers
2
Created

Package description

What is @react-aria/radio?

@react-aria/radio is a library that provides accessible radio button components for React applications. It is part of the React Aria collection, which focuses on providing high-quality, accessible UI components.

What are @react-aria/radio's main functionalities?

Basic Radio Group

This code demonstrates how to create a basic radio group using @react-aria/radio. It includes a RadioGroup component that manages the state and a Radio component that renders individual radio buttons.

import { useRadioGroupState } from '@react-stately/radio';
import { useRadioGroup, useRadio } from '@react-aria/radio';
import { useRef } from 'react';

function RadioGroup(props) {
  let state = useRadioGroupState(props);
  let { radioGroupProps } = useRadioGroup(props, state);

  return (
    <div {...radioGroupProps}>
      {props.children}
    </div>
  );
}

function Radio(props) {
  let ref = useRef();
  let { inputProps } = useRadio(props, props.state, ref);

  return (
    <label>
      <input {...inputProps} ref={ref} />
      {props.children}
    </label>
  );
}

export default function App() {
  return (
    <RadioGroup label="Favorite Pet">
      <Radio value="dogs">Dogs</Radio>
      <Radio value="cats">Cats</Radio>
    </RadioGroup>
  );
}

Custom Styling

This code demonstrates how to apply custom styling to the radio group and radio buttons using CSS classes. The styles are defined in an external stylesheet (styles.css).

import { useRadioGroupState } from '@react-stately/radio';
import { useRadioGroup, useRadio } from '@react-aria/radio';
import { useRef } from 'react';
import './styles.css';

function RadioGroup(props) {
  let state = useRadioGroupState(props);
  let { radioGroupProps } = useRadioGroup(props, state);

  return (
    <div {...radioGroupProps} className="radio-group">
      {props.children}
    </div>
  );
}

function Radio(props) {
  let ref = useRef();
  let { inputProps } = useRadio(props, props.state, ref);

  return (
    <label className="radio-label">
      <input {...inputProps} ref={ref} className="radio-input" />
      {props.children}
    </label>
  );
}

export default function App() {
  return (
    <RadioGroup label="Favorite Pet">
      <Radio value="dogs">Dogs</Radio>
      <Radio value="cats">Cats</Radio>
    </RadioGroup>
  );
}

Disabled Radio Buttons

This code demonstrates how to create a radio group with disabled radio buttons. The 'isDisabled' prop is used to disable specific radio buttons.

import { useRadioGroupState } from '@react-stately/radio';
import { useRadioGroup, useRadio } from '@react-aria/radio';
import { useRef } from 'react';

function RadioGroup(props) {
  let state = useRadioGroupState(props);
  let { radioGroupProps } = useRadioGroup(props, state);

  return (
    <div {...radioGroupProps}>
      {props.children}
    </div>
  );
}

function Radio(props) {
  let ref = useRef();
  let { inputProps } = useRadio(props, props.state, ref);

  return (
    <label>
      <input {...inputProps} ref={ref} disabled={props.isDisabled} />
      {props.children}
    </label>
  );
}

export default function App() {
  return (
    <RadioGroup label="Favorite Pet">
      <Radio value="dogs">Dogs</Radio>
      <Radio value="cats" isDisabled={true}>Cats</Radio>
    </RadioGroup>
  );
}

Other packages similar to @react-aria/radio

Readme

Source

@react-aria/radio

This package is part of react-spectrum. See the repo for more details.

FAQs

Package last updated on 20 Dec 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc