Skip to content

Commit a8b75e1

Browse files
Merge pull request #827 from buildo/fixes_to_table
Search lastStickyColumnIndex by column id if set; search headers only inside tableContainer
2 parents f4da2f5 + ef9a7de commit a8b75e1

File tree

1 file changed

+9
-10
lines changed
  • packages/bento-design-system/src/Table

1 file changed

+9
-10
lines changed

packages/bento-design-system/src/Table/Table.tsx

+9-10
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export function Table<
231231

232232
// Determine the id of the last left sticky column (used to draw a visual separator in the UI)
233233
const lastStickyColumnIndex = flatColumns
234-
.map((c) => c.accessor)
234+
.map((c) => c.id ?? c.accessor)
235235
.indexOf(stickyLeftColumnsIds[stickyLeftColumnsIds.length - 1]);
236236

237237
// Keep a style object for each sticky column, which will be updated by the useLayoutEffect below
@@ -242,23 +242,24 @@ export function Table<
242242
// Keep a state for the height of the first row of headers, which will be updated by the useLayoutEffect below
243243
const [stickyHeaderHeight, setStickyHeaderHeight] = useState(0);
244244

245+
const tableContainerRef = useRef<HTMLDivElement>(null);
246+
const getHeaderById = (id: string) =>
247+
// Search header only inside the table container, to avoid selecting headers from other tables
248+
tableContainerRef.current!.querySelector(`[id='header-cell-${id}']`);
249+
245250
/** Get the width of each sticky column (using the header width as reference) and use it to set the `left` of each sticky column.
246251
* Each sticky column must have as `left` the total width of the previous sticky columns.
247252
*/
248253
useLayoutEffect(() => {
249254
// Make this computation only if we have any data, because headers are not rendered when there are no rows
250255
// and we need them to get the column width.
251256
if (data.length > 0) {
252-
const columnWidths = stickyLeftColumnsIds.map(
253-
(id) => document.getElementById(`header-cell-${id}`)!.clientWidth
254-
);
257+
const columnWidths = stickyLeftColumnsIds.map((id) => getHeaderById(id)!.clientWidth);
255258
const columnGroupWidths = stickyLeftColumnGroupsIds.map(
256-
(id) => document.getElementById(`header-cell-${id}`)!.clientWidth
259+
(id) => getHeaderById(id)!.clientWidth
257260
);
258261
const columnGroupHeight = Math.max(
259-
...headerGroups[0].headers.map((h) =>
260-
h.columns ? document.getElementById(`header-cell-${h.id}`)!.clientHeight : 0
261-
)
262+
...headerGroups[0].headers.map((h) => (h.columns ? getHeaderById(h.id)!.clientHeight : 0))
262263
);
263264

264265
const styleReducer =
@@ -304,8 +305,6 @@ export function Table<
304305

305306
const flatRows = rows.flatMap((row) => (row.isGrouped ? [row, ...row.leafRows] : [row]));
306307

307-
const tableContainerRef = useRef<HTMLDivElement>(null);
308-
309308
const virtualizeRows =
310309
typeof virtualizeRowsConfig === "boolean" ? virtualizeRowsConfig : virtualizeRowsConfig != null;
311310
const estimateSize =

0 commit comments

Comments
 (0)