react-radioactive
DRY radio buttons for React
Installation
npm install react-radioactive
The Problem
Unlike other HTML inputs, radio buttons always operate as a group. Consequently, we end up specifying the same attributes over and over again:
<input ="radio" ="language" = ="ruby" /> Ruby<input ="radio" ="language" = ="python" /> Python<input ="radio" ="language" = ="javascript" /> Javascript
To make matters worse, the only way to indicate the radio group's value is by setting the checked
attribute to true
on the appropriate radio button. This effectively means that we need to define a custom checked
attribute for each individual radio button.
The Solution
react-radioactive
solves these problems through the use of factory functions. First, we define the behavior of the group:
var rf = factory;var r = ;
Then, in our JSX template, we can invoke the r(...)
function to quickly build radio buttons that belong to this group:
<div> Ruby Python Javascript</div>
Example
Here's a more complete example:
var rf = factory; var MyComponent = React
This will produce HTML output such as the following:
What's your favorite language? Ruby Python Javascript Java
Notice that the input for Javascript specifies checked=""
. This is because its value
attribute matches the radio group's selectedValue
.
TODO
- Provide
factory
as a default export.