0.5.1 • Published 6 years ago

react-choices v0.5.1

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

react-choices

Component to build simple, flexible, and accessible choice components

The problem

You want a radio component that's simple and gives you complete control over rendering and state.

The solution

Choices is a conventional-component compatible React component, that follows the patterns of react-toggled to expose an API that renders nothing and simply encapsulates the logic of a radio component.

Installation

npm install --save react-choices

Usage

import React from 'react'
import { render } from 'react-dom'
import Choices from 'react-choices'

render(
  <Choices
    name="speed"
    label="Speed"
    availableStates={[
      { value: 'S' },
      { value: 'M' },
      { value: 'F' }
    ]}
    defaultValue="M"
  >
    {({
      name,
      label,
      states,
      selectedValue,
      setValue,
      hoverValue
    }) => (
      <div
        className="choices"
        role="radiogroup"
        aria-labelledby={`choices__label-${name}`}
        aria-activedescendant={`choice-${selectedValue}`}
      >
        <div id={`choices__label-${name}`} className="choices__label">
          {label}
        </div>
        <div className="choices__items">
          {states.map((state, idx) => (
            <button
              key={`choice-${idx}`}
              id={`choice-${state.value}`}
              tabIndex={state.selected ? 0 : -1}
              className={cx('choice', state.inputClassName, {
                'choice--focused': state.focused,
                'choice--hovered': state.hovered,
                'choice--selected': state.selected
              })}
              onMouseOver={hoverValue.bind(null, state.value)}
              onClick={setValue.bind(null, state.value)}
              role="radio"
              aria-checked={state.selected ? 'true' : 'false'}
            >
              {state.label}
            </button>
          ))}
        </div>
      </div>
    )}
  </Choices>,
  document.getElementById('root'),
)
0.5.1

6 years ago

0.5.0

6 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.0

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago