fix vc-table

pull/165/head
tjz 2018-03-25 22:23:04 +08:00
parent 41f4d3b035
commit e0da134066
7 changed files with 103 additions and 36 deletions

View File

@ -166,10 +166,6 @@ export function getComponentName (opts) {
return opts && (opts.Ctor.options.name || opts.tag) return opts && (opts.Ctor.options.name || opts.tag)
} }
export function isValidElement (ele) {
return !!ele.tag
}
export function isEmptyElement (ele) { export function isEmptyElement (ele) {
return !(ele.tag || ele.text.trim() !== '') return !(ele.tag || ele.text.trim() !== '')
} }
@ -206,6 +202,11 @@ export function mergeProps () {
return props return props
} }
function isValidElement (element) {
const name = element.constructor.name
return element.tag && (name === 'VNode' || name === 'VueComponent')
}
export { export {
hasProp, hasProp,
filterProps, filterProps,
@ -219,5 +220,6 @@ export {
getValueByProp, getValueByProp,
parseStyleText, parseStyleText,
initDefaultProps, initDefaultProps,
isValidElement,
} }
export default hasProp export default hasProp

View File

@ -0,0 +1,45 @@
/* eslint-disable no-console,func-names,react/no-multi-comp */
import Table from '../index'
import '../assets/index.less'
const data = [
{ a: '123', key: '1' },
{ a: 'cdd', b: 'edd', key: '2' },
{ a: '1333', c: 'eee', d: 2, key: '3' },
]
export default {
render () {
const columns = [
{ title: 'title1', dataIndex: 'a',
className: 'a',
key: 'a', width: 100 },
{ id: '123', title: 'title2', dataIndex: 'b',
className: 'b',
key: 'b', width: 100 },
{ title: 'title3', dataIndex: 'c',
className: 'c',
key: 'c', width: 200 },
{
title: 'Operations', dataIndex: '',
className: 'd',
key: 'd', render (h) {
return <a href='#'>Operations</a>
},
},
]
return (
<div>
<h2>rowClassName and className</h2>
<Table
columns={columns}
rowClassName={(record, i) => `row-${i}`}
expandedRowRender={record => <p>extra: {record.a}</p>}
expandedRowClassName={(record, i) => `ex-row-${i}`}
data={data}
class='table'
/>
</div>
)
},
}

View File

@ -112,6 +112,7 @@ const ExpandableRow = {
renderExpandIcon: this.renderExpandIcon, renderExpandIcon: this.renderExpandIcon,
renderExpandIconCell: this.renderExpandIconCell, renderExpandIconCell: this.renderExpandIconCell,
}, },
on: { on: {
rowClick: this.handleRowClick, rowClick: this.handleRowClick,
}, },

View File

@ -154,12 +154,11 @@ const ExpandableTable = {
cell: 'td', cell: 'td',
}, },
} }
return ( return (
<TableRow <TableRow
key={rowKey} key={rowKey}
columns={columns} columns={columns}
className={className} class={className}
rowKey={rowKey} rowKey={rowKey}
ancestorKeys={ancestorKeys} ancestorKeys={ancestorKeys}
prefixCls={`${prefixCls}-expanded-row`} prefixCls={`${prefixCls}-expanded-row`}
@ -168,6 +167,7 @@ const ExpandableTable = {
fixed={fixed} fixed={fixed}
components={components} components={components}
expandedRow expandedRow
hasExpandIcon={() => {}}
/> />
) )
}, },

View File

@ -1,5 +1,6 @@
import PropTypes from '../../_util/vue-types' import PropTypes from '../../_util/vue-types'
import get from 'lodash/get' import get from 'lodash/get'
import { isValidElement } from '../../_util/props-util'
export default { export default {
name: 'TableCell', name: 'TableCell',
@ -16,7 +17,7 @@ export default {
methods: { methods: {
isInvalidRenderCellText (text) { isInvalidRenderCellText (text) {
// debugger // debugger
return text && return text && !isValidElement(text) &&
Object.prototype.toString.call(text) === '[object Object]' Object.prototype.toString.call(text) === '[object Object]'
}, },
@ -28,7 +29,7 @@ export default {
}, },
}, },
render () { render (h) {
const { const {
record, record,
indentSize, indentSize,
@ -39,8 +40,8 @@ export default {
column, column,
component: BodyCell, component: BodyCell,
} = this } = this
const { dataIndex, render } = column const { dataIndex, render, className = '' } = column
const cls = column.class || className
// We should return undefined if no dataIndex is specified, but in order to // 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. // be compatible with object-path's behavior, we return the record object instead.
let text let text
@ -51,22 +52,29 @@ export default {
} else { } else {
text = get(record, dataIndex) text = get(record, dataIndex)
} }
let tdProps = {} const tdProps = {
props: {},
attrs: {},
class: cls,
on: {
click: this.handleClick,
},
}
let colSpan let colSpan
let rowSpan let rowSpan
if (render) { if (render) {
text = render(text, record, index) text = render(h, text, record, index)
if (this.isInvalidRenderCellText(text)) { if (this.isInvalidRenderCellText(text)) {
tdProps = text.props || tdProps tdProps.attrs = text.attrs || text.props || {}
colSpan = tdProps.colSpan colSpan = tdProps.attrs.colSpan
rowSpan = tdProps.rowSpan rowSpan = tdProps.attrs.rowSpan
text = text.children text = text.children
} }
} }
if (column.onCell) { if (column.onCell) {
tdProps = { ...tdProps, ...column.onCell(record) } tdProps.attrs = { ...tdProps.attrs, ...column.onCell(record) }
} }
// Fix https://github.com/ant-design/ant-design/issues/1202 // Fix https://github.com/ant-design/ant-design/issues/1202
@ -88,11 +96,9 @@ export default {
if (column.align) { if (column.align) {
tdProps.style = { textAlign: column.align } tdProps.style = { textAlign: column.align }
} }
console.log('tdProps', tdProps)
return ( return (
<BodyCell <BodyCell
onClick={this.handleClick}
{...tdProps} {...tdProps}
> >
{indentText} {indentText}

View File

@ -1,5 +1,6 @@
import PropTypes from '../../_util/vue-types' import PropTypes from '../../_util/vue-types'
import { connect } from '../../_util/store' import { connect } from '../../_util/store'
import { mergeProps } from '../../_util/props-util'
const TableHeaderRow = { const TableHeaderRow = {
props: { props: {
@ -25,17 +26,23 @@ const TableHeaderRow = {
<HeaderRow {...rowProps} style={style}> <HeaderRow {...rowProps} style={style}>
{row.map((cell, i) => { {row.map((cell, i) => {
const { column, children, className, ...cellProps } = cell const { column, children, className, ...cellProps } = cell
const cls = cell.class || className
const customProps = column.onHeaderCell ? column.onHeaderCell(column) : {} const customProps = column.onHeaderCell ? column.onHeaderCell(column) : {}
if (column.align) { if (column.align) {
cellProps.style = { textAlign: column.align } cellProps.style = { textAlign: column.align }
} }
const headerCellProps = mergeProps({
attrs: {
...cellProps,
},
class: cls,
}, {
...customProps,
key: column.key || column.dataIndex || i,
})
return ( return (
<HeaderCell <HeaderCell
{...cellProps} {...headerCellProps}
class={className}
{...customProps}
key={column.key || column.dataIndex || i}
> >
{children} {children}
</HeaderCell> </HeaderCell>

View File

@ -2,9 +2,9 @@ import PropTypes from '../../_util/vue-types'
import { connect } from '../../_util/store' import { connect } from '../../_util/store'
import TableCell from './TableCell' import TableCell from './TableCell'
import { warningOnce } from './utils' import { warningOnce } from './utils'
import { initDefaultProps } from '../../_util/props-util' import { initDefaultProps, mergeProps } from '../../_util/props-util'
import BaseMixin from '../../_util/BaseMixin' import BaseMixin from '../../_util/BaseMixin'
function noop () {}
const TableRow = { const TableRow = {
name: 'TableRow', name: 'TableRow',
mixins: [BaseMixin], mixins: [BaseMixin],
@ -71,7 +71,11 @@ const TableRow = {
} }
}, },
watch: { watch: {
visible (val) {
if (val) {
this.shouldRender = true
}
},
}, },
componentWillReceiveProps (nextProps) { componentWillReceiveProps (nextProps) {
@ -193,7 +197,7 @@ const TableRow = {
renderExpandIconCell, renderExpandIconCell,
$listeners, $listeners,
} = this } = this
const { row: onRow } = $listeners const { row: onRow = noop } = $listeners
const BodyRow = components.body.row const BodyRow = components.body.row
const BodyCell = components.body.cell const BodyCell = components.body.cell
@ -242,17 +246,19 @@ const TableRow = {
} }
style = { ...style, ...customStyle } style = { ...style, ...customStyle }
console.log('rowProps', rowProps) const bodyRowProps = mergeProps({
on: {
click: this.onRowClick,
dblclick: this.onRowDoubleClick,
mouseenter: this.onMouseEnter,
mouseleave: this.onMouseLeave,
contextmenu: this.onContextMenu,
},
class: rowClassName,
}, { ...rowProps, style })
return ( return (
<BodyRow <BodyRow
onClick={this.onRowClick} {...bodyRowProps}
onDoubleclick={this.onRowDoubleClick}
onMouseenter={this.onMouseEnter}
onMouseleave={this.onMouseLeave}
onContextmenu={this.onContextMenu}
class={rowClassName}
{...rowProps}
style={style}
> >
{cells} {cells}
</BodyRow> </BodyRow>