Commit b4176de8 by 同語 Committed by GitHub

fix(data-table): capture row selection state in memo comparison (#5524)

Merge pull request #5524 from RedwindA/fix/channelCheckbox
parents 9bc1a53d 179f69df
...@@ -29,15 +29,20 @@ type DataTableRowProps<TData> = { ...@@ -29,15 +29,20 @@ type DataTableRowProps<TData> = {
getColumnClassName?: DataTableColumnClassName getColumnClassName?: DataTableColumnClassName
} & Omit<React.ComponentProps<typeof TableRow>, 'children'> } & Omit<React.ComponentProps<typeof TableRow>, 'children'>
type DataTableRowInnerProps<TData> = DataTableRowProps<TData> & {
isSelected: boolean
}
function DataTableRowInner<TData>({ function DataTableRowInner<TData>({
row, row,
isSelected,
className, className,
getColumnClassName, getColumnClassName,
...rowProps ...rowProps
}: DataTableRowProps<TData>) { }: DataTableRowInnerProps<TData>) {
return ( return (
<TableRow <TableRow
data-state={row.getIsSelected() ? 'selected' : undefined} data-state={isSelected ? 'selected' : undefined}
className={className} className={className}
{...rowProps} {...rowProps}
> >
...@@ -56,17 +61,23 @@ function DataTableRowInner<TData>({ ...@@ -56,17 +61,23 @@ function DataTableRowInner<TData>({
) )
} }
export const DataTableRow = React.memo(DataTableRowInner, (prev, next) => { const MemoizedDataTableRow = React.memo(DataTableRowInner, (prev, next) => {
// Skip re-render when only the getColumnClassName reference changed but the // Do not read row.getIsSelected() here: TanStack row objects may keep a stable
// row identity and selection state are the same — callers rarely stabilize // reference while their selection state changes.
// this callback, so excluding it from comparison avoids unnecessary renders.
return ( return (
prev.row === next.row && prev.row === next.row &&
prev.className === next.className && prev.className === next.className &&
prev.row.getIsSelected() === next.row.getIsSelected() prev.getColumnClassName === next.getColumnClassName &&
prev.isSelected === next.isSelected
) )
}) as typeof DataTableRowInner }) as typeof DataTableRowInner
export function DataTableRow<TData>(props: DataTableRowProps<TData>) {
return (
<MemoizedDataTableRow {...props} isSelected={props.row.getIsSelected()} />
)
}
function renderCellContent<TData>(cell: Cell<TData, unknown>) { function renderCellContent<TData>(cell: Cell<TData, unknown>) {
const content = flexRender(cell.column.columnDef.cell, cell.getContext()) const content = flexRender(cell.column.columnDef.cell, cell.getContext())
const textContent = getPrimitiveTextContent(content) const textContent = getPrimitiveTextContent(content)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment