ant-design-vue/components/table/createBodyRow.jsx

63 lines
1.3 KiB
Vue
Raw Normal View History

2018-03-29 14:08:04 +00:00
import PropTypes from '../_util/vue-types'
import { Store } from './createStore'
const BodyRowProps = {
store: Store,
2018-04-01 14:33:01 +00:00
rowKey: PropTypes.oneOfType([
PropTypes.string,
PropTypes.number,
]),
2018-03-29 14:08:04 +00:00
prefixCls: PropTypes.string,
}
export default function createTableRow (Component = 'tr') {
const BodyRow = {
name: 'BodyRow',
props: BodyRowProps,
data () {
const { selectedRowKeys } = this.store.getState()
return {
selected: selectedRowKeys.indexOf(this.rowKey) >= 0,
}
},
mounted () {
this.subscribe()
},
beforeDestroy () {
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 = {
2018-03-31 13:11:02 +00:00
[`${this.prefixCls}-row-selected`]: this.selected,
2018-03-29 14:08:04 +00:00
}
return (
2018-05-10 11:15:43 +00:00
<Component class={className} {...{ on: this.$listeners }}>
2018-03-29 14:08:04 +00:00
{this.$slots.default}
</Component>
)
},
}
return BodyRow
}