2019-01-12 03:33:27 +00:00
|
|
|
import PropTypes from '../_util/vue-types';
|
2020-10-23 06:29:39 +00:00
|
|
|
import { defineComponent } from 'vue';
|
2019-01-12 03:33:27 +00:00
|
|
|
import { Store } from './createStore';
|
2020-07-23 10:34:24 +00:00
|
|
|
import { getSlot } from '../_util/props-util';
|
2020-10-23 06:29:39 +00:00
|
|
|
import omit from 'omit.js';
|
2018-03-29 14:08:04 +00:00
|
|
|
|
|
|
|
const BodyRowProps = {
|
|
|
|
store: Store,
|
2019-01-12 03:33:27 +00:00
|
|
|
rowKey: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
|
2018-03-29 14:08:04 +00:00
|
|
|
prefixCls: PropTypes.string,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-29 14:08:04 +00:00
|
|
|
|
2020-03-07 11:45:13 +00:00
|
|
|
export default function createBodyRow(Component = 'tr') {
|
2020-10-23 06:29:39 +00:00
|
|
|
const BodyRow = defineComponent({
|
2018-03-29 14:08:04 +00:00
|
|
|
name: 'BodyRow',
|
2020-07-23 10:34:24 +00:00
|
|
|
inheritAttrs: false,
|
2018-03-29 14:08:04 +00:00
|
|
|
props: BodyRowProps,
|
2020-11-07 07:37:17 +00:00
|
|
|
setup() {
|
|
|
|
return {
|
|
|
|
unsubscribe: null,
|
|
|
|
};
|
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
data() {
|
|
|
|
const { selectedRowKeys } = this.store.getState();
|
2018-03-29 14:08:04 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
selected: selectedRowKeys.indexOf(this.rowKey) >= 0,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-29 14:08:04 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
|
|
|
this.subscribe();
|
2018-03-29 14:08:04 +00:00
|
|
|
},
|
|
|
|
|
2020-06-11 08:13:09 +00:00
|
|
|
beforeUnmount() {
|
2018-03-29 14:08:04 +00:00
|
|
|
if (this.unsubscribe) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.unsubscribe();
|
2018-03-29 14:08:04 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
subscribe() {
|
|
|
|
const { store, rowKey } = this;
|
2018-03-29 14:08:04 +00:00
|
|
|
this.unsubscribe = store.subscribe(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
const { selectedRowKeys } = this.store.getState();
|
|
|
|
const selected = selectedRowKeys.indexOf(rowKey) >= 0;
|
2018-03-29 14:08:04 +00:00
|
|
|
if (selected !== this.selected) {
|
2019-01-12 03:33:27 +00:00
|
|
|
this.selected = selected;
|
2018-03-29 14:08:04 +00:00
|
|
|
}
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
2018-03-29 14:08:04 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2020-10-23 06:29:39 +00:00
|
|
|
const rowProps = omit({ ...this.$props, ...this.$attrs }, [
|
2020-07-23 10:34:24 +00:00
|
|
|
'prefixCls',
|
|
|
|
'rowKey',
|
|
|
|
'store',
|
|
|
|
'class',
|
|
|
|
]);
|
2018-03-29 14:08:04 +00:00
|
|
|
const className = {
|
2018-03-31 13:11:02 +00:00
|
|
|
[`${this.prefixCls}-row-selected`]: this.selected,
|
2020-10-23 06:29:39 +00:00
|
|
|
[this.$attrs.class as string]: !!this.$attrs.class,
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-03-29 14:08:04 +00:00
|
|
|
|
|
|
|
return (
|
2020-07-23 10:34:24 +00:00
|
|
|
<Component class={className} {...rowProps}>
|
|
|
|
{getSlot(this)}
|
2018-03-29 14:08:04 +00:00
|
|
|
</Component>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-03-29 14:08:04 +00:00
|
|
|
},
|
2020-10-23 06:29:39 +00:00
|
|
|
});
|
2018-03-29 14:08:04 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
return BodyRow;
|
2018-03-29 14:08:04 +00:00
|
|
|
}
|