2020-10-23 06:29:39 +00:00
|
|
|
import { defineComponent } from 'vue';
|
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-07-23 10:34:24 +00:00
|
|
|
import { getOptionProps } from '../_util/props-util';
|
2018-03-30 14:00:55 +00:00
|
|
|
|
2020-10-23 06:29:39 +00:00
|
|
|
export default defineComponent({
|
2018-03-30 14:00:55 +00:00
|
|
|
name: 'SelectionBox',
|
2019-02-01 09:23:00 +00:00
|
|
|
mixins: [BaseMixin],
|
2020-07-23 10:34:24 +00:00
|
|
|
inheritAttrs: false,
|
2018-03-30 14:00:55 +00:00
|
|
|
props: SelectionBoxProps,
|
|
|
|
|
2020-10-23 06:29:39 +00:00
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
unsubscribe: null,
|
|
|
|
};
|
|
|
|
},
|
2020-11-07 07:37:17 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
checked: false,
|
|
|
|
};
|
|
|
|
},
|
2020-10-23 06:29:39 +00:00
|
|
|
|
|
|
|
created() {
|
|
|
|
this.checked = this.getCheckState(this.$props);
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
|
|
|
this.subscribe();
|
2018-03-30 14:00:55 +00:00
|
|
|
},
|
|
|
|
|
2020-06-11 08:13:09 +00:00
|
|
|
beforeUnmount() {
|
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: {
|
2020-10-23 06:29:39 +00:00
|
|
|
getCheckState(props): boolean {
|
2019-01-12 03:33:27 +00:00
|
|
|
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
|
|
|
},
|
2020-03-07 11:45:13 +00:00
|
|
|
subscribe() {
|
|
|
|
const { store } = this;
|
|
|
|
this.unsubscribe = store.subscribe(() => {
|
|
|
|
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
|
|
|
render() {
|
2020-10-23 06:29:39 +00:00
|
|
|
const { type, rowIndex, ...rest } = { ...getOptionProps(this), ...this.$attrs } as any;
|
2020-01-18 08:14:42 +00:00
|
|
|
const { checked } = this;
|
2018-03-30 14:00:55 +00:00
|
|
|
const checkboxProps = {
|
2020-07-23 10:34:24 +00:00
|
|
|
checked,
|
|
|
|
...rest,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-30 14:00:55 +00:00
|
|
|
if (type === 'radio') {
|
2020-07-23 10:34:24 +00:00
|
|
|
checkboxProps.value = rowIndex;
|
2019-01-12 03:33:27 +00:00
|
|
|
return <Radio {...checkboxProps} />;
|
2018-03-30 14:00:55 +00:00
|
|
|
}
|
2020-03-07 11:45:13 +00:00
|
|
|
return <Checkbox {...checkboxProps} />;
|
2018-03-30 14:00:55 +00:00
|
|
|
},
|
2020-10-23 06:29:39 +00:00
|
|
|
});
|