UNPKG

antd

Version:

An enterprise-class UI design language and React-based implementation

90 lines (79 loc) 3.18 kB
import _extends from 'babel-runtime/helpers/extends'; import _classCallCheck from 'babel-runtime/helpers/classCallCheck'; import _createClass from 'babel-runtime/helpers/createClass'; import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn'; import _inherits from 'babel-runtime/helpers/inherits'; var __rest = this && this.__rest || function (s, e) { var t = {}; for (var p in s) { if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; }if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0) t[p[i]] = s[p[i]]; }return t; }; import * as React from 'react'; import Checkbox from '../checkbox'; import Radio from '../radio'; var SelectionBox = function (_React$Component) { _inherits(SelectionBox, _React$Component); function SelectionBox(props) { _classCallCheck(this, SelectionBox); var _this = _possibleConstructorReturn(this, (SelectionBox.__proto__ || Object.getPrototypeOf(SelectionBox)).call(this, props)); _this.state = { checked: _this.getCheckState(props) }; return _this; } _createClass(SelectionBox, [{ key: 'componentDidMount', value: function componentDidMount() { this.subscribe(); } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { if (this.unsubscribe) { this.unsubscribe(); } } }, { key: 'subscribe', value: function subscribe() { var _this2 = this; var store = this.props.store; this.unsubscribe = store.subscribe(function () { var checked = _this2.getCheckState(_this2.props); _this2.setState({ checked: checked }); }); } }, { key: 'getCheckState', value: function getCheckState(props) { var store = props.store, defaultSelection = props.defaultSelection, rowIndex = props.rowIndex; var checked = false; if (store.getState().selectionDirty) { checked = store.getState().selectedRowKeys.indexOf(rowIndex) >= 0; } else { checked = store.getState().selectedRowKeys.indexOf(rowIndex) >= 0 || defaultSelection.indexOf(rowIndex) >= 0; } return checked; } }, { key: 'render', value: function render() { var _a = this.props, type = _a.type, rowIndex = _a.rowIndex, rest = __rest(_a, ["type", "rowIndex"]);var checked = this.state.checked; if (type === 'radio') { return React.createElement(Radio, _extends({ checked: checked, value: rowIndex }, rest)); } else { return React.createElement(Checkbox, _extends({ checked: checked }, rest)); } } }]); return SelectionBox; }(React.Component); export default SelectionBox;