mirror of
https://gitee.com/ant-design-vue/ant-design-vue.git
synced 2024-12-16 01:41:15 +08:00
61 lines
1.3 KiB
Vue
61 lines
1.3 KiB
Vue
import PropTypes from '../_util/vue-types';
|
|
|
|
import { Store } from './createStore';
|
|
import { getListeners } from '../_util/props-util';
|
|
|
|
const BodyRowProps = {
|
|
store: Store,
|
|
rowKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
|
prefixCls: PropTypes.string,
|
|
};
|
|
|
|
export default function createBodyRow(Component = 'tr') {
|
|
const BodyRow = {
|
|
name: 'BodyRow',
|
|
props: BodyRowProps,
|
|
data() {
|
|
const { selectedRowKeys } = this.store.getState();
|
|
|
|
return {
|
|
selected: selectedRowKeys.indexOf(this.rowKey) >= 0,
|
|
};
|
|
},
|
|
|
|
mounted() {
|
|
this.subscribe();
|
|
},
|
|
|
|
beforeUnmount() {
|
|
if (this.unsubscribe) {
|
|
this.unsubscribe();
|
|
}
|
|
},
|
|
methods: {
|
|
subscribe() {
|
|
const { store, rowKey } = this;
|
|
this.unsubscribe = store.subscribe(() => {
|
|
const { selectedRowKeys } = this.store.getState();
|
|
const selected = selectedRowKeys.indexOf(rowKey) >= 0;
|
|
if (selected !== this.selected) {
|
|
this.selected = selected;
|
|
}
|
|
});
|
|
},
|
|
},
|
|
|
|
render() {
|
|
const className = {
|
|
[`${this.prefixCls}-row-selected`]: this.selected,
|
|
};
|
|
|
|
return (
|
|
<Component class={className} {...{ on: getListeners(this) }}>
|
|
{this.$slots.default}
|
|
</Component>
|
|
);
|
|
},
|
|
};
|
|
|
|
return BodyRow;
|
|
}
|