Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/components/table/__tests__/row.events.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,28 @@ TABLES.forEach((TTable) => {
});
});
});

describe('PrimaryTable row selection', () => {
it('select all skips rows disabled by checkProps', () => {
const fn = vi.fn();
const columns = [
{
colKey: 'row-select',
type: 'multiple' as const,
checkProps: ({ row }) => ({ disabled: row.index === 101 }),
},
...SIMPLE_COLUMNS,
];

const { container } = render(
<PrimaryTable rowKey="index" data={data} columns={columns} selectedRowKeys={[]} onSelectChange={fn} />,
);

fireEvent.click(container.querySelector('thead .t-checkbox'));

expect(fn).toHaveBeenCalledWith(
[100, 102, 103, 104],
expect.objectContaining({ currentRowKey: 'CHECK_ALL_BOX', type: 'check' }),
);
});
});
9 changes: 5 additions & 4 deletions packages/components/table/hooks/useRowSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import React, { useEffect, useState, MouseEvent, useMemo } from 'react';
import { intersection, get, isFunction } from 'lodash-es';
import { isRowSelectedDisabled } from '@tdesign/common-js/table/utils';
import log from '@tdesign/common-js/log/index';
import useControlled from '../../hooks/useControlled';
import {
Expand Down Expand Up @@ -31,7 +30,8 @@ export default function useRowSelect(
const [tSelectedRowKeys, setTSelectedRowKeys] = useControlled(props, 'selectedRowKeys', props.onSelectChange, {
defaultSelectedRowKeys: props.defaultSelectedRowKeys || [],
});
const selectColumn = columns.find(({ type }) => ['multiple', 'single'].includes(type));
const selectColumnIndex = columns.findIndex(({ type }) => ['multiple', 'single'].includes(type));
const selectColumn = columns[selectColumnIndex];

const canSelectedRows = useMemo(() => {
const currentData = reserveSelectedRowOnPaginate ? data : currentPaginateData;
Expand Down Expand Up @@ -78,7 +78,8 @@ export default function useRowSelect(
);

function isDisabled(row: Record<string, any>, rowIndex: number): boolean {
return isRowSelectedDisabled(selectColumn, row, rowIndex);
if (!selectColumn) return false;
return getRowSelectDisabledData({ row, rowIndex, col: selectColumn, colIndex: selectColumnIndex }).disabled;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个修复没有考虑「禁用项提前被选中(state 注入)」的情况,导致半选无法取消
issue 相关的完整修复已在另一个PR,近期会发布

}

function getSelectedHeader() {
Expand Down Expand Up @@ -109,7 +110,7 @@ export default function useRowSelect(
const disabled: boolean = typeof col.disabled === 'function' ? col.disabled({ row, rowIndex }) : col.disabled;
const checkProps = isFunction(col.checkProps) ? col.checkProps({ row, rowIndex }) : col.checkProps;
return {
disabled: disabled || checkProps?.disabled,
disabled: Boolean(disabled || checkProps?.disabled),
checkProps,
};
}
Expand Down
Loading