Skip to content

Commit

Permalink
fix: Make table sticky columns util support columns unmount (#1542)
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-kot authored Sep 13, 2023
1 parent cfd28c9 commit 03e59f2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/table/sticky-columns/__tests__/use-sticky-columns.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
StickyColumnsModel,
useStickyCellStyles,
} from '../../../../lib/components/table/sticky-columns';
import { updateCellOffsets } from '../../../../lib/components/table/sticky-columns/utils';
import { getStickyClassNames } from '../../../../lib/components/table/utils';
import { renderHook } from '../../../__tests__/render-hook';

Expand Down Expand Up @@ -321,3 +322,12 @@ describe('getStickyClassNames helper', () => {
});
});
});

test('updateCellOffsets element widths fallback to 0 when elements are missing', () => {
const { offsets } = updateCellOffsets(
{},
{ stickyColumnsFirst: 1, stickyColumnsLast: 1, visibleColumns: ['a', 'b', 'c'] }
);
expect(offsets.get('a')).toEqual({ first: 0, last: 0 });
expect(offsets.get('c')).toEqual({ first: 0, last: 0 });
});
4 changes: 2 additions & 2 deletions src/table/sticky-columns/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export function updateCellOffsets(cells: Record<PropertyKey, HTMLElement>, props
const firstColumnsWidths: number[] = [];
for (let i = 0; i < Math.min(totalColumns, props.stickyColumnsFirst); i++) {
const element = cells[props.visibleColumns[i]];
const cellWidth = element.getBoundingClientRect().width ?? 0;
const cellWidth = element?.getBoundingClientRect().width ?? 0;
firstColumnsWidths[i] = (firstColumnsWidths[i - 1] ?? 0) + cellWidth;
}

const lastColumnsWidths: number[] = [];
for (let i = 0; i < Math.min(totalColumns, props.stickyColumnsLast); i++) {
const element = cells[props.visibleColumns[totalColumns - 1 - i]];
const cellWidth = element.getBoundingClientRect().width ?? 0;
const cellWidth = element?.getBoundingClientRect().width ?? 0;
lastColumnsWidths[i] = (lastColumnsWidths[i - 1] ?? 0) + cellWidth;
}

Expand Down

0 comments on commit 03e59f2

Please sign in to comment.