2016-11-11 15:26:51 +08:00
|
|
|
import React from 'react';
|
|
|
|
import Checkbox from '../checkbox';
|
|
|
|
import { Store } from './createStore';
|
|
|
|
|
|
|
|
export interface SelectionCheckboxAllProps {
|
|
|
|
store: Store;
|
|
|
|
disabled: boolean;
|
2016-12-16 17:53:08 +08:00
|
|
|
getCheckboxPropsByItem: (item, index) => any;
|
2016-11-11 15:26:51 +08:00
|
|
|
getRecordKey: (record, index?) => string;
|
|
|
|
data: any[];
|
|
|
|
onChange: (e) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class SelectionCheckboxAll extends React.Component<SelectionCheckboxAllProps, any> {
|
|
|
|
unsubscribe: () => void;
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2016-11-22 13:42:45 +08:00
|
|
|
checked: this.getCheckState(props),
|
|
|
|
indeterminate: this.getIndeterminateState(props),
|
2016-11-11 15:26:51 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.subscribe();
|
|
|
|
}
|
|
|
|
|
2016-11-22 13:42:45 +08:00
|
|
|
componentWillReceiveProps(nextProps) {
|
|
|
|
this.setCheckState(nextProps);
|
|
|
|
}
|
|
|
|
|
2016-11-11 15:26:51 +08:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.unsubscribe) {
|
|
|
|
this.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribe() {
|
|
|
|
const { store } = this.props;
|
|
|
|
this.unsubscribe = store.subscribe(() => {
|
2016-11-22 13:42:45 +08:00
|
|
|
this.setCheckState(this.props);
|
2016-11-11 15:26:51 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
checkSelection(data, type, byDefaultChecked) {
|
|
|
|
const { store, getCheckboxPropsByItem, getRecordKey } = this.props;
|
|
|
|
// type should be 'every' | 'some'
|
|
|
|
if (type === 'every' || type === 'some') {
|
|
|
|
return (
|
|
|
|
byDefaultChecked
|
2016-12-16 17:53:08 +08:00
|
|
|
? data[type]((item, i) => getCheckboxPropsByItem(item, i).defaultChecked)
|
2016-11-11 15:26:51 +08:00
|
|
|
: data[type]((item, i) =>
|
|
|
|
store.getState().selectedRowKeys.indexOf(getRecordKey(item, i)) >= 0)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-22 13:42:45 +08:00
|
|
|
setCheckState(props) {
|
|
|
|
const checked = this.getCheckState(props);
|
|
|
|
const indeterminate = this.getIndeterminateState(props);
|
|
|
|
if (checked !== this.state.checked) {
|
|
|
|
this.setState({ checked });
|
|
|
|
}
|
|
|
|
if (indeterminate !== this.state.indeterminate) {
|
|
|
|
this.setState({ indeterminate });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getCheckState(props) {
|
|
|
|
const { store, data } = props;
|
2016-11-11 15:26:51 +08:00
|
|
|
let checked;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-11-22 13:42:45 +08:00
|
|
|
getIndeterminateState(props) {
|
|
|
|
const { store, data } = props;
|
2016-11-11 15:26:51 +08:00
|
|
|
let indeterminate;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { disabled, onChange } = this.props;
|
|
|
|
const { checked, indeterminate } = this.state;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Checkbox
|
|
|
|
checked={checked}
|
|
|
|
indeterminate={indeterminate}
|
|
|
|
disabled={disabled}
|
|
|
|
onChange={onChange}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|