Skip to content

Commit bbc6c29

Browse files
committed
feat: table sortTitle opt
1 parent 8340a0b commit bbc6c29

File tree

11 files changed

+226
-72
lines changed

11 files changed

+226
-72
lines changed

components/_util/props-util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export function mergeProps () {
252252
}
253253

254254
function isValidElement (element) {
255-
return element && element.context && element.context._isVue
255+
return element && typeof element === 'object' && ('componentOptions' in element && 'context' in element)
256256
}
257257

258258
export {

components/table/Table.jsx

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import Column from './Column'
1616
import ColumnGroup from './ColumnGroup'
1717
import createBodyRow from './createBodyRow'
1818
import { flatArray, treeMap, flatFilter } from './util'
19-
import { initDefaultProps, mergeProps, getOptionProps, getComponentFromProp, isValidElement } from '../_util/props-util'
19+
import { initDefaultProps, mergeProps, getOptionProps, isValidElement, filterEmpty, getAllProps } from '../_util/props-util'
2020
import BaseMixin from '../_util/BaseMixin'
2121
import {
2222
TableProps,
@@ -740,13 +740,7 @@ export default {
740740
let filterDropdown
741741
let sortButton
742742
let customHeaderCell = column.customHeaderCell
743-
const { slots } = column
744-
let title = column.title
745-
if (title === undefined && slots && slots.title) {
746-
title = this.$slots[slots.title]
747-
title = title && title[0]
748-
}
749-
const sortTitle = this.getColumnTitle(title, {}) || locale.sortTitle
743+
const sortTitle = this.getColumnTitle(column.title, {}) || locale.sortTitle
750744
const isSortColumn = this.isSortColumn(column)
751745
if ((column.filters && column.filters.length > 0) || column.filterDropdown) {
752746
const colFilters = key in filters ? filters[key] : []
@@ -842,11 +836,20 @@ export default {
842836
return
843837
}
844838
if (isValidElement(title)) {
845-
debugger
846-
const props = title.props
847-
if (props && props.children) {
848-
const { children } = props
849-
return this.getColumnTitle(children, props)
839+
const props = title.componentOptions
840+
let children = null
841+
if (props && props.children) { // for component
842+
children = filterEmpty(props.children)
843+
} else if (title.children) { // for dom
844+
children = filterEmpty(title.children)
845+
}
846+
if (children && children.length === 1) {
847+
children = children[0]
848+
const attrs = getAllProps(title)
849+
if (!children.tag && children.text) { // for textNode
850+
children = children.text
851+
}
852+
return this.getColumnTitle(children, attrs)
850853
}
851854
} else {
852855
return parentNode.title || title

components/table/__tests__/Table.rowSelection.test.js

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,4 +479,149 @@ describe('Table.rowSelection', () => {
479479
expect(wrapper.findAll('thead tr div').at(0).text()).toBe('单选')
480480
})
481481
})
482+
483+
// https://github.com/ant-design/ant-design/issues/11384
484+
it('should keep item even if in filter', async () => {
485+
const filterColumns = [
486+
{
487+
title: 'Name',
488+
dataIndex: 'name',
489+
filters: [
490+
{
491+
text: 'Jack',
492+
value: 'Jack',
493+
},
494+
{
495+
text: 'Lucy',
496+
value: 'Lucy',
497+
},
498+
],
499+
filterDropdownVisible: true,
500+
onFilter: (value, record) => record.name.indexOf(value) === 0,
501+
},
502+
]
503+
504+
const onChange = jest.fn()
505+
const rowSelection = {
506+
onChange,
507+
}
508+
509+
const wrapper = mount(Table, {
510+
propsData: {
511+
columns: filterColumns, dataSource: data, rowSelection: rowSelection,
512+
},
513+
sync: false,
514+
})
515+
516+
const dropdownWrapper = mount({
517+
render () {
518+
return wrapper.find({ name: 'Trigger' }).vm.getComponent()
519+
},
520+
}, { sync: false })
521+
522+
function clickItem () {
523+
wrapper
524+
.findAll('tbody .ant-table-selection-column .ant-checkbox-input')
525+
.at(0)
526+
.element.checked = true
527+
wrapper
528+
.findAll('tbody .ant-table-selection-column .ant-checkbox-input')
529+
.at(0)
530+
.trigger('change')
531+
}
532+
533+
// Check Jack
534+
dropdownWrapper
535+
.findAll('.ant-dropdown-menu-item .ant-checkbox-wrapper')
536+
.at(0)
537+
.trigger('click')
538+
dropdownWrapper
539+
.find('.ant-table-filter-dropdown-btns .ant-table-filter-dropdown-link.confirm')
540+
.trigger('click')
541+
await asyncExpect(() => {
542+
expect(wrapper.findAll('tbody tr').length).toBe(1)
543+
})
544+
await asyncExpect(() => {
545+
clickItem()
546+
})
547+
await asyncExpect(() => {
548+
expect(onChange.mock.calls[0][0].length).toBe(1)
549+
expect(onChange.mock.calls[0][1].length).toBe(1)
550+
})
551+
552+
await asyncExpect(() => {
553+
dropdownWrapper
554+
.findAll('.ant-dropdown-menu-item .ant-checkbox-wrapper')
555+
.at(0)
556+
.trigger('click')
557+
})
558+
559+
await asyncExpect(() => {
560+
// Check Lucy
561+
dropdownWrapper
562+
.findAll('.ant-dropdown-menu-item .ant-checkbox-wrapper')
563+
.at(1)
564+
.trigger('click')
565+
})
566+
await asyncExpect(() => {
567+
dropdownWrapper
568+
.find('.ant-table-filter-dropdown-btns .ant-table-filter-dropdown-link.confirm')
569+
.trigger('click')
570+
})
571+
await asyncExpect(() => {
572+
expect(wrapper.findAll('tbody tr').length).toBe(1)
573+
})
574+
await asyncExpect(() => {
575+
clickItem()
576+
})
577+
await asyncExpect(() => {
578+
expect(onChange.mock.calls[1][0].length).toBe(2)
579+
expect(onChange.mock.calls[1][1].length).toBe(2)
580+
})
581+
})
582+
583+
it('render correctly when set childrenColumnName', async () => {
584+
const newDatas = [
585+
{
586+
key: 1,
587+
name: 'Jack',
588+
children: [
589+
{
590+
key: 11,
591+
name: 'John Brown',
592+
},
593+
],
594+
},
595+
{
596+
key: 2,
597+
name: 'Lucy',
598+
children: [
599+
{
600+
key: 21,
601+
name: 'Lucy Brown',
602+
},
603+
],
604+
},
605+
]
606+
607+
const wrapper = mount(Table, {
608+
propsData: {
609+
columns: columns, dataSource: newDatas, rowSelection: {}, childrenColumnName: 'test',
610+
},
611+
sync: false,
612+
})
613+
614+
const checkboxes = wrapper.findAll('input')
615+
const checkboxAll = wrapper.find({ name: 'SelectionCheckboxAll' })
616+
617+
checkboxes.at(1).element.checked = true
618+
checkboxes.at(1).trigger('change')
619+
expect(checkboxAll.vm.$data).toEqual({ indeterminate: true, checked: false })
620+
621+
checkboxes.at(2).element.checked = true
622+
checkboxes.at(2).trigger('change')
623+
await asyncExpect(() => {
624+
expect(checkboxAll.vm.$data).toEqual({ indeterminate: false, checked: true })
625+
})
626+
})
482627
})

components/table/__tests__/__snapshots__/Table.sorter.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`Table.sorter renders sorter icon correctly 1`] = `
44
<thead class="ant-table-thead">
55
<tr>
66
<th key="name" class="ant-table-column-has-actions ant-table-column-has-sorters">
7-
<div title="Sort" class="ant-table-column-sorters">Name<div class="ant-table-column-sorter"><i class="ant-table-column-sorter-up off anticon anticon-caret-up"><svg viewBox="0 0 1024 1024" data-icon="caret-up" width="1em" height="1em" fill="currentColor" aria-hidden="true" class="">
7+
<div title="Name" class="ant-table-column-sorters">Name<div class="ant-table-column-sorter"><i class="ant-table-column-sorter-up off anticon anticon-caret-up"><svg viewBox="0 0 1024 1024" data-icon="caret-up" width="1em" height="1em" fill="currentColor" aria-hidden="true" class="">
88
<path d="M858.9 689L530.5 308.2c-9.4-10.9-27.5-10.9-37 0L165.1 689c-12.2 14.2-1.2 35 18.5 35h656.8c19.7 0 30.7-20.8 18.5-35z"></path>
99
</svg></i><i class="ant-table-column-sorter-down off anticon anticon-caret-down"><svg viewBox="0 0 1024 1024" data-icon="caret-down" width="1em" height="1em" fill="currentColor" aria-hidden="true" class="">
1010
<path d="M840.4 300H183.6c-19.7 0-30.7 20.8-18.5 35l328.4 380.8c9.4 10.9 27.5 10.9 37 0L858.9 335c12.2-14.2 1.2-35-18.5-35z"></path>

0 commit comments

Comments
 (0)