mirror of
https://gitee.com/ant-design/ant-design.git
synced 2024-12-02 20:19:44 +08:00
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:
parent
c29c46e967
commit
04d140d7a6
@ -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,
|
||||
|
@ -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;
|
||||
}),
|
||||
);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user