ant-design-vue/components/table/SelectionBox.jsx

67 lines
1.6 KiB
Vue
Raw Normal View History

2019-01-12 03:33:27 +00:00
import Checkbox from '../checkbox';
import Radio from '../radio';
import { SelectionBoxProps } from './interface';
import BaseMixin from '../_util/BaseMixin';
2020-01-18 08:14:42 +00:00
import { getOptionProps, getListeners } from '../_util/props-util';
2018-03-30 14:00:55 +00:00
export default {
name: 'SelectionBox',
2019-02-01 09:23:00 +00:00
mixins: [BaseMixin],
2018-03-30 14:00:55 +00:00
props: SelectionBoxProps,
2019-01-12 03:33:27 +00:00
data() {
2018-03-30 14:00:55 +00:00
return {
2018-06-08 14:30:25 +00:00
checked: this.getCheckState(this.$props),
2019-01-12 03:33:27 +00:00
};
2018-03-30 14:00:55 +00:00
},
2019-01-12 03:33:27 +00:00
mounted() {
this.subscribe();
2018-03-30 14:00:55 +00:00
},
2019-01-12 03:33:27 +00:00
beforeDestroy() {
2018-03-30 14:00:55 +00:00
if (this.unsubscribe) {
2019-01-12 03:33:27 +00:00
this.unsubscribe();
2018-03-30 14:00:55 +00:00
}
},
methods: {
2019-01-12 03:33:27 +00:00
subscribe() {
const { store } = this;
2018-03-30 14:00:55 +00:00
this.unsubscribe = store.subscribe(() => {
2019-01-12 03:33:27 +00:00
const checked = this.getCheckState(this.$props);
this.setState({ checked });
});
2018-03-30 14:00:55 +00:00
},
2019-01-12 03:33:27 +00:00
getCheckState(props) {
const { store, defaultSelection, rowIndex } = props;
let checked = false;
2018-03-30 14:00:55 +00:00
if (store.getState().selectionDirty) {
2019-01-12 03:33:27 +00:00
checked = store.getState().selectedRowKeys.indexOf(rowIndex) >= 0;
2018-03-30 14:00:55 +00:00
} else {
2019-01-12 03:33:27 +00:00
checked =
store.getState().selectedRowKeys.indexOf(rowIndex) >= 0 ||
defaultSelection.indexOf(rowIndex) >= 0;
2018-03-30 14:00:55 +00:00
}
2019-01-12 03:33:27 +00:00
return checked;
2018-03-30 14:00:55 +00:00
},
},
2019-01-12 03:33:27 +00:00
render() {
const { type, rowIndex, ...rest } = getOptionProps(this);
2020-01-18 08:14:42 +00:00
const { checked } = this;
2018-03-30 14:00:55 +00:00
const checkboxProps = {
props: {
checked,
...rest,
},
2020-01-18 08:14:42 +00:00
on: getListeners(this),
2019-01-12 03:33:27 +00:00
};
2018-03-30 14:00:55 +00:00
if (type === 'radio') {
2019-01-12 03:33:27 +00:00
checkboxProps.props.value = rowIndex;
return <Radio {...checkboxProps} />;
2018-03-30 14:00:55 +00:00
} else {
2019-01-12 03:33:27 +00:00
return <Checkbox {...checkboxProps} />;
2018-03-30 14:00:55 +00:00
}
},
2019-01-12 03:33:27 +00:00
};