UNPKG

3.54 kBJavaScriptView Raw
1import _extends from "@babel/runtime/helpers/extends";
2import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
3import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
4import _createClass from "@babel/runtime/helpers/createClass";
5import _inherits from "@babel/runtime/helpers/inherits";
6import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
7import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
8
9function _createSuper(Derived) {
10 function isNativeReflectConstruct() {
11 if (typeof Reflect === "undefined" || !Reflect.construct) return false;
12 if (Reflect.construct.sham) return false;
13 if (typeof Proxy === "function") return true;
14
15 try {
16 Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
17 return true;
18 } catch (e) {
19 return false;
20 }
21 }
22
23 return function () {
24 var Super = _getPrototypeOf(Derived),
25 result;
26
27 if (isNativeReflectConstruct()) {
28 var NewTarget = _getPrototypeOf(this).constructor;
29
30 result = Reflect.construct(Super, arguments, NewTarget);
31 } else {
32 result = Super.apply(this, arguments);
33 }
34
35 return _possibleConstructorReturn(this, result);
36 };
37}
38
39import React, { Component } from 'react';
40import Checkbox from '../checkbox';
41import Radio from '../radio';
42
43var SelectionBox =
44/*#__PURE__*/
45function (_Component) {
46 _inherits(SelectionBox, _Component);
47
48 var _super = _createSuper(SelectionBox);
49
50 function SelectionBox(props) {
51 var _this;
52
53 _classCallCheck(this, SelectionBox);
54
55 _this = _super.call(this, props);
56 _this.state = {
57 checked: _this.getCheckState(props)
58 };
59 return _this;
60 }
61
62 _createClass(SelectionBox, [{
63 key: "componentDidMount",
64 value: function componentDidMount() {
65 this.subscribe();
66 }
67 }, {
68 key: "componentWillUnmount",
69 value: function componentWillUnmount() {
70 if (this.unsubscribe) {
71 this.unsubscribe();
72 }
73 }
74 }, {
75 key: "subscribe",
76 value: function subscribe() {
77 var _this2 = this;
78
79 var store = this.props.store;
80 this.unsubscribe = store.subscribe(function () {
81 var checked = _this2.getCheckState(_this2.props);
82
83 _this2.setState({
84 checked: checked
85 });
86 });
87 }
88 }, {
89 key: "getCheckState",
90 value: function getCheckState(props) {
91 var store = props.store,
92 defaultSelection = props.defaultSelection,
93 rowIndex = props.rowIndex;
94 var checked = false;
95
96 if (store.getState().selectionDirty) {
97 checked = store.getState().selectedRowKeys.indexOf(rowIndex) >= 0;
98 } else {
99 checked = store.getState().selectedRowKeys.indexOf(rowIndex) >= 0 || defaultSelection.indexOf(rowIndex) >= 0;
100 }
101
102 return checked;
103 }
104 }, {
105 key: "render",
106 value: function render() {
107 var _this$props = this.props,
108 type = _this$props.type,
109 rowIndex = _this$props.rowIndex,
110 rest = _objectWithoutProperties(_this$props, ["type", "rowIndex"]);
111
112 var checked = this.state.checked;
113
114 if (type === 'radio') {
115 return React.createElement(Radio, _extends({
116 checked: checked,
117 value: rowIndex
118 }, rest));
119 }
120
121 return React.createElement(Checkbox, _extends({
122 checked: checked
123 }, rest));
124 }
125 }]);
126
127 return SelectionBox;
128}(Component);
129
130export { SelectionBox as default };
131//# sourceMappingURL=SelectionBox.js.map