diff --git a/components/table/Table.tsx b/components/table/Table.tsx index be88ca4afd..a1a21952f2 100755 --- a/components/table/Table.tsx +++ b/components/table/Table.tsx @@ -552,7 +552,12 @@ export default class Table extends React.Component, any> { selectionDirty: true, }); // when select custom selection, callback selections[n].onSelect - if (index > 1 && typeof onSelectFunc === 'function') { + const { rowSelection } = this.props; + let customSelectionStartIndex = 2; + if (rowSelection && rowSelection.hideDefaultSelections) { + customSelectionStartIndex = 0; + } + if (index >= customSelectionStartIndex && typeof onSelectFunc === 'function') { return onSelectFunc(changeableRowKeys); } this.setSelectedRowKeys(selectedRowKeys, { diff --git a/components/table/__tests__/Table.rowSelection.test.js b/components/table/__tests__/Table.rowSelection.test.js index 3b9a74abf7..5e4d1a4f49 100644 --- a/components/table/__tests__/Table.rowSelection.test.js +++ b/components/table/__tests__/Table.rowSelection.test.js @@ -252,6 +252,33 @@ describe('Table.rowSelection', () => { expect(dropdownWrapper.find('.ant-dropdown-menu-item').length).toBe(2); }); + it('handle custom selection onSelect correctly when hide default selection options', () => { + const handleSelectOdd = jest.fn(); + const handleSelectEven = jest.fn(); + const rowSelection = { + hideDefaultSelections: true, + selections: [{ + key: 'odd', + text: '奇数项', + onSelect: handleSelectOdd, + }, { + key: 'even', + text: '偶数项', + onSelect: handleSelectEven, + }], + }; + const wrapper = mount(createTable({ rowSelection })); + + const dropdownWrapper = mount(wrapper.find('Trigger').node.getComponent()); + expect(dropdownWrapper.find('.ant-dropdown-menu-item').length).toBe(2); + + dropdownWrapper.find('.ant-dropdown-menu-item > div').at(0).simulate('click'); + expect(handleSelectOdd).toBeCalledWith([0, 1, 2, 3]); + + dropdownWrapper.find('.ant-dropdown-menu-item > div').at(1).simulate('click'); + expect(handleSelectEven).toBeCalledWith([0, 1, 2, 3]); + }); + // https://github.com/ant-design/ant-design/issues/4245 it('handles disabled checkbox correctly when dataSource changes', () => { const rowSelection = {