|
1 | 1 | import {append, contains, without} from 'ramda';
|
2 |
| -import React, {PropTypes} from 'react'; |
| 2 | +import React, {Component, PropTypes} from 'react'; |
3 | 3 |
|
4 | 4 | /**
|
5 | 5 | * Checklist is a component that encapsulates several checkboxes.
|
6 | 6 | * The values and labels of the checklist is specified in the `options`
|
7 | 7 | * property and the checked items are specified with the `values` property.
|
8 | 8 | * Each checkbox is rendered as an input with a surrounding label.
|
9 | 9 | */
|
10 |
| -export default function Checklist(props) { |
11 |
| - const { |
12 |
| - fireEvent, |
13 |
| - id, |
14 |
| - inputClassName, |
15 |
| - inputStyle, |
16 |
| - labelClassName, |
17 |
| - labelStyle, |
18 |
| - options, |
19 |
| - values, |
20 |
| - setProps |
21 |
| - } = props; |
22 |
| - return ( |
23 |
| - <div id={id}> |
24 |
| - {options.map(option => ( |
25 |
| - <label |
26 |
| - key={option.value} |
27 |
| - style={labelStyle} |
28 |
| - className={labelClassName} |
29 |
| - > |
30 |
| - <input |
31 |
| - checked={contains(option.value, values)} |
32 |
| - className={inputClassName} |
33 |
| - disabled={Boolean(option.disabled)} |
34 |
| - style={inputStyle} |
35 |
| - type="checkbox" |
36 |
| - onChange={() => { |
37 |
| - if (setProps) { |
| 10 | +export default class Checklist extends Component { |
| 11 | + constructor(props) { |
| 12 | + super(props); |
| 13 | + this.state = {values: props.values}; |
| 14 | + } |
| 15 | + |
| 16 | + componentWillReceiveProps(newProps) { |
| 17 | + this.setState({values: newProps.values}); |
| 18 | + } |
| 19 | + |
| 20 | + render() { |
| 21 | + const { |
| 22 | + fireEvent, |
| 23 | + id, |
| 24 | + inputClassName, |
| 25 | + inputStyle, |
| 26 | + labelClassName, |
| 27 | + labelStyle, |
| 28 | + options, |
| 29 | + setProps |
| 30 | + } = this.props; |
| 31 | + const {values} = this.state; |
| 32 | + |
| 33 | + return ( |
| 34 | + <div id={id}> |
| 35 | + {options.map(option => ( |
| 36 | + <label |
| 37 | + key={option.value} |
| 38 | + style={labelStyle} |
| 39 | + className={labelClassName} |
| 40 | + > |
| 41 | + <input |
| 42 | + checked={contains(option.value, values)} |
| 43 | + className={inputClassName} |
| 44 | + disabled={Boolean(option.disabled)} |
| 45 | + style={inputStyle} |
| 46 | + type="checkbox" |
| 47 | + onChange={() => { |
38 | 48 | let newValues;
|
39 | 49 | if (contains(option.value, values)) {
|
40 | 50 | newValues = without([option.value], values);
|
41 | 51 | } else {
|
42 | 52 | newValues = append(option.value, values);
|
43 | 53 | }
|
44 |
| - setProps({values: newValues}); |
45 |
| - } |
46 |
| - if (fireEvent) fireEvent({event: 'change'}); |
47 |
| - }} |
48 |
| - /> |
49 |
| - {option.label} |
50 |
| - </label> |
51 |
| - ))} |
52 |
| - </div> |
53 |
| - ); |
| 54 | + this.setState({values: newValues}); |
| 55 | + if (setProps) setProps({values: newValues}); |
| 56 | + if (fireEvent) fireEvent({event: 'change'}); |
| 57 | + }} |
| 58 | + /> |
| 59 | + {option.label} |
| 60 | + </label> |
| 61 | + ))} |
| 62 | + </div> |
| 63 | + ); |
| 64 | + } |
54 | 65 | }
|
55 | 66 |
|
56 | 67 | Checklist.propTypes = {
|
|
0 commit comments