From 8340a0b6842037460950be8772ecb6423fecc7c1 Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Sat, 5 Jan 2019 18:09:27 +0800 Subject: [PATCH] feat: table add expandIcon --- components/table/SelectionCheckboxAll.jsx | 38 +++++----- components/table/Table.jsx | 92 ++++++++++++++--------- components/table/demo/index.vue | 2 +- components/table/index.en-US.md | 3 +- components/table/index.zh-CN.md | 5 +- types/table/column.d.ts | 3 +- types/table/table.d.ts | 6 ++ 7 files changed, 91 insertions(+), 58 deletions(-) diff --git a/components/table/SelectionCheckboxAll.jsx b/components/table/SelectionCheckboxAll.jsx index de584ebda..7cb3fa8de 100644 --- a/components/table/SelectionCheckboxAll.jsx +++ b/components/table/SelectionCheckboxAll.jsx @@ -50,12 +50,12 @@ export default { subscribe () { const { store } = this this.unsubscribe = store.subscribe(() => { - this.setCheckState() + this.setCheckState(this.$props) }) }, - checkSelection (data, type, byDefaultChecked) { - const { store, getCheckboxPropsByItem, getRecordKey } = this + checkSelection (props, data, type, byDefaultChecked) { + const { store, getCheckboxPropsByItem, getRecordKey } = props || this.$props // type should be 'every' | 'some' if (type === 'every' || type === 'some') { return ( @@ -68,9 +68,9 @@ export default { return false }, - setCheckState () { - const checked = this.getCheckState() - const indeterminate = this.getIndeterminateState() + setCheckState (props) { + const checked = this.getCheckState(props) + const indeterminate = this.getIndeterminateState(props) this.setState((prevState) => { const newState = {} if (indeterminate !== prevState.indeterminate) { @@ -83,23 +83,23 @@ export default { }) }, - getCheckState () { + getCheckState (props) { const { store, data } = this let checked if (!data.length) { checked = false } else { checked = store.getState().selectionDirty - ? this.checkSelection(data, 'every', false) + ? this.checkSelection(props, data, 'every', false) : ( - this.checkSelection(data, 'every', false) || - this.checkSelection(data, 'every', true) + this.checkSelection(props, data, 'every', false) || + this.checkSelection(props, data, 'every', true) ) } return checked }, - getIndeterminateState () { + getIndeterminateState (props) { const { store, data } = this let indeterminate if (!data.length) { @@ -107,19 +107,19 @@ export default { } else { indeterminate = store.getState().selectionDirty ? ( - this.checkSelection(data, 'some', false) && - !this.checkSelection(data, 'every', false) + this.checkSelection(props, data, 'some', false) && + !this.checkSelection(props, data, 'every', false) ) - : ((this.checkSelection(data, 'some', false) && - !this.checkSelection(data, 'every', false)) || - (this.checkSelection(data, 'some', true) && - !this.checkSelection(data, 'every', true)) + : ((this.checkSelection(props, data, 'some', false) && + !this.checkSelection(props, data, 'every', false)) || + (this.checkSelection(props, data, 'some', true) && + !this.checkSelection(props, data, 'every', true)) ) } return indeterminate }, - handleSelectAllChagne (e) { + handleSelectAllChange (e) { const checked = e.target.checked this.$emit('select', checked ? 'all' : 'removeAll', 0, null) }, @@ -182,7 +182,7 @@ export default { checked={checked} indeterminate={indeterminate} disabled={disabled} - onChange={this.handleSelectAllChagne} + onChange={this.handleSelectAllChange} /> {customSelections} diff --git a/components/table/Table.jsx b/components/table/Table.jsx index 7bdb7e9c4..a81b2a719 100755 --- a/components/table/Table.jsx +++ b/components/table/Table.jsx @@ -16,7 +16,7 @@ import Column from './Column' import ColumnGroup from './ColumnGroup' import createBodyRow from './createBodyRow' import { flatArray, treeMap, flatFilter } from './util' -import { initDefaultProps, mergeProps, getOptionProps } from '../_util/props-util' +import { initDefaultProps, mergeProps, getOptionProps, getComponentFromProp, isValidElement } from '../_util/props-util' import BaseMixin from '../_util/BaseMixin' import { TableProps, @@ -437,7 +437,7 @@ export default { let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection) const key = this.getRecordKey(record, rowIndex) const { pivot } = this.$data - const rows = this.getFlatCurrentPageData() + const rows = this.getFlatCurrentPageData(this.$props.childrenColumnName) let realIndex = rowIndex if (this.$props.expandedRowRender) { realIndex = rows.findIndex(row => this.getRecordKey(row, rowIndex) === key) @@ -500,10 +500,8 @@ export default { handleRadioSelect (record, rowIndex, e) { const checked = e.target.checked const nativeEvent = e.nativeEvent - const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection() - let selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection) const key = this.getRecordKey(record, rowIndex) - selectedRowKeys = [key] + const selectedRowKeys = [key] this.store.setState({ selectionDirty: true, }) @@ -517,7 +515,7 @@ export default { }, handleSelectRow (selectionKey, index, onSelectFunc) { - const data = this.getFlatCurrentPageData() + const data = this.getFlatCurrentPageData(this.$props.childrenColumnName) const defaultSelection = this.store.getState().selectionDirty ? [] : this.getDefaultSelection() const selectedRowKeys = this.store.getState().selectedRowKeys.concat(defaultSelection) const changeableRowKeys = data @@ -662,10 +660,10 @@ export default { }, renderRowSelection (locale) { - const { prefixCls, rowSelection } = this + const { prefixCls, rowSelection, childrenColumnName } = this const columns = this.columns.concat() if (rowSelection) { - const data = this.getFlatCurrentPageData().filter((item, index) => { + const data = this.getFlatCurrentPageData(childrenColumnName).filter((item, index) => { if (rowSelection.getCheckboxProps) { return !this.getCheckboxPropsByItem(item, index).props.disabled } @@ -741,6 +739,14 @@ export default { const key = this.getColumnKey(column, i) let filterDropdown let sortButton + let customHeaderCell = column.customHeaderCell + const { slots } = column + let title = column.title + if (title === undefined && slots && slots.title) { + title = this.$slots[slots.title] + title = title && title[0] + } + const sortTitle = this.getColumnTitle(title, {}) || locale.sortTitle const isSortColumn = this.isSortColumn(column) if ((column.filters && column.filters.length > 0) || column.filterDropdown) { const colFilters = key in filters ? filters[key] : [] @@ -759,12 +765,6 @@ export default { ) } if (column.sorter) { - // const isSortColumn = this.isSortColumn(column) - // if (isSortColumn) { - // column.className = classNames(column.className, { - // [`${prefixCls}-column-sort`]: sortOrder, - // }) - // } const isAscend = isSortColumn && sortOrder === 'ascend' const isDescend = isSortColumn && sortOrder === 'descend' sortButton = ( @@ -781,7 +781,27 @@ export default { /> ) + customHeaderCell = (col) => { + let colProps = {} + // Get original first + if (column.customHeaderCell) { + colProps = { + ...column.customHeaderCell(col), + } + } + colProps.on = colProps.on || {} + // Add sorter logic + const onHeaderCellClick = colProps.on.click + colProps.on.click = (...args) => { + this.toggleSortOrder(column) + if (onHeaderCellClick) { + onHeaderCellClick(...args) + } + } + return colProps + } } + const sortTitleString = sortButton && typeof sortTitle === 'string' ? sortTitle : undefined return { ...column, className: classNames(column.className, { @@ -793,29 +813,16 @@ export default { title: [