2019-01-12 11:33:27 +08:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2018-03-29 22:08:04 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
import { Store } from './createStore';
|
2020-01-18 16:14:42 +08:00
|
|
|
import { getListeners } from '../_util/props-util';
|
2018-03-29 22:08:04 +08:00
|
|
|
|
|
|
|
const BodyRowProps = {
|
|
|
|
store: Store,
|
2019-01-12 11:33:27 +08:00
|
|
|
rowKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
2018-03-29 22:08:04 +08:00
|
|
|
prefixCls: PropTypes.string,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-29 22:08:04 +08:00
|
|
|
|
2020-03-07 19:45:13 +08:00
|
|
|
export default function createBodyRow(Component = 'tr') {
|
2018-03-29 22:08:04 +08:00
|
|
|
const BodyRow = {
|
|
|
|
name: 'BodyRow',
|
|
|
|
props: BodyRowProps,
|
2019-01-12 11:33:27 +08:00
|
|
|
data() {
|
|
|
|
const { selectedRowKeys } = this.store.getState();
|
2018-03-29 22:08:04 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
selected: selectedRowKeys.indexOf(this.rowKey) >= 0,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-29 22:08:04 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
mounted() {
|
|
|
|
this.subscribe();
|
2018-03-29 22:08:04 +08:00
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
beforeDestroy() {
|
2018-03-29 22:08:04 +08:00
|
|
|
if (this.unsubscribe) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.unsubscribe();
|
2018-03-29 22:08:04 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 11:33:27 +08:00
|
|
|
subscribe() {
|
|
|
|
const { store, rowKey } = this;
|
2018-03-29 22:08:04 +08:00
|
|
|
this.unsubscribe = store.subscribe(() => {
|
2019-01-12 11:33:27 +08:00
|
|
|
const { selectedRowKeys } = this.store.getState();
|
|
|
|
const selected = selectedRowKeys.indexOf(rowKey) >= 0;
|
2018-03-29 22:08:04 +08:00
|
|
|
if (selected !== this.selected) {
|
2019-01-12 11:33:27 +08:00
|
|
|
this.selected = selected;
|
2018-03-29 22:08:04 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-29 22:08:04 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
render() {
|
2018-03-29 22:08:04 +08:00
|
|
|
const className = {
|
2018-03-31 21:11:02 +08:00
|
|
|
[`${this.prefixCls}-row-selected`]: this.selected,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-29 22:08:04 +08:00
|
|
|
|
|
|
|
return (
|
2020-01-18 16:14:42 +08:00
|
|
|
<Component class={className} {...{ on: getListeners(this) }}>
|
2018-03-29 22:08:04 +08:00
|
|
|
{this.$slots.default}
|
|
|
|
</Component>
|
2019-01-12 11:33:27 +08:00
|
|
|
);
|
2018-03-29 22:08:04 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-29 22:08:04 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
return BodyRow;
|
2018-03-29 22:08:04 +08:00
|
|
|
}
|