diff --git a/components/table/__tests__/Table.rowSelection.test.js b/components/table/__tests__/Table.rowSelection.test.js index 3c63a6e3ca..efbf14b1ee 100644 --- a/components/table/__tests__/Table.rowSelection.test.js +++ b/components/table/__tests__/Table.rowSelection.test.js @@ -441,6 +441,76 @@ describe('Table.rowSelection', () => { expect(handleSelectEven).toHaveBeenCalledWith([0, 1, 2, 3]); }); + describe('preset selection options', () => { + const presetData = [ + { key: 0, name: 'Jack' }, + { key: 1, name: 'Lucy', disabled: true }, + { key: 2, name: 'Tom' }, + ]; + + const getCheckboxProps = record => record; + + it('SELECTION_ALL', () => { + const onChange = jest.fn(); + const wrapper = mount( + createTable({ + dataSource: presetData, + rowSelection: { + onChange, + defaultSelectedRowKeys: [2], + getCheckboxProps, + selections: [Table.SELECTION_ALL], + }, + }), + ); + + wrapper.find('Trigger').setState({ popupVisible: true }); + wrapper.find('li.ant-dropdown-menu-item').first().simulate('click'); + + expect(onChange).toHaveBeenCalledWith([0, 2], expect.anything()); + }); + + it('SELECTION_INVERT', () => { + const onChange = jest.fn(); + const wrapper = mount( + createTable({ + dataSource: presetData, + rowSelection: { + onChange, + defaultSelectedRowKeys: [2], + getCheckboxProps, + selections: [Table.SELECTION_INVERT], + }, + }), + ); + + wrapper.find('Trigger').setState({ popupVisible: true }); + wrapper.find('li.ant-dropdown-menu-item').first().simulate('click'); + + expect(onChange).toHaveBeenCalledWith([0], expect.anything()); + }); + + it('SELECTION_NONE', () => { + const onChange = jest.fn(); + const wrapper = mount( + createTable({ + dataSource: presetData, + rowSelection: { + onChange, + defaultSelectedRowKeys: [1, 2], + getCheckboxProps, + selections: [Table.SELECTION_NONE], + }, + }), + ); + + wrapper.find('Trigger').setState({ popupVisible: true }); + wrapper.find('li.ant-dropdown-menu-item').first().simulate('click'); + + expect(onChange).toHaveBeenCalledWith([1], expect.anything()); + }); + }); + it('could hide selectAll checkbox and custom selection', () => { const rowSelection = { hideSelectAll: true, diff --git a/components/table/hooks/useSelection.tsx b/components/table/hooks/useSelection.tsx index 36084447f7..92144902d7 100644 --- a/components/table/hooks/useSelection.tsx +++ b/components/table/hooks/useSelection.tsx @@ -281,7 +281,14 @@ export default function useSelection( key: 'all', text: tableLocale.selectionAll, onSelect() { - setSelectedKeys(data.map((record, index) => getRowKey(record, index))); + setSelectedKeys( + data + .map((record, index) => getRowKey(record, index)) + .filter(key => { + const checkProps = checkboxPropsMap.get(key); + return !checkProps?.disabled || derivedSelectedKeySet.has(key); + }), + ); }, }; } @@ -293,11 +300,14 @@ export default function useSelection( const keySet = new Set(derivedSelectedKeySet); pageData.forEach((record, index) => { const key = getRowKey(record, index); + const checkProps = checkboxPropsMap.get(key); - if (keySet.has(key)) { - keySet.delete(key); - } else { - keySet.add(key); + if (!checkProps?.disabled) { + if (keySet.has(key)) { + keySet.delete(key); + } else { + keySet.add(key); + } } }); @@ -321,7 +331,12 @@ export default function useSelection( text: tableLocale.selectNone, onSelect() { onSelectNone?.(); - setSelectedKeys([]); + setSelectedKeys( + Array.from(derivedSelectedKeySet).filter(key => { + const checkProps = checkboxPropsMap.get(key); + return checkProps?.disabled; + }), + ); }, }; }