项目作者: sebinsua

项目描述 :
:clipboard: Component to build simple, flexible, and accessible choice components
高级语言: JavaScript
项目地址: git://github.com/sebinsua/react-choices.git
创建时间: 2018-01-04T21:28:59Z
项目社区:https://github.com/sebinsua/react-choices

开源协议:MIT License

下载


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

  1. npm install --save react-choices

Usage

  1. import React from 'react'
  2. import { render } from 'react-dom'
  3. import Choices from 'react-choices'
  4. render(
  5. <Choices
  6. name="speed"
  7. label="Speed"
  8. availableStates={[
  9. { value: 'S' },
  10. { value: 'M' },
  11. { value: 'F' }
  12. ]}
  13. defaultValue="M"
  14. >
  15. {({
  16. name,
  17. label,
  18. states,
  19. selectedValue,
  20. setValue,
  21. hoverValue
  22. }) => (
  23. <div
  24. className="choices"
  25. role="radiogroup"
  26. aria-labelledby={`choices__label-${name}`}
  27. aria-activedescendant={`choice-${selectedValue}`}
  28. >
  29. <div id={`choices__label-${name}`} className="choices__label">
  30. {label}
  31. </div>
  32. <div className="choices__items">
  33. {states.map((state, idx) => (
  34. <button
  35. key={`choice-${idx}`}
  36. id={`choice-${state.value}`}
  37. tabIndex={state.selected ? 0 : -1}
  38. className={cx('choice', state.inputClassName, {
  39. 'choice--focused': state.focused,
  40. 'choice--hovered': state.hovered,
  41. 'choice--selected': state.selected
  42. })}
  43. onMouseOver={hoverValue.bind(null, state.value)}
  44. onClick={setValue.bind(null, state.value)}
  45. role="radio"
  46. aria-checked={state.selected ? 'true' : 'false'}
  47. >
  48. {state.label}
  49. </button>
  50. ))}
  51. </div>
  52. </div>
  53. )}
  54. </Choices>,
  55. document.getElementById('root'),
  56. )