antd
Version:
An enterprise-class UI design language and React-based implementation
189 lines (175 loc) • 7.48 kB
JavaScript
import _defineProperty from 'babel-runtime/helpers/defineProperty';
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';
import * as React from 'react';
import Checkbox from '../checkbox';
import Dropdown from '../dropdown';
import Menu from '../menu';
import Icon from '../icon';
import classNames from 'classnames';
var SelectionCheckboxAll = function (_React$Component) {
_inherits(SelectionCheckboxAll, _React$Component);
function SelectionCheckboxAll(props) {
_classCallCheck(this, SelectionCheckboxAll);
var _this = _possibleConstructorReturn(this, (SelectionCheckboxAll.__proto__ || Object.getPrototypeOf(SelectionCheckboxAll)).call(this, props));
_this.handleSelectAllChagne = function (e) {
var checked = e.target.checked;
_this.props.onSelect(checked ? 'all' : 'removeAll', 0, null);
};
_this.defaultSelections = props.hideDefaultSelections ? [] : [{
key: 'all',
text: props.locale.selectAll,
onSelect: function onSelect() {}
}, {
key: 'invert',
text: props.locale.selectInvert,
onSelect: function onSelect() {}
}];
_this.state = {
checked: _this.getCheckState(props),
indeterminate: _this.getIndeterminateState(props)
};
return _this;
}
_createClass(SelectionCheckboxAll, [{
key: 'componentDidMount',
value: function componentDidMount() {
this.subscribe();
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
this.setCheckState(nextProps);
}
}, {
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 () {
_this2.setCheckState(_this2.props);
});
}
}, {
key: 'checkSelection',
value: function checkSelection(data, type, byDefaultChecked) {
var _props = this.props,
store = _props.store,
getCheckboxPropsByItem = _props.getCheckboxPropsByItem,
getRecordKey = _props.getRecordKey;
// type should be 'every' | 'some'
if (type === 'every' || type === 'some') {
return byDefaultChecked ? data[type](function (item, i) {
return getCheckboxPropsByItem(item, i).defaultChecked;
}) : data[type](function (item, i) {
return store.getState().selectedRowKeys.indexOf(getRecordKey(item, i)) >= 0;
});
}
return false;
}
}, {
key: 'setCheckState',
value: function setCheckState(props) {
var checked = this.getCheckState(props);
var indeterminate = this.getIndeterminateState(props);
if (checked !== this.state.checked) {
this.setState({ checked: checked });
}
if (indeterminate !== this.state.indeterminate) {
this.setState({ indeterminate: indeterminate });
}
}
}, {
key: 'getCheckState',
value: function getCheckState(props) {
var store = props.store,
data = props.data;
var checked = void 0;
if (!data.length) {
checked = false;
} else {
checked = store.getState().selectionDirty ? this.checkSelection(data, 'every', false) : this.checkSelection(data, 'every', false) || this.checkSelection(data, 'every', true);
}
return checked;
}
}, {
key: 'getIndeterminateState',
value: function getIndeterminateState(props) {
var store = props.store,
data = props.data;
var indeterminate = void 0;
if (!data.length) {
indeterminate = false;
} else {
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);
}
return indeterminate;
}
}, {
key: 'renderMenus',
value: function renderMenus(selections) {
var _this3 = this;
return selections.map(function (selection, index) {
return React.createElement(
Menu.Item,
{ key: selection.key || index },
React.createElement(
'div',
{ onClick: function onClick() {
_this3.props.onSelect(selection.key, index, selection.onSelect);
} },
selection.text
)
);
});
}
}, {
key: 'render',
value: function render() {
var _props2 = this.props,
disabled = _props2.disabled,
prefixCls = _props2.prefixCls,
selections = _props2.selections,
getPopupContainer = _props2.getPopupContainer;
var _state = this.state,
checked = _state.checked,
indeterminate = _state.indeterminate;
var selectionPrefixCls = prefixCls + '-selection';
var customSelections = null;
if (selections) {
var newSelections = Array.isArray(selections) ? this.defaultSelections.concat(selections) : this.defaultSelections;
var menu = React.createElement(
Menu,
{ className: selectionPrefixCls + '-menu', selectedKeys: [] },
this.renderMenus(newSelections)
);
customSelections = newSelections.length > 0 ? React.createElement(
Dropdown,
{ overlay: menu, getPopupContainer: getPopupContainer },
React.createElement(
'div',
{ className: selectionPrefixCls + '-down' },
React.createElement(Icon, { type: 'down' })
)
) : null;
}
return React.createElement(
'div',
{ className: selectionPrefixCls },
React.createElement(Checkbox, { className: classNames(_defineProperty({}, selectionPrefixCls + '-select-all-custom', customSelections)), checked: checked, indeterminate: indeterminate, disabled: disabled, onChange: this.handleSelectAllChagne }),
customSelections
);
}
}]);
return SelectionCheckboxAll;
}(React.Component);
export default SelectionCheckboxAll;