-
Notifications
You must be signed in to change notification settings - Fork 4
fix(Table): table rerenders #150
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ffb5b06
07bda9f
82e6c7c
1b618f7
26fd3a4
16e2965
0438d24
d5f8962
167bf53
c592628
844a44f
8dc1e4a
40ad95f
c7cb31a
2cae068
cd1bb44
5fa461c
81e97e5
bd67982
81e98ef
898879f
500ba96
dd84a47
7475eb4
9d3985d
da9eeeb
5990aaf
45e460c
6866364
75d477a
93dba9c
fb5f8bc
6e1a6f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| import {arraysShallowEqual} from '../../utils'; | ||
|
|
||
| import type {BaseCellProps} from './BaseCell'; | ||
| import {BaseCell} from './BaseCell'; | ||
|
|
||
| function areCellPropsEqual<TData>(prev: BaseCellProps<TData>, next: BaseCellProps<TData>): boolean { | ||
| return ( | ||
| arraysShallowEqual(prev._rowVersion ?? [], next._rowVersion ?? []) && | ||
| prev.cell === next.cell && | ||
| prev.className === next.className && | ||
| prev.attributes === next.attributes && | ||
| prev.style === next.style && | ||
| prev.children === next.children && | ||
| prev.colSpan === next.colSpan && | ||
| prev['aria-colindex'] === next['aria-colindex'] | ||
| ); | ||
| } | ||
|
|
||
| export const MemoBaseCell = React.memo(BaseCell, areCellPropsEqual) as typeof BaseCell; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from './BaseCell'; | ||
| export * from './BaseCell.memo'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| import {arraysShallowEqual} from '../../utils'; | ||
| import {MemoBaseCell} from '../BaseCell/BaseCell.memo'; | ||
|
|
||
| import {BaseDraggableRow} from './BaseDraggableRow'; | ||
| import type {BaseDraggableRowProps} from './BaseDraggableRow'; | ||
|
|
||
| export interface MemoBaseDraggableRowProps< | ||
| TData, | ||
| TScrollElement extends Element | Window = HTMLDivElement, | ||
| > extends BaseDraggableRowProps<TData, TScrollElement> { | ||
| _rowVersion: readonly unknown[]; | ||
| } | ||
|
|
||
| // eslint-disable-next-line complexity | ||
| function areEqual<TData, TScrollElement extends Element | Window>( | ||
| prev: Readonly<BaseDraggableRowProps<TData, TScrollElement>>, | ||
| next: Readonly<BaseDraggableRowProps<TData, TScrollElement>>, | ||
| ): boolean { | ||
| return ( | ||
| prev.row === next.row && | ||
| arraysShallowEqual(prev._rowVersion ?? [], next._rowVersion ?? []) && | ||
| prev.table === next.table && | ||
| prev.virtualItem?.start === next.virtualItem?.start && | ||
| prev.virtualItem?.size === next.virtualItem?.size && | ||
| prev.style === next.style && | ||
| prev.cellClassName === next.cellClassName && | ||
| prev.className === next.className && | ||
| prev.onClick === next.onClick && | ||
| prev.getIsCustomRow === next.getIsCustomRow && | ||
| prev.getIsGroupHeaderRow === next.getIsGroupHeaderRow && | ||
| prev.renderCustomRowContent === next.renderCustomRowContent && | ||
| prev.renderGroupHeader === next.renderGroupHeader && | ||
| prev.renderGroupHeaderRowContent === next.renderGroupHeaderRowContent && | ||
| prev.getGroupTitle === next.getGroupTitle && | ||
| prev.groupHeaderClassName === next.groupHeaderClassName && | ||
| prev.attributes === next.attributes && | ||
| prev.cellAttributes === next.cellAttributes && | ||
| prev.rowVirtualizer === next.rowVirtualizer && | ||
| prev['aria-rowindex'] === next['aria-rowindex'] && | ||
| prev['aria-selected'] === next['aria-selected'] | ||
| ); | ||
| } | ||
|
|
||
| const BaseDraggableRowWithMemoCell = React.forwardRef(function BaseDraggableRowWithMemoCellRender< | ||
| TData, | ||
| TScrollElement extends Element | Window, | ||
| >(props: BaseDraggableRowProps<TData, TScrollElement>, ref: React.Ref<HTMLTableRowElement>) { | ||
| return <BaseDraggableRow {...props} Cell={MemoBaseCell} ref={ref} />; | ||
| }) as <TData, TScrollElement extends Element | Window = HTMLDivElement>( | ||
| props: BaseDraggableRowProps<TData, TScrollElement> & {ref?: React.Ref<HTMLTableRowElement>}, | ||
| ) => React.ReactElement; | ||
|
|
||
| export const MemoBaseDraggableRow = React.memo(BaseDraggableRowWithMemoCell, areEqual) as < | ||
| TData, | ||
| TScrollElement extends Element | Window = HTMLDivElement, | ||
| >( | ||
| props: MemoBaseDraggableRowProps<TData, TScrollElement> & { | ||
| ref?: React.Ref<HTMLTableRowElement>; | ||
| }, | ||
| ) => React.ReactElement; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export * from './BaseDraggableRow'; | ||
| export * from './BaseDraggableRow.memo'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| import {arraysShallowEqual} from '../../utils'; | ||
| import {MemoBaseCell} from '../BaseCell/BaseCell.memo'; | ||
|
|
||
| import type {BaseRowProps} from './BaseRow'; | ||
| import {BaseRow} from './BaseRow'; | ||
|
|
||
| export interface MemoBaseRowProps<TData, TScrollElement extends Element | Window = HTMLDivElement> | ||
| extends BaseRowProps<TData, TScrollElement> { | ||
| _rowVersion: readonly unknown[]; | ||
| } | ||
|
|
||
| // eslint-disable-next-line complexity | ||
| function areEqual<TData, TScrollElement extends Element | Window>( | ||
| prev: Readonly<BaseRowProps<TData, TScrollElement>>, | ||
| next: Readonly<BaseRowProps<TData, TScrollElement>>, | ||
| ): boolean { | ||
| return ( | ||
| prev.row === next.row && | ||
| arraysShallowEqual(prev._rowVersion ?? [], next._rowVersion ?? []) && | ||
| prev.table === next.table && | ||
| prev.virtualItem?.start === next.virtualItem?.start && | ||
| prev.virtualItem?.size === next.virtualItem?.size && | ||
| prev.style === next.style && | ||
| prev.cellClassName === next.cellClassName && | ||
| prev.className === next.className && | ||
| prev.onClick === next.onClick && | ||
| prev.getIsCustomRow === next.getIsCustomRow && | ||
| prev.getIsGroupHeaderRow === next.getIsGroupHeaderRow && | ||
| prev.renderCustomRowContent === next.renderCustomRowContent && | ||
| prev.renderGroupHeader === next.renderGroupHeader && | ||
| prev.renderGroupHeaderRowContent === next.renderGroupHeaderRowContent && | ||
| prev.getGroupTitle === next.getGroupTitle && | ||
| prev.groupHeaderClassName === next.groupHeaderClassName && | ||
| prev.attributes === next.attributes && | ||
| prev.cellAttributes === next.cellAttributes && | ||
| prev.rowVirtualizer === next.rowVirtualizer && | ||
| prev['aria-selected'] === next['aria-selected'] | ||
| ); | ||
| } | ||
|
|
||
| const BaseRowWithMemoCell = React.forwardRef(function BaseRowWithMemoCellRender< | ||
| TData, | ||
| TScrollElement extends Element | Window, | ||
| >(props: BaseRowProps<TData, TScrollElement>, ref: React.Ref<HTMLTableRowElement>) { | ||
| return <BaseRow {...props} Cell={MemoBaseCell} ref={ref} />; | ||
| }) as <TData, TScrollElement extends Element | Window = HTMLDivElement>( | ||
| props: BaseRowProps<TData, TScrollElement> & {ref?: React.Ref<HTMLTableRowElement>}, | ||
| ) => React.ReactElement; | ||
|
|
||
| export const MemoBaseRow = React.memo(BaseRowWithMemoCell, areEqual) as < | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't the new memoized components have |
||
| TData, | ||
| TScrollElement extends Element | Window = HTMLDivElement, | ||
| >( | ||
| props: MemoBaseRowProps<TData, TScrollElement> & {ref?: React.Ref<HTMLTableRowElement>}, | ||
| ) => React.ReactElement; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,8 @@ export interface BaseRowProps<TData, TScrollElement extends Element | Window = H | |
| | React.HTMLAttributes<HTMLTableRowElement> | ||
| | ((row: Row<TData>) => React.HTMLAttributes<HTMLTableRowElement>); | ||
| cellAttributes?: BaseCellProps<TData>['attributes']; | ||
| Cell?: React.FunctionComponent<BaseCellProps<TData>>; | ||
| _rowVersion?: readonly unknown[]; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this prop is not |
||
| } | ||
|
|
||
| export const BaseRow = React.forwardRef( | ||
|
|
@@ -61,13 +63,17 @@ export const BaseRow = React.forwardRef( | |
| virtualItem, | ||
| attributes: attributesProp, | ||
| cellAttributes, | ||
| Cell = BaseCell, | ||
| table: _, | ||
| _rowVersion, | ||
| ...restProps | ||
| }: BaseRowProps<TData, TScrollElement>, | ||
| ref: React.Ref<HTMLTableRowElement>, | ||
| ) => { | ||
| const rowRef = useForkRef(rowVirtualizer?.measureElement, ref); | ||
|
|
||
| const isSelected = row.getIsSelected(); | ||
|
|
||
| const attributes = | ||
| typeof attributesProp === 'function' ? attributesProp(row) : attributesProp; | ||
|
|
||
|
|
@@ -96,11 +102,12 @@ export const BaseRow = React.forwardRef( | |
| getGroupTitle, | ||
| }) | ||
| ) : ( | ||
| <BaseCell | ||
| <Cell | ||
| className={cellClassName} | ||
| colSpan={row.getVisibleCells().length} | ||
| attributes={cellAttributes} | ||
| aria-colindex={1} | ||
| _rowVersion={_rowVersion} | ||
| > | ||
| {renderGroupHeader ? ( | ||
| renderGroupHeader({ | ||
|
|
@@ -115,7 +122,7 @@ export const BaseRow = React.forwardRef( | |
| getGroupTitle={getGroupTitle} | ||
| /> | ||
| )} | ||
| </BaseCell> | ||
| </Cell> | ||
| ); | ||
| } | ||
|
|
||
|
|
@@ -126,12 +133,13 @@ export const BaseRow = React.forwardRef( | |
| return row | ||
| .getVisibleCells() | ||
| .map((cell) => ( | ||
| <BaseCell | ||
| <Cell | ||
| key={cell.id} | ||
| cell={cell} | ||
| className={cellClassName} | ||
| attributes={cellAttributes} | ||
| aria-colindex={cell.column.getIndex() + 1} | ||
| _rowVersion={_rowVersion} | ||
| /> | ||
| )); | ||
| }; | ||
|
|
@@ -142,7 +150,7 @@ export const BaseRow = React.forwardRef( | |
| className={b( | ||
| 'row', | ||
| { | ||
| selected: row.getIsSelected(), | ||
| selected: isSelected, | ||
| interactive: Boolean(onClick), | ||
| }, | ||
| className, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In MemoBaseRow aria-rowindex was removed from the comparator, but here it's still compared. Should the logic be the same in both components?