feat: Table adds rowSelection.defaultSelectedRowKeys (#29879)

This commit is contained in:
kily zhou 2021-03-25 09:11:39 +08:00 committed by GitHub
parent e49281c7f5
commit 59c3c71b72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 11 deletions

View File

@ -66,6 +66,22 @@ describe('Table.rowSelection', () => {
.filter(key => key !== null);
}
it('select default row', () => {
const wrapper = mount(createTable({ rowSelection: { defaultSelectedRowKeys: [0] } }));
const checkboxes = wrapper.find('input');
expect(getSelections(wrapper)).toEqual([0]);
checkboxes.at(1).simulate('change', { target: { checked: false } });
expect(getSelections(wrapper)).toEqual([]);
checkboxes.at(0).simulate('change', { target: { checked: true } });
expect(getSelections(wrapper)).toEqual([0, 1, 2, 3]);
checkboxes.at(0).simulate('change', { target: { checked: false } });
expect(getSelections(wrapper)).toEqual([]);
});
it('select by checkbox', () => {
const wrapper = mount(createTable());
const checkboxes = wrapper.find('input');

View File

@ -79,6 +79,7 @@ export default function useSelection<RecordType>(
const {
preserveSelectedRowKeys,
selectedRowKeys,
defaultSelectedRowKeys,
getCheckboxProps,
onChange: onSelectionChange,
onSelect,
@ -112,9 +113,12 @@ export default function useSelection<RecordType>(
const preserveRecordsRef = React.useRef(new Map<Key, RecordType>());
// ========================= Keys =========================
const [mergedSelectedKeys, setMergedSelectedKeys] = useMergedState(selectedRowKeys || [], {
value: selectedRowKeys,
});
const [mergedSelectedKeys, setMergedSelectedKeys] = useMergedState(
selectedRowKeys || defaultSelectedRowKeys || [],
{
value: selectedRowKeys,
},
);
const { keyEntities } = useMemo(
() =>

View File

@ -155,9 +155,9 @@ const columns = [
### ColumnGroup
| 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| title | 列头显示文字 | ReactNode | - |
| 参数 | 说明 | 类型 | 默认值 |
| ----- | ------------ | --------- | ------ |
| title | 列头显示文字 | ReactNode | - |
### pagination
@ -205,6 +205,7 @@ const columns = [
| preserveSelectedRowKeys | 当数据被删除时仍然保留选项的 `key` | boolean | - | 4.4.0 |
| renderCell | 渲染勾选框,用法与 Column 的 `render` 相同 | function(checked, record, index, originNode) {} | - | 4.1.0 |
| selectedRowKeys | 指定选中项的 key 数组,需要和 onChange 进行配合 | string\[] \| number\[] | \[] | |
| defaultSelectedRowKeys | 默认选中项的 key 数组 | string\[] \| number\[] | \[] | |
| selections | 自定义选择项 [配置项](#selection), 设为 `true` 时使用默认选择项 | object\[] \| boolean | true | |
| type | 多选/单选 | `checkbox` \| `radio` | `checkbox` | |
| onChange | 选中项发生变化时的回调 | function(selectedRowKeys, selectedRows) | - | |
@ -223,11 +224,11 @@ const columns = [
### selection
| 参数 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- |
| key | React 需要的 key建议设置 | string | - |
| text | 选择项显示的文字 | ReactNode | - |
| onSelect | 选择项点击回调 | function(changeableRowKeys) | - |
| 参数 | 说明 | 类型 | 默认值 |
| -------- | -------------------------- | --------------------------- | ------ |
| key | React 需要的 key建议设置 | string | - |
| text | 选择项显示的文字 | ReactNode | - |
| onSelect | 选择项点击回调 | function(changeableRowKeys) | - |
## 在 TypeScript 中使用

View File

@ -143,6 +143,7 @@ export interface TableRowSelection<T> {
preserveSelectedRowKeys?: boolean;
type?: RowSelectionType;
selectedRowKeys?: Key[];
defaultSelectedRowKeys?: Key[];
onChange?: (selectedRowKeys: Key[], selectedRows: T[]) => void;
getCheckboxProps?: (record: T) => Partial<Omit<CheckboxProps, 'checked' | 'defaultChecked'>>;
onSelect?: SelectionSelectFn<T>;