1 | import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
2 | import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
3 | import _createClass from "@babel/runtime/helpers/createClass";
|
4 | import _inherits from "@babel/runtime/helpers/inherits";
|
5 | import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
|
6 | import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
|
7 |
|
8 | function _createSuper(Derived) {
|
9 | function isNativeReflectConstruct() {
|
10 | if (typeof Reflect === "undefined" || !Reflect.construct) return false;
|
11 | if (Reflect.construct.sham) return false;
|
12 | if (typeof Proxy === "function") return true;
|
13 |
|
14 | try {
|
15 | Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
|
16 | return true;
|
17 | } catch (e) {
|
18 | return false;
|
19 | }
|
20 | }
|
21 |
|
22 | return function () {
|
23 | var Super = _getPrototypeOf(Derived),
|
24 | result;
|
25 |
|
26 | if (isNativeReflectConstruct()) {
|
27 | var NewTarget = _getPrototypeOf(this).constructor;
|
28 |
|
29 | result = Reflect.construct(Super, arguments, NewTarget);
|
30 | } else {
|
31 | result = Super.apply(this, arguments);
|
32 | }
|
33 |
|
34 | return _possibleConstructorReturn(this, result);
|
35 | };
|
36 | }
|
37 |
|
38 | import React, { Component } from 'react';
|
39 | import classNames from 'classnames';
|
40 | import Checkbox from '../checkbox';
|
41 | import Dropdown from '../dropdown';
|
42 | import Menu from '../menu';
|
43 | import Icon from '../icon';
|
44 |
|
45 | var SelectionCheckboxAll =
|
46 |
|
47 | function (_Component) {
|
48 | _inherits(SelectionCheckboxAll, _Component);
|
49 |
|
50 | var _super = _createSuper(SelectionCheckboxAll);
|
51 |
|
52 | function SelectionCheckboxAll(props) {
|
53 | var _this;
|
54 |
|
55 | _classCallCheck(this, SelectionCheckboxAll);
|
56 |
|
57 | _this = _super.call(this, props);
|
58 |
|
59 | _this.handleSelectAllChagne = function (e) {
|
60 | var checked = e.target.checked;
|
61 | var onSelect = _this.props.onSelect;
|
62 | onSelect(checked ? 'all' : 'removeAll', 0, null);
|
63 | };
|
64 |
|
65 | _this.defaultSelections = props.hideDefaultSelections ? [] : [{
|
66 | key: 'all',
|
67 | text: props.locale.selectAll,
|
68 | onSelect: function onSelect() {}
|
69 | }, {
|
70 | key: 'invert',
|
71 | text: props.locale.selectInvert,
|
72 | onSelect: function onSelect() {}
|
73 | }];
|
74 | _this.state = {
|
75 | checked: _this.getCheckState(props),
|
76 | indeterminate: _this.getIndeterminateState(props)
|
77 | };
|
78 | return _this;
|
79 | }
|
80 |
|
81 | _createClass(SelectionCheckboxAll, [{
|
82 | key: "componentDidMount",
|
83 | value: function componentDidMount() {
|
84 | this.subscribe();
|
85 | }
|
86 | }, {
|
87 | key: "componentWillReceiveProps",
|
88 | value: function componentWillReceiveProps(nextProps) {
|
89 | this.setCheckState(nextProps);
|
90 | }
|
91 | }, {
|
92 | key: "componentWillUnmount",
|
93 | value: function componentWillUnmount() {
|
94 | if (this.unsubscribe) {
|
95 | this.unsubscribe();
|
96 | }
|
97 | }
|
98 | }, {
|
99 | key: "subscribe",
|
100 | value: function subscribe() {
|
101 | var _this2 = this;
|
102 |
|
103 | var store = this.props.store;
|
104 | this.unsubscribe = store.subscribe(function () {
|
105 | _this2.setCheckState(_this2.props);
|
106 | });
|
107 | }
|
108 | }, {
|
109 | key: "checkSelection",
|
110 | value: function checkSelection(data, type, byDefaultChecked) {
|
111 | var _this$props = this.props,
|
112 | store = _this$props.store,
|
113 | getCheckboxPropsByItem = _this$props.getCheckboxPropsByItem,
|
114 | getRecordKey = _this$props.getRecordKey;
|
115 |
|
116 | if (type === 'every' || type === 'some') {
|
117 | return byDefaultChecked ? data[type](function (item, i) {
|
118 | return getCheckboxPropsByItem(item, i).defaultChecked;
|
119 | }) : data[type](function (item, i) {
|
120 | return store.getState().selectedRowKeys.indexOf(getRecordKey(item, i)) >= 0;
|
121 | });
|
122 | }
|
123 |
|
124 | return false;
|
125 | }
|
126 | }, {
|
127 | key: "setCheckState",
|
128 | value: function setCheckState(props) {
|
129 | var checked = this.getCheckState(props);
|
130 | var state = this.state;
|
131 | var indeterminate = this.getIndeterminateState(props);
|
132 |
|
133 | if (checked !== state.checked) {
|
134 | this.setState({
|
135 | checked: checked
|
136 | });
|
137 | }
|
138 |
|
139 | if (indeterminate !== state.indeterminate) {
|
140 | this.setState({
|
141 | indeterminate: indeterminate
|
142 | });
|
143 | }
|
144 | }
|
145 | }, {
|
146 | key: "getCheckState",
|
147 | value: function getCheckState(props) {
|
148 | var store = props.store,
|
149 | data = props.data;
|
150 | var checked;
|
151 |
|
152 | if (!data.length) {
|
153 | checked = false;
|
154 | } else {
|
155 | checked = store.getState().selectionDirty ? this.checkSelection(data, 'every', false) : this.checkSelection(data, 'every', false) || this.checkSelection(data, 'every', true);
|
156 | }
|
157 |
|
158 | return checked;
|
159 | }
|
160 | }, {
|
161 | key: "getIndeterminateState",
|
162 | value: function getIndeterminateState(props) {
|
163 | var store = props.store,
|
164 | data = props.data;
|
165 | var indeterminate;
|
166 |
|
167 | if (!data.length) {
|
168 | indeterminate = false;
|
169 | } else {
|
170 | indeterminate = store.getState().selectionDirty ? this.checkSelection(data, 'some', false) && !this.checkSelection(data, 'every', false) : this.checkSelection(data, 'some', false) && !this.checkSelection(data, 'every', false) || this.checkSelection(data, 'some', true) && !this.checkSelection(data, 'every', true);
|
171 | }
|
172 |
|
173 | return indeterminate;
|
174 | }
|
175 | }, {
|
176 | key: "renderMenus",
|
177 | value: function renderMenus(selections) {
|
178 | var onSelect = this.props.onSelect;
|
179 | return selections.map(function (selection, index) {
|
180 | return React.createElement(Menu.Item, {
|
181 | key: selection.key || index
|
182 | }, React.createElement("div", {
|
183 | onClick: function onClick() {
|
184 | return onSelect(selection.key, index, selection.onSelect);
|
185 | }
|
186 | }, selection.text));
|
187 | });
|
188 | }
|
189 | }, {
|
190 | key: "render",
|
191 | value: function render() {
|
192 | var _this$props2 = this.props,
|
193 | disabled = _this$props2.disabled,
|
194 | prefixCls = _this$props2.prefixCls,
|
195 | selections = _this$props2.selections,
|
196 | getPopupContainer = _this$props2.getPopupContainer;
|
197 | var _this$state = this.state,
|
198 | checked = _this$state.checked,
|
199 | indeterminate = _this$state.indeterminate;
|
200 | var selectionPrefixCls = "".concat(prefixCls, "-selection");
|
201 | var customSelections = null;
|
202 |
|
203 | if (selections) {
|
204 | var newSelections = Array.isArray(selections) ? this.defaultSelections.concat(selections) : this.defaultSelections;
|
205 | var menu = React.createElement(Menu, {
|
206 | className: "".concat(selectionPrefixCls, "-menu"),
|
207 | selectedKeys: []
|
208 | }, this.renderMenus(newSelections));
|
209 | customSelections = newSelections.length > 0 ? React.createElement(Dropdown, {
|
210 | overlay: menu,
|
211 | getPopupContainer: getPopupContainer
|
212 | }, React.createElement("div", {
|
213 | className: "".concat(selectionPrefixCls, "-down")
|
214 | }, React.createElement(Icon, {
|
215 | type: "expand_more"
|
216 | }))) : null;
|
217 | }
|
218 |
|
219 | return React.createElement("div", {
|
220 | className: selectionPrefixCls
|
221 | }, React.createElement(Checkbox, {
|
222 | className: classNames(_defineProperty({}, "".concat(selectionPrefixCls, "-select-all-custom"), customSelections)),
|
223 | checked: checked,
|
224 | indeterminate: indeterminate,
|
225 | disabled: disabled,
|
226 | onChange: this.handleSelectAllChagne
|
227 | }), customSelections);
|
228 | }
|
229 | }]);
|
230 |
|
231 | return SelectionCheckboxAll;
|
232 | }(Component);
|
233 |
|
234 | export { SelectionCheckboxAll as default };
|
235 |
|