2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2019-08-05 18:38:10 +08:00
|
|
|
import classNames from 'classnames';
|
2019-08-14 17:44:09 +08:00
|
|
|
import { polyfill } from 'react-lifecycles-compat';
|
2018-03-08 20:34:40 +08:00
|
|
|
import Checkbox, { CheckboxChangeEvent } from '../checkbox';
|
2017-02-23 19:29:47 +08:00
|
|
|
import Dropdown from '../dropdown';
|
|
|
|
import Menu from '../menu';
|
|
|
|
import Icon from '../icon';
|
2017-11-21 13:48:37 +08:00
|
|
|
import { SelectionCheckboxAllProps, SelectionCheckboxAllState, SelectionItem } from './interface';
|
2017-02-23 19:29:47 +08:00
|
|
|
|
2019-08-14 17:44:09 +08:00
|
|
|
function checkSelection<T>({
|
|
|
|
store,
|
|
|
|
getCheckboxPropsByItem,
|
|
|
|
getRecordKey,
|
|
|
|
data,
|
|
|
|
type,
|
|
|
|
byDefaultChecked,
|
|
|
|
}: {
|
|
|
|
store: SelectionCheckboxAllProps<T>['store'];
|
|
|
|
getCheckboxPropsByItem: SelectionCheckboxAllProps<T>['getCheckboxPropsByItem'];
|
|
|
|
getRecordKey: SelectionCheckboxAllProps<T>['getRecordKey'];
|
|
|
|
data: T[];
|
|
|
|
type: string;
|
|
|
|
byDefaultChecked: boolean;
|
|
|
|
}) {
|
|
|
|
// type should be 'every' | 'some'
|
|
|
|
if (type === 'every' || type === 'some') {
|
|
|
|
return byDefaultChecked
|
|
|
|
? data[type]((item, i) => getCheckboxPropsByItem(item, i).defaultChecked)
|
|
|
|
: data[type](
|
|
|
|
(item, i) => store.getState().selectedRowKeys.indexOf(getRecordKey(item, i)) >= 0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getIndeterminateState<T>(props: SelectionCheckboxAllProps<T>) {
|
|
|
|
const { store, data } = props;
|
|
|
|
if (!data.length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const someCheckedNotByDefaultChecked =
|
|
|
|
checkSelection<T>({
|
|
|
|
...props,
|
|
|
|
data,
|
|
|
|
type: 'some',
|
|
|
|
byDefaultChecked: false,
|
|
|
|
}) &&
|
|
|
|
!checkSelection<T>({
|
|
|
|
...props,
|
|
|
|
data,
|
|
|
|
type: 'every',
|
|
|
|
byDefaultChecked: false,
|
|
|
|
});
|
|
|
|
const someCheckedByDefaultChecked =
|
|
|
|
checkSelection<T>({
|
|
|
|
...props,
|
|
|
|
data,
|
|
|
|
type: 'some',
|
|
|
|
byDefaultChecked: true,
|
|
|
|
}) &&
|
|
|
|
!checkSelection<T>({
|
|
|
|
...props,
|
|
|
|
data,
|
|
|
|
type: 'every',
|
|
|
|
byDefaultChecked: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (store.getState().selectionDirty) {
|
|
|
|
return someCheckedNotByDefaultChecked;
|
|
|
|
}
|
|
|
|
return someCheckedNotByDefaultChecked || someCheckedByDefaultChecked;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCheckState<T>(props: SelectionCheckboxAllProps<T>) {
|
|
|
|
const { store, data } = props;
|
|
|
|
if (!data.length) {
|
|
|
|
return false;
|
2019-08-14 19:38:01 +08:00
|
|
|
}
|
|
|
|
if (store.getState().selectionDirty) {
|
2019-08-14 17:44:09 +08:00
|
|
|
return checkSelection({
|
|
|
|
...props,
|
|
|
|
data,
|
|
|
|
type: 'every',
|
|
|
|
byDefaultChecked: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
checkSelection({
|
|
|
|
...props,
|
|
|
|
data,
|
|
|
|
type: 'every',
|
|
|
|
byDefaultChecked: false,
|
|
|
|
}) ||
|
|
|
|
checkSelection({
|
|
|
|
...props,
|
|
|
|
data,
|
|
|
|
type: 'every',
|
|
|
|
byDefaultChecked: true,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
class SelectionCheckboxAll<T> extends React.Component<
|
2018-12-07 16:17:45 +08:00
|
|
|
SelectionCheckboxAllProps<T>,
|
|
|
|
SelectionCheckboxAllState
|
|
|
|
> {
|
2019-08-14 17:44:09 +08:00
|
|
|
state = {
|
|
|
|
checked: false,
|
|
|
|
indeterminate: false,
|
|
|
|
};
|
|
|
|
|
2016-11-11 15:26:51 +08:00
|
|
|
unsubscribe: () => void;
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
defaultSelections: SelectionItem[];
|
2016-11-11 15:26:51 +08:00
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
constructor(props: SelectionCheckboxAllProps<T>) {
|
2016-11-11 15:26:51 +08:00
|
|
|
super(props);
|
2018-12-07 16:17:45 +08:00
|
|
|
this.defaultSelections = props.hideDefaultSelections
|
|
|
|
? []
|
|
|
|
: [
|
|
|
|
{
|
|
|
|
key: 'all',
|
|
|
|
text: props.locale.selectAll,
|
|
|
|
onSelect: () => {},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: 'invert',
|
|
|
|
text: props.locale.selectInvert,
|
|
|
|
onSelect: () => {},
|
|
|
|
},
|
|
|
|
];
|
2019-08-14 17:44:09 +08:00
|
|
|
}
|
2017-03-06 17:14:34 +08:00
|
|
|
|
2019-08-14 17:44:09 +08:00
|
|
|
static getDerivedStateFromProps<T>(
|
|
|
|
props: SelectionCheckboxAllProps<T>,
|
|
|
|
state: SelectionCheckboxAllState,
|
|
|
|
) {
|
|
|
|
const checked = getCheckState(props);
|
|
|
|
const indeterminate = getIndeterminateState(props);
|
|
|
|
const newState: SelectionCheckboxAllState = {};
|
|
|
|
if (indeterminate !== state.indeterminate) {
|
|
|
|
newState.indeterminate = indeterminate;
|
|
|
|
}
|
|
|
|
if (checked !== state.checked) {
|
|
|
|
newState.checked = checked;
|
|
|
|
}
|
|
|
|
return newState;
|
2016-11-11 15:26:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.subscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.unsubscribe) {
|
|
|
|
this.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
setCheckState(props: SelectionCheckboxAllProps<T>) {
|
2019-08-14 17:44:09 +08:00
|
|
|
const checked = getCheckState(props);
|
|
|
|
const indeterminate = getIndeterminateState(props);
|
2018-12-07 16:17:45 +08:00
|
|
|
this.setState(prevState => {
|
2018-11-10 21:40:21 +08:00
|
|
|
const newState: SelectionCheckboxAllState = {};
|
2018-06-05 20:45:19 +08:00
|
|
|
if (indeterminate !== prevState.indeterminate) {
|
|
|
|
newState.indeterminate = indeterminate;
|
|
|
|
}
|
|
|
|
if (checked !== prevState.checked) {
|
|
|
|
newState.checked = checked;
|
|
|
|
}
|
|
|
|
return newState;
|
|
|
|
});
|
2016-11-22 13:42:45 +08:00
|
|
|
}
|
|
|
|
|
2018-12-17 16:09:41 +08:00
|
|
|
handleSelectAllChange = (e: CheckboxChangeEvent) => {
|
2019-08-05 18:38:10 +08:00
|
|
|
const { checked } = e.target;
|
2017-02-23 19:29:47 +08:00
|
|
|
this.props.onSelect(checked ? 'all' : 'removeAll', 0, null);
|
2018-12-07 16:17:45 +08:00
|
|
|
};
|
2017-02-23 19:29:47 +08:00
|
|
|
|
2019-08-05 18:38:10 +08:00
|
|
|
subscribe() {
|
|
|
|
const { store } = this.props;
|
|
|
|
this.unsubscribe = store.subscribe(() => {
|
|
|
|
this.setCheckState(this.props);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
renderMenus(selections: SelectionItem[]) {
|
2017-02-23 19:29:47 +08:00
|
|
|
return selections.map((selection, index) => {
|
|
|
|
return (
|
2018-12-07 16:17:45 +08:00
|
|
|
<Menu.Item key={selection.key || index}>
|
2017-02-23 19:29:47 +08:00
|
|
|
<div
|
2018-12-07 16:17:45 +08:00
|
|
|
onClick={() => {
|
|
|
|
this.props.onSelect(selection.key, index, selection.onSelect);
|
|
|
|
}}
|
2017-02-23 19:29:47 +08:00
|
|
|
>
|
|
|
|
{selection.text}
|
|
|
|
</div>
|
|
|
|
</Menu.Item>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-11-11 15:26:51 +08:00
|
|
|
render() {
|
2017-05-24 12:17:40 +08:00
|
|
|
const { disabled, prefixCls, selections, getPopupContainer } = this.props;
|
2016-11-11 15:26:51 +08:00
|
|
|
const { checked, indeterminate } = this.state;
|
|
|
|
|
2018-11-10 21:40:21 +08:00
|
|
|
const selectionPrefixCls = `${prefixCls}-selection`;
|
2017-02-23 19:29:47 +08:00
|
|
|
|
2017-03-24 11:44:36 +08:00
|
|
|
let customSelections: React.ReactNode = null;
|
2017-02-23 19:29:47 +08:00
|
|
|
|
2017-03-24 11:44:36 +08:00
|
|
|
if (selections) {
|
2018-12-07 16:17:45 +08:00
|
|
|
const newSelections = Array.isArray(selections)
|
|
|
|
? this.defaultSelections.concat(selections)
|
|
|
|
: this.defaultSelections;
|
2017-02-23 19:29:47 +08:00
|
|
|
|
2017-08-27 15:44:22 +08:00
|
|
|
const menu = (
|
2018-12-07 16:17:45 +08:00
|
|
|
<Menu className={`${selectionPrefixCls}-menu`} selectedKeys={[]}>
|
2017-03-24 11:44:36 +08:00
|
|
|
{this.renderMenus(newSelections)}
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
customSelections =
|
|
|
|
newSelections.length > 0 ? (
|
|
|
|
<Dropdown overlay={menu} getPopupContainer={getPopupContainer}>
|
|
|
|
<div className={`${selectionPrefixCls}-down`}>
|
|
|
|
<Icon type="down" />
|
|
|
|
</div>
|
|
|
|
</Dropdown>
|
|
|
|
) : null;
|
2017-03-24 11:44:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={selectionPrefixCls}>
|
|
|
|
<Checkbox
|
|
|
|
className={classNames({ [`${selectionPrefixCls}-select-all-custom`]: customSelections })}
|
|
|
|
checked={checked}
|
|
|
|
indeterminate={indeterminate}
|
|
|
|
disabled={disabled}
|
2018-12-17 16:09:41 +08:00
|
|
|
onChange={this.handleSelectAllChange}
|
2017-03-24 11:44:36 +08:00
|
|
|
/>
|
|
|
|
{customSelections}
|
2017-02-23 19:29:47 +08:00
|
|
|
</div>
|
2016-11-11 15:26:51 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-14 17:44:09 +08:00
|
|
|
|
|
|
|
polyfill(SelectionCheckboxAll);
|
|
|
|
|
|
|
|
export default SelectionCheckboxAll;
|