
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
react-radio-input
Advanced tools
This package is a re-take on react-radio-group by Cheng Lou, but written using TypeScript and the ”new” Context API introduced in React 16.3.0.
$ npm i react-radio-input
// or…
$ yarn add react-radio-input
import { useState } from 'react';
import { RadioGroup, Radio } from 'react-radio-input';
const ExampleComponent = () => {
const initialValue = 'apple';
const [selectedFruit, setSelectedFruit] = useState(initialValue);
return (
<RadioGroup
name="favoriteFruit"
onChange={setSelectedFruit}
selectedValue={selectedFruit}
>
<label htmlFor="bananaOption">
<Radio id="bananaOption" value="banana" />
Bananas
</label>
<label htmlFor="appleOption">
<Radio id="appleOption" value="apple" />
Apples
</label>
<label htmlFor="orangeOption">
<Radio id="orangeOption" value="orange" />
Oranges
</label>
</RadioGroup>
);
};
RadioGroup
wrapper or already implicitly set on the Radio
components, which themselves are rendered as native HTML <input type="radio" />
elements.<input type="radio" />
’s, there is no need for aria-*
or role
attributes to be added – the element itself provides the necessary semantic meaning for accessability features (remember the first rule of ARIA use).RadioGroup
by default is rendered as a fieldset
HTML element. See below for ways of changing this, but I would strongly advice to consider re-styling it instead – fieldset
’s job is to group several controls as well as labels in a HTML form, which is prety much what we want to do here.useRadioGroup
If you need to get access to the provided RadioGroup
context in intermediate components between your RadioGroup
and Radio
components, react-radio-input
also provides a useRadioGroup
hook:
// CustomRadio
import { styled } from '@emotion/styled';
import { Radio, useRadioGroup } from 'react-radio-input';
const RadioWrapper = styled.label(({ isSelected }) => `
background-color: ${isSelected ? '#5a5ae8' : '#dddde1'};
`);
const CustomRadio = ({ description, label, value }) => {
const { selectedValue } = useRadioGroup();
const isSelected = value === selectedValue;
return (
<RadioWrapper isSelected={isSelected}>
<Radio value={value} />
{label}
{description}
<RadioWrapper />
);
};
// Parent
import { useState } from 'react';
import { RadioGroup } from 'react-radio-input';
import CustomRadio from './CustomRadio';
const ExampleComponent = () => {
const [selectedTier, setSelectedTier] = useState();
return (
<RadioGroup name="productTier" onChange={setSelectedTier} selectedValue={selectedTier}>
<legend>Select the size of your side project</legend>
<CustomRadio label="Hobby" description="1GB – $5/month" value="hobby" />
<CustomRadio
label="Freelancer"
description="5GB – $10/month"
value="freelancer"
/>
<CustomRadio
label="Startup"
description="10GB – $15/month"
value="startup"
/>
</RadioGroup>
);
};
Prop | Type | Description |
---|---|---|
children | React component | [React components] | |
component | String | React component | Defaults to fieldset – what HTML tag/React component to use for rendering the RadioGroup . |
disabled | Boolean | Disables all Radio ’s inside the RadioGroup . |
name | String | Unique name for the current RadioGroup – will be implicitly set on each Radio . |
onChange | Function | Will be called with the selected value as argument whenever a radio is selected. |
selectedValue | String | Number | Boolean | The currently selected value. |
Apart from the above, any additional props to RadioGroup
will be passed down to component
.
Prop | Type | Description |
---|---|---|
disabled | Boolean | Disables an individual Radio . |
value | String | Number | Boolean | Value of the Radio . |
Apart from the above, any additional props to Radio
will be passed down to the underlying <input type="radio" />
element.
FAQs
Radio buttons the proper (React) way.
The npm package react-radio-input receives a total of 40 weekly downloads. As such, react-radio-input popularity was classified as not popular.
We found that react-radio-input demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.