fix: Table preset SELECTION can modify disabled record (#32027)

* fix: tbl select opt affact disabled

* test: add disabled test case
This commit is contained in:
二货机器人 2021-09-03 19:47:23 +08:00 committed by GitHub
parent c29c46e967
commit 04d140d7a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 6 deletions

View File

@ -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,

View File

@ -281,7 +281,14 @@ export default function useSelection<RecordType>(
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<RecordType>(
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<RecordType>(
text: tableLocale.selectNone,
onSelect() {
onSelectNone?.();
setSelectedKeys([]);
setSelectedKeys(
Array.from(derivedSelectedKeySet).filter(key => {
const checkProps = checkboxPropsMap.get(key);
return checkProps?.disabled;
}),
);
},
};
}