You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ant-design-vue/components/table/SelectionBox.jsx

68 lines
1.6 KiB

import Checkbox from '../checkbox';
import Radio from '../radio';
import { SelectionBoxProps } from './interface';
import BaseMixin from '../_util/BaseMixin';
import { getOptionProps } from '../_util/props-util';
7 years ago
export default {
mixins: [BaseMixin],
name: 'SelectionBox',
props: SelectionBoxProps,
data() {
7 years ago
return {
checked: this.getCheckState(this.$props),
};
7 years ago
},
mounted() {
this.subscribe();
7 years ago
},
beforeDestroy() {
7 years ago
if (this.unsubscribe) {
this.unsubscribe();
7 years ago
}
},
methods: {
subscribe() {
const { store } = this;
7 years ago
this.unsubscribe = store.subscribe(() => {
const checked = this.getCheckState(this.$props);
this.setState({ checked });
});
7 years ago
},
getCheckState(props) {
const { store, defaultSelection, rowIndex } = props;
let checked = false;
7 years ago
if (store.getState().selectionDirty) {
checked = store.getState().selectedRowKeys.indexOf(rowIndex) >= 0;
7 years ago
} else {
checked =
store.getState().selectedRowKeys.indexOf(rowIndex) >= 0 ||
defaultSelection.indexOf(rowIndex) >= 0;
7 years ago
}
return checked;
7 years ago
},
},
render() {
const { type, rowIndex, ...rest } = getOptionProps(this);
const { checked, $attrs, $listeners } = this;
7 years ago
const checkboxProps = {
props: {
checked,
...rest,
},
attrs: $attrs,
on: $listeners,
};
7 years ago
if (type === 'radio') {
checkboxProps.props.value = rowIndex;
return <Radio {...checkboxProps} />;
7 years ago
} else {
return <Checkbox {...checkboxProps} />;
7 years ago
}
},
};