1 | import _extends from 'babel-runtime/helpers/extends';
|
2 | import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
|
3 | import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
|
4 | import _inherits from 'babel-runtime/helpers/inherits';
|
5 |
|
6 | var _class, _temp;
|
7 |
|
8 | import React, { Component } from 'react';
|
9 | import PropTypes from 'prop-types';
|
10 | import classNames from 'classnames';
|
11 | import { polyfill } from 'react-lifecycles-compat';
|
12 | import { obj, func } from '../util';
|
13 | import Tag from './tag';
|
14 |
|
15 | var noop = func.noop,
|
16 | bindCtx = func.bindCtx;
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var Selectable = (_temp = _class = function (_Component) {
|
23 | _inherits(Selectable, _Component);
|
24 |
|
25 | function Selectable(props) {
|
26 | _classCallCheck(this, Selectable);
|
27 |
|
28 | var _this = _possibleConstructorReturn(this, _Component.call(this, props));
|
29 |
|
30 | _this.state = {
|
31 | checked: 'checked' in props ? props.checked : props.defaultChecked || false
|
32 | };
|
33 |
|
34 | bindCtx(_this, ['handleClick']);
|
35 | return _this;
|
36 | }
|
37 |
|
38 | Selectable.getDerivedStateFromProps = function getDerivedStateFromProps(props, state) {
|
39 | if (props.checked !== undefined && props.checked !== state.checked) {
|
40 | return {
|
41 | checked: props.checked
|
42 | };
|
43 | }
|
44 |
|
45 | return null;
|
46 | };
|
47 |
|
48 | Selectable.prototype.handleClick = function handleClick(e) {
|
49 | e && e.preventDefault();
|
50 |
|
51 | if (this.props.disabled) {
|
52 | return false;
|
53 | }
|
54 |
|
55 | var checked = this.state.checked;
|
56 |
|
57 |
|
58 | this.setState({
|
59 | checked: !checked
|
60 | });
|
61 |
|
62 | this.props.onChange(!checked, e);
|
63 | };
|
64 |
|
65 | Selectable.prototype.render = function render() {
|
66 | var attrFilterTarget = ['checked', 'defaultChecked', 'onChange', 'className',
|
67 |
|
68 | '_shape', 'closable'];
|
69 |
|
70 | var others = obj.pickOthers(attrFilterTarget, this.props);
|
71 | var isChecked = 'checked' in this.props ? this.props.checked : this.state.checked;
|
72 | var clazz = classNames(this.props.className, {
|
73 | checked: isChecked
|
74 | });
|
75 | return React.createElement(Tag, _extends({}, others, {
|
76 | role: 'checkbox',
|
77 | _shape: 'checkable',
|
78 | 'aria-checked': isChecked,
|
79 | className: clazz,
|
80 | onClick: this.handleClick
|
81 | }));
|
82 | };
|
83 |
|
84 | return Selectable;
|
85 | }(Component), _class.propTypes = {
|
86 | |
87 |
|
88 |
|
89 |
|
90 | checked: PropTypes.bool,
|
91 | |
92 |
|
93 |
|
94 |
|
95 | defaultChecked: PropTypes.bool,
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 | onChange: PropTypes.func,
|
102 | |
103 |
|
104 |
|
105 | disabled: PropTypes.bool,
|
106 | className: PropTypes.any
|
107 | }, _class.defaultProps = {
|
108 | onChange: noop
|
109 | }, _temp);
|
110 | Selectable.displayName = 'Selectable';
|
111 |
|
112 |
|
113 | export default polyfill(Selectable); |
\ | No newline at end of file |