npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-radio-context

v2.0.0

Published

Create radios in React without the hassle.

Downloads

433

Readme

⚛ React Radio Context

This package is intended to make your life easier when working with <input type="radio" /> in React. It provides a <RadioGroup /> wrapper component and a <Radio /> component that can be used inside of a RadioGroup wrapper. Instead of having to manage the state by yourself the RadioGroup component takes care of that.

Installation

npm install react-radio-context

or

yarn add react-radio-context

Example

What does react-radio-context do and how does it do that?

This is your average radio group:

const FruitSelector = () => {
    const [fruit, setFruit] = React.useState();
    const handleFruitChange = (e) => {
        const {
            target: { value },
        } = e;

        setFruit(value);
    };

    return (
        <form>
            <input
                onChange={handleFruitChange}
                type="radio"
                name="fruit"
                value="apple"
                checked={fruit === 'apple'}
            />{' '}
            Apple
            <input
                onChange={handleFruitChange}
                type="radio"
                name="fruit"
                value="orange"
                checked={fruit === 'orange'}
            /> Orange
            <input
                onChange={handleFruitChange}
                type="radio"
                name="fruit"
                value="watermelon"
                checked={fruit === 'watermelon'}
            />{' '}
            Watermelon
        </form>
    );
};

Repetitive, hard to manipulate and easily desynchronized. Lift up name and onChange, and (optionally) give the group an initially selectedValue:

import { Radio, RadioGroup } from 'react-radio-context';

const FruitSelector = () => (
    <RadioGroup name="fruits" selectedValue="pineapple">
        <Radio value="kiwi" /> Kiwi
        <Radio value="pineapple" /> Pineapple
        <Radio value="watermelon" /> Watermelon
    </RadioGroup>
);

Since this component uses React's Context API, <Radio> elements can by anywhere inside of a <RadioGroup> and they do not have to be a direct descendant. You can also define a custom onChange handler which is called after the state was updated and receives the Event object and the new value as its parameters.

<RadioGroup
    name="frameworks"
    onChange={(event, newValue) => {
        console.log(newValue);
    }}
>
    <p>
        <label htmlFor="react">
            <Radio value="react" id="react" /> React
        </label>
    </p>
    <p>
        <label htmlFor="vue">
            <Radio value="vue" id="vue" /> Vue
        </label>
    </p>
</RadioGroup>

Components

<RadioGroup />

| Prop | Type | Description | | ---------- | --------------------------------------------------------------------- | ------------------------------------------------------- | | onChange | (event: React.ChangeEvent<HTMLInputElement>, value: string) => void | Will be called on every time a radio changes its state. | | name | string | Name for all radioes within one <RadioGroup> | | value | string | Value of the currently selected <Radio> element` |

<Radio />

The Radio component passes all of its props the the underlying <input type="radio" /> element. All valid HTML attributes can be used with the exception of checked, name, onChange as they will be set by the parent <RadioGroup> component.

Todo

  • Add comprehensive tests

License

MIT.