diff --git a/components/table/demo/basic.md b/components/table/demo/basic.md index 325d0a2df..332ec9843 100644 --- a/components/table/demo/basic.md +++ b/components/table/demo/basic.md @@ -10,13 +10,29 @@ Simple table with actions. ```html +``` diff --git a/components/table/index.jsx b/components/table/index.jsx index ce2a96d01..3bb03589c 100644 --- a/components/table/index.jsx +++ b/components/table/index.jsx @@ -1,5 +1,80 @@ -import Table from './Table' +import T from './Table' -export * from './interface' +import { getOptionProps, getKey, getClass, + getStyle, getEvents, getSlotOptions, camelize, getSlots, +} from '../_util/props-util' +const Table = { + name: 'Table', + Column: T.Column, + ColumnGroup: T.ColumnGroup, + props: T.props, + methods: { + normalize (elements = []) { + const columns = [] + elements.forEach(element => { + if (!element.tag) { + return + } + const key = getKey(element) + const style = getStyle(element) + const cls = getClass(element) + const props = getOptionProps(element) + const events = getEvents(element) + const listeners = {} + Object.keys(events).forEach(e => { + const k = `on_${e}` + listeners[camelize(k)] = events[e] + }) + const { default: children, title } = getSlots(element) + const column = { title, ...props, style, class: cls, ...listeners } + if (key) { + column.key = key + } + if (getSlotOptions(element).__ANT_TABLE_COLUMN_GROUP) { + column.children = this.normalize(children) + } else { + const customRender = element.data && element.data.scopedSlots && element.data.scopedSlots.default + column.customRender = column.customRender || customRender + } + columns.push(column) + }) + return columns + }, + updateColumns (cols = []) { + const columns = [] + const { $slots, $scopedSlots } = this + cols.forEach(col => { + const { slotTitle, slotScopeName, ...restProps } = col + const column = { + ...restProps, + title: col.title || $slots[slotTitle], + } + if (slotScopeName && $scopedSlots[slotScopeName]) { + column.customRender = column.customRender || $scopedSlots[slotScopeName] + } + if (col.children) { + column.children = this.updateColumns(column.children) + } + columns.push(column) + }) + return columns + }, + }, + render () { + const { $listeners, $slots, normalize } = this + const props = getOptionProps(this) + const columns = props.columns ? this.updateColumns(props.columns) : normalize($slots.default) + const tProps = { + props: { + ...props, + columns, + }, + on: $listeners, + } + return ( + + ) + }, +} export default Table diff --git a/components/table/interface.js b/components/table/interface.js index 55693f9ae..1d21f4411 100644 --- a/components/table/interface.js +++ b/components/table/interface.js @@ -17,7 +17,7 @@ export const ColumnProps = { title: PropTypes.any, // key?: React.Key; dataIndex: PropTypes.string, - render: PropTypes.func, + customRender: PropTypes.func, filters: PropTypes.arrayOf(ColumnFilterItem), // onFilter: (value: any, record: T) => PropTypes.bool, filterMultiple: PropTypes.bool, diff --git a/components/vc-table/demo/className.js b/components/vc-table/demo/className.js index 35336108e..220c1fd8d 100644 --- a/components/vc-table/demo/className.js +++ b/components/vc-table/demo/className.js @@ -22,7 +22,7 @@ export default { { title: 'Operations', dataIndex: '', className: 'd', - key: 'd', render () { + key: 'd', customRender () { return Operations }, }, diff --git a/components/vc-table/demo/colspan-rowspan.js b/components/vc-table/demo/colspan-rowspan.js index eb5bcf82b..51d56b6ce 100644 --- a/components/vc-table/demo/colspan-rowspan.js +++ b/components/vc-table/demo/colspan-rowspan.js @@ -3,7 +3,7 @@ import Table from '../index' import '../assets/index.less' const columns = [ - { title: '手机号', dataIndex: 'a', colSpan: 2, width: 100, key: 'a', render (h, o, row, index) { + { title: '手机号', dataIndex: 'a', colSpan: 2, width: 100, key: 'a', customRender (o, row, index) { const obj = { children: o, props: {}, @@ -22,7 +22,7 @@ const columns = [ } return obj } }, - { title: '电话', dataIndex: 'b', colSpan: 0, width: 100, key: 'b', render (h, o, row, index) { + { title: '电话', dataIndex: 'b', colSpan: 0, width: 100, key: 'b', customRender (o, row, index) { const obj = { children: o, props: {}, @@ -33,7 +33,7 @@ const columns = [ } return obj } }, - { title: 'Name', dataIndex: 'c', width: 100, key: 'c', render (h, o, row, index) { + { title: 'Name', dataIndex: 'c', width: 100, key: 'c', customRender (o, row, index) { const obj = { children: o, props: {}, @@ -44,7 +44,7 @@ const columns = [ } return obj } }, - { title: 'Address', dataIndex: 'd', width: 200, key: 'd', render (h, o, row, index) { + { title: 'Address', dataIndex: 'd', width: 200, key: 'd', customRender (o, row, index) { const obj = { children: o, props: {}, @@ -58,7 +58,7 @@ const columns = [ return obj } }, - { title: 'Gender', dataIndex: 'e', width: 200, key: 'e', render (h, o, row, index) { + { title: 'Gender', dataIndex: 'e', width: 200, key: 'e', customRender (o, row, index) { const obj = { children: o, props: {}, @@ -70,7 +70,7 @@ const columns = [ } }, { title: 'Operations', dataIndex: '', key: 'f', - render (h, o, row, index) { + customRender (o, row, index) { if (index === 5) { return { props: { diff --git a/components/vc-table/demo/column-resize.js b/components/vc-table/demo/column-resize.js index 04fbf6026..2b2a5afff 100644 --- a/components/vc-table/demo/column-resize.js +++ b/components/vc-table/demo/column-resize.js @@ -24,7 +24,7 @@ export default { { id: '123', title: 'title2', dataIndex: 'b', key: 'b', width: 100 }, { title: 'title3', dataIndex: 'c', key: 'c', width: 200 }, { - title: 'Operations', dataIndex: '', key: 'd', render () { + title: 'Operations', dataIndex: '', key: 'd', customRender () { return Operations }, }, diff --git a/components/vc-table/demo/expandedRowRender.js b/components/vc-table/demo/expandedRowRender.js index 0e925518a..92df9e8d0 100644 --- a/components/vc-table/demo/expandedRowRender.js +++ b/components/vc-table/demo/expandedRowRender.js @@ -19,7 +19,7 @@ export default { { title: 'title 1', dataIndex: 'a', key: 'a', width: 100 }, { title: 'title 2', dataIndex: 'b', key: 'b', width: 100 }, { title: 'title 3', dataIndex: 'c', key: 'c', width: 200 }, - { title: 'Operation', dataIndex: '', key: 'x', render: this.renderAction }, + { title: 'Operation', dataIndex: '', key: 'x', customRender: this.renderAction }, ], } }, diff --git a/components/vc-table/demo/fixedColumns-auto-height.js b/components/vc-table/demo/fixedColumns-auto-height.js index 85056ebea..3015a74a2 100644 --- a/components/vc-table/demo/fixedColumns-auto-height.js +++ b/components/vc-table/demo/fixedColumns-auto-height.js @@ -2,22 +2,6 @@ import Table from '../index' import '../assets/index.less' -const columns = [ - { title: 'title1', dataIndex: 'a', key: 'a', width: 100, fixed: 'left' }, - { title: 'title2', dataIndex: 'b', key: 'b', width: 100, fixed: 'left' }, - { title: 'title3', dataIndex: 'c', key: 'c' }, - { title: 'title4', dataIndex: 'b', key: 'd' }, - { title: 'title5', dataIndex: 'b', key: 'e' }, - { title: 'title6', dataIndex: 'b', key: 'f', - render: (h) =>
我很高
}, - { title: 'title7', dataIndex: 'b', key: 'g' }, - { title: 'title8', dataIndex: 'b', key: 'h' }, - { title: 'title9', dataIndex: 'b', key: 'i' }, - { title: 'title10', dataIndex: 'b', key: 'j' }, - { title: 'title11', dataIndex: 'b', key: 'k' }, - { title: 'title12', dataIndex: 'b', key: 'l', width: 100, fixed: 'right' }, -] - const data = [ { a: '123', b: 'xxxxxxxx', d: 3, key: '1', title: 'hello' }, { a: 'cdd', b: 'edd12221', d: 3, key: '2', title: 'hello' }, @@ -36,6 +20,21 @@ export default { } }, render () { + const columns = [ + { title: 'title1', dataIndex: 'a', key: 'a', width: 100, fixed: 'left' }, + { title: 'title2', dataIndex: 'b', key: 'b', width: 100, fixed: 'left' }, + { title: 'title3', dataIndex: 'c', key: 'c' }, + { title: 'title4', dataIndex: 'b', key: 'd' }, + { title: 'title5', dataIndex: 'b', key: 'e' }, + { title: 'title6', dataIndex: 'b', key: 'f', + customRender: () =>
我很高
}, + { title: 'title7', dataIndex: 'b', key: 'g' }, + { title: 'title8', dataIndex: 'b', key: 'h' }, + { title: 'title9', dataIndex: 'b', key: 'i' }, + { title: 'title10', dataIndex: 'b', key: 'j' }, + { title: 'title11', dataIndex: 'b', key: 'k' }, + { title: 'title12', dataIndex: 'b', key: 'l', width: 100, fixed: 'right' }, + ] return (

Fixed columns

diff --git a/components/vc-table/demo/jsx.js b/components/vc-table/demo/jsx.js index c6f2b79b1..ef0cc39de 100644 --- a/components/vc-table/demo/jsx.js +++ b/components/vc-table/demo/jsx.js @@ -44,7 +44,7 @@ export default { key='d' // render={() => Operations} scopedSlots= { - { render: () => Operations } + { default: () => Operations } } > diff --git a/components/vc-table/demo/key.js b/components/vc-table/demo/key.js index 55fd7ac8c..1e45cb772 100644 --- a/components/vc-table/demo/key.js +++ b/components/vc-table/demo/key.js @@ -20,24 +20,24 @@ export default { this.remove(index) }, - checkbox (h, a) { + checkbox (a) { return }, - renderAction (h, o, row, index) { + renderAction (o, row, index) { return this.handleClick(index)}>Delete }, }, render () { const columns = [ - { title: 'title1', dataIndex: 'a', key: 'a', width: 100, render: this.checkbox }, + { title: 'title1', dataIndex: 'a', key: 'a', width: 100, customRender: this.checkbox }, { title: 'title2', dataIndex: 'b', key: 'b', width: 100 }, { title: 'title3', dataIndex: 'c', key: 'c', width: 200 }, - { title: 'Operations', dataIndex: '', key: 'x', render: this.renderAction }, + { title: 'Operations', dataIndex: '', key: 'x', customRender: this.renderAction }, ] return ( record.a} /> diff --git a/components/vc-table/demo/no-data.js b/components/vc-table/demo/no-data.js index d12d5a196..11457b5b4 100644 --- a/components/vc-table/demo/no-data.js +++ b/components/vc-table/demo/no-data.js @@ -11,7 +11,7 @@ export default { { id: '123', title: 'title2', dataIndex: 'b', key: 'b', width: 100 }, { title: 'title3', dataIndex: 'c', key: 'c', width: 200 }, { - title: 'Operations', dataIndex: '', key: 'd', render () { + title: 'Operations', dataIndex: '', key: 'd', customRender () { return Operations }, }, diff --git a/components/vc-table/demo/scrollY.js b/components/vc-table/demo/scrollY.js index 628b11264..f8b4463db 100644 --- a/components/vc-table/demo/scrollY.js +++ b/components/vc-table/demo/scrollY.js @@ -33,7 +33,7 @@ export default { title: {this.showBody ? '隐藏' : '显示'}体, key: 'x', width: 200, - render () { + customRender () { return Operations }, }, diff --git a/components/vc-table/demo/styled-components.js b/components/vc-table/demo/styled-components.js index 0b0ebef85..5b9721067 100644 --- a/components/vc-table/demo/styled-components.js +++ b/components/vc-table/demo/styled-components.js @@ -1,20 +1,6 @@ import Table from '../index' import '../assets/index.less' -const columns = [ - { title: 'title1', dataIndex: 'a', key: 'a', width: 100 }, - { id: '123', title: 'title2', dataIndex: 'b', key: 'b', width: 100 }, - { title: 'title3', dataIndex: 'c', key: 'c', width: 200 }, - { - title: 'Operations', - dataIndex: '', - key: 'd', - render (h) { - return Operations - }, - }, -] - const data = [ { a: '123', key: '1' }, { a: 'cdd', b: 'edd', key: '2' }, @@ -23,6 +9,19 @@ const data = [ export default { render () { + const columns = [ + { title: 'title1', dataIndex: 'a', key: 'a', width: 100 }, + { id: '123', title: 'title2', dataIndex: 'b', key: 'b', width: 100 }, + { title: 'title3', dataIndex: 'c', key: 'c', width: 200 }, + { + title: 'Operations', + dataIndex: '', + key: 'd', + customRender () { + return Operations + }, + }, + ] const row = { render () { return {this.$slots.default} diff --git a/components/vc-table/demo/subTable.js b/components/vc-table/demo/subTable.js index ddb78f894..6b77720f9 100644 --- a/components/vc-table/demo/subTable.js +++ b/components/vc-table/demo/subTable.js @@ -41,7 +41,7 @@ export default { { title: 'title2', dataIndex: 'b', key: 'b', width: 100 }, { title: 'title3', dataIndex: 'c', key: 'c', width: 200 }, { - title: 'Operations', dataIndex: '', key: 'x', render: (h, text, record) => { + title: 'Operations', dataIndex: '', key: 'x', customRender: (text, record) => { return this.handleClick(record, e)}>click {record.a} }, }, diff --git a/components/vc-table/demo/title-and-footer.js b/components/vc-table/demo/title-and-footer.js index 15f013b20..2c7f38a64 100644 --- a/components/vc-table/demo/title-and-footer.js +++ b/components/vc-table/demo/title-and-footer.js @@ -2,17 +2,6 @@ import Table from '../index' import '../assets/index.less' -const columns = [ - { title: 'title1', dataIndex: 'a', key: 'a', width: 100 }, - { id: '123', title: 'title2', dataIndex: 'b', key: 'b', width: 100 }, - { title: 'title3', dataIndex: 'c', key: 'c', width: 200 }, - { - title: 'Operations', dataIndex: '', key: 'd', render (h) { - return Operations - }, - }, -] - const data = [ { a: '123', key: '1' }, { a: 'cdd', b: 'edd', key: '2' }, @@ -21,6 +10,16 @@ const data = [ export default { render () { + const columns = [ + { title: 'title1', dataIndex: 'a', key: 'a', width: 100 }, + { id: '123', title: 'title2', dataIndex: 'b', key: 'b', width: 100 }, + { title: 'title3', dataIndex: 'c', key: 'c', width: 200 }, + { + title: 'Operations', dataIndex: '', key: 'd', customRender () { + return Operations + }, + }, + ] return (

title and footer

diff --git a/components/vc-table/index.js b/components/vc-table/index.js index 518e79aca..356b5a1fa 100644 --- a/components/vc-table/index.js +++ b/components/vc-table/index.js @@ -34,8 +34,8 @@ const Table = { if (getSlotOptions(element).isTableColumnGroup) { column.children = this.normalize(children) } else { - const render = element.data && element.data.scopedSlots && element.data.scopedSlots.render - column.render = column.render || render + const customRender = element.data && element.data.scopedSlots && element.data.scopedSlots.default + column.customRender = column.customRender || customRender } columns.push(column) }) diff --git a/components/vc-table/src/Column.jsx b/components/vc-table/src/Column.jsx index 1b7022130..5bb44604f 100644 --- a/components/vc-table/src/Column.jsx +++ b/components/vc-table/src/Column.jsx @@ -15,7 +15,7 @@ export default { 'left', 'right', ]), - render: PropTypes.func, + customRender: PropTypes.func, className: PropTypes.string, // onCellClick: PropTypes.func, // onCell: PropTypes.func, diff --git a/components/vc-table/src/ExpandableTable.jsx b/components/vc-table/src/ExpandableTable.jsx index 2aead09c7..7500be3e7 100644 --- a/components/vc-table/src/ExpandableTable.jsx +++ b/components/vc-table/src/ExpandableTable.jsx @@ -122,7 +122,7 @@ const ExpandableTable = { rows[0].unshift({ ...iconColumn, column: iconColumn }) }, - renderExpandedRow (record, index, render, className, ancestorKeys, indent, fixed) { + renderExpandedRow (record, index, expandedRowRender, className, ancestorKeys, indent, fixed) { const { prefixCls, expandIconAsCell, indentSize } = this let colCount if (fixed === 'left') { @@ -138,7 +138,7 @@ const ExpandableTable = { props: { colSpan: colCount, }, - children: fixed !== 'right' ? render(record, index, indent) : ' ', + children: fixed !== 'right' ? expandedRowRender(record, index, indent) : ' ', }), }] if (expandIconAsCell && fixed !== 'right') { diff --git a/components/vc-table/src/TableCell.jsx b/components/vc-table/src/TableCell.jsx index 831a8dac2..70d07a1ba 100644 --- a/components/vc-table/src/TableCell.jsx +++ b/components/vc-table/src/TableCell.jsx @@ -40,7 +40,7 @@ export default { column, component: BodyCell, } = this - const { dataIndex, render, className = '' } = column + const { dataIndex, customRender, className = '' } = column const cls = className || column.class // 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. @@ -63,8 +63,8 @@ export default { let colSpan let rowSpan - if (render) { - text = render(h, text, record, index) + if (customRender) { + text = customRender(text, record, index) if (this.isInvalidRenderCellText(text)) { tdProps.attrs = text.attrs || text.props || {} colSpan = tdProps.attrs.colSpan