ant-design-vue/components/vc-table/src/TableCell.jsx

111 lines
2.6 KiB
React
Raw Normal View History

2018-03-24 14:02:24 +00:00
import PropTypes from '../../_util/vue-types'
import get from 'lodash/get'
2018-03-25 14:23:04 +00:00
import { isValidElement } from '../../_util/props-util'
2018-03-24 14:02:24 +00:00
export default {
name: 'TableCell',
props: {
record: PropTypes.object,
prefixCls: PropTypes.string,
index: PropTypes.number,
indent: PropTypes.number,
indentSize: PropTypes.number,
column: PropTypes.object,
2018-03-25 10:07:04 +00:00
expandIcon: PropTypes.any,
2018-03-24 14:02:24 +00:00
component: PropTypes.any,
},
methods: {
isInvalidRenderCellText (text) {
2018-03-25 10:07:04 +00:00
// debugger
2018-03-25 14:23:04 +00:00
return text && !isValidElement(text) &&
2018-03-24 14:02:24 +00:00
Object.prototype.toString.call(text) === '[object Object]'
},
handleClick (e) {
const { record, column: { onCellClick }} = this
if (onCellClick) {
onCellClick(record, e)
}
},
},
2018-03-25 14:23:04 +00:00
render (h) {
2018-03-24 14:02:24 +00:00
const {
record,
indentSize,
prefixCls,
indent,
index,
expandIcon,
column,
component: BodyCell,
} = this
2018-03-25 14:23:04 +00:00
const { dataIndex, render, className = '' } = column
const cls = column.class || className
2018-03-24 14:02:24 +00:00
// We should return undefined if no dataIndex is specified, but in order to
// be compatible with object-path's behavior, we return the record object instead.
let text
if (typeof dataIndex === 'number') {
text = get(record, dataIndex)
} else if (!dataIndex || dataIndex.length === 0) {
text = record
} else {
text = get(record, dataIndex)
}
2018-03-25 14:23:04 +00:00
const tdProps = {
props: {},
attrs: {},
class: cls,
on: {
click: this.handleClick,
},
}
2018-03-24 14:02:24 +00:00
let colSpan
let rowSpan
if (render) {
2018-03-25 14:23:04 +00:00
text = render(h, text, record, index)
2018-03-24 14:02:24 +00:00
if (this.isInvalidRenderCellText(text)) {
2018-03-25 14:23:04 +00:00
tdProps.attrs = text.attrs || text.props || {}
colSpan = tdProps.attrs.colSpan
rowSpan = tdProps.attrs.rowSpan
2018-03-24 14:02:24 +00:00
text = text.children
}
}
if (column.onCell) {
2018-03-25 14:23:04 +00:00
tdProps.attrs = { ...tdProps.attrs, ...column.onCell(record) }
2018-03-24 14:02:24 +00:00
}
// Fix https://github.com/ant-design/ant-design/issues/1202
if (this.isInvalidRenderCellText(text)) {
text = null
}
const indentText = expandIcon ? (
<span
style={{ paddingLeft: `${indentSize * indent}px` }}
class={`${prefixCls}-indent indent-level-${indent}`}
/>
) : null
if (rowSpan === 0 || colSpan === 0) {
return null
}
if (column.align) {
tdProps.style = { textAlign: column.align }
}
return (
<BodyCell
{...tdProps}
>
{indentText}
{expandIcon}
{text}
</BodyCell>
)
},
}