2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-11-11 15:26:51 +08:00
|
|
|
import Checkbox from '../checkbox';
|
|
|
|
import Radio from '../radio';
|
2017-11-21 13:48:37 +08:00
|
|
|
import { SelectionBoxProps, SelectionBoxState } from './interface';
|
2016-11-11 15:26:51 +08:00
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
export default class SelectionBox extends React.Component<SelectionBoxProps, SelectionBoxState> {
|
2016-11-11 15:26:51 +08:00
|
|
|
unsubscribe: () => void;
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
constructor(props: SelectionBoxProps) {
|
2016-11-11 15:26:51 +08:00
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
checked: this.getCheckState(props),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.subscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.unsubscribe) {
|
|
|
|
this.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
subscribe() {
|
|
|
|
const { store } = this.props;
|
|
|
|
this.unsubscribe = store.subscribe(() => {
|
|
|
|
const checked = this.getCheckState(this.props);
|
2016-11-28 13:35:05 +08:00
|
|
|
this.setState({ checked });
|
2016-11-11 15:26:51 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-21 13:48:37 +08:00
|
|
|
getCheckState(props: SelectionBoxProps) {
|
2016-11-11 15:26:51 +08:00
|
|
|
const { store, defaultSelection, rowIndex } = props;
|
|
|
|
let 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-01-31 15:07:53 +08:00
|
|
|
const { type, rowIndex, ...rest } = this.props;
|
2016-11-11 15:26:51 +08:00
|
|
|
const { checked } = this.state;
|
|
|
|
|
|
|
|
if (type === 'radio') {
|
|
|
|
return (
|
|
|
|
<Radio
|
2018-01-31 15:07:53 +08:00
|
|
|
checked={checked}
|
2016-11-11 15:26:51 +08:00
|
|
|
value={rowIndex}
|
2018-01-31 15:07:53 +08:00
|
|
|
{...rest}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<Checkbox
|
2016-11-11 15:26:51 +08:00
|
|
|
checked={checked}
|
2018-01-31 15:07:53 +08:00
|
|
|
{...rest}
|
2016-11-11 15:26:51 +08:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|