Skip to content

Commit 0148e8a

Browse files
committed
🐛 Fixes the amount of width
1 parent efa51be commit 0148e8a

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

package/DataTable.tsx

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,27 @@ export function DataTable<T>({
191191
return;
192192
}
193193

194-
const updates = headerCells
194+
let measured = headerCells
195195
.map((cell) => {
196196
const accessor = cell.getAttribute('data-accessor');
197197
if (!accessor || accessor === '__selection__') return null;
198-
const width = Math.ceil(cell.getBoundingClientRect().width);
199-
return { accessor, width: `${width}px` } as const;
198+
const width = Math.round(cell.getBoundingClientRect().width);
199+
return { accessor, width } as const;
200200
})
201-
.filter(Boolean) as Array<{ accessor: string; width: string }>;
201+
.filter(Boolean) as Array<{ accessor: string; width: number }>;
202+
203+
const viewport = refs.scrollViewport.current;
204+
const viewportWidth = viewport?.clientWidth ?? 0;
205+
if (viewportWidth && measured.length) {
206+
const total = measured.reduce((acc, u) => acc + u.width, 0);
207+
const overflow = total - viewportWidth;
208+
if (overflow > 0) {
209+
const last = measured[measured.length - 1];
210+
last.width = Math.max(50, last.width - overflow);
211+
}
212+
}
213+
214+
const updates = measured.map((m) => ({ accessor: m.accessor, width: `${m.width}px` }));
202215

203216
setTimeout(() => {
204217
if (updates.length) dragToggle.setMultipleColumnWidths(updates);
@@ -233,14 +246,27 @@ export function DataTable<T>({
233246

234247
const headerCells = Array.from(thead.querySelectorAll<HTMLTableCellElement>('th[data-accessor]'));
235248

236-
const updates = headerCells
249+
let measured = headerCells
237250
.map((cell) => {
238251
const accessor = cell.getAttribute('data-accessor');
239252
if (!accessor || accessor === '__selection__') return null;
240-
const width = Math.ceil(cell.getBoundingClientRect().width);
241-
return { accessor, width: `${width}px` } as const;
253+
const width = Math.round(cell.getBoundingClientRect().width);
254+
return { accessor, width } as const;
242255
})
243-
.filter(Boolean) as Array<{ accessor: string; width: string }>;
256+
.filter(Boolean) as Array<{ accessor: string; width: number }>;
257+
258+
const viewport = refs.scrollViewport.current;
259+
const viewportWidth = viewport?.clientWidth ?? 0;
260+
if (viewportWidth && measured.length) {
261+
const total = measured.reduce((acc, u) => acc + u.width, 0);
262+
const overflow = total - viewportWidth;
263+
if (overflow > 0) {
264+
const last = measured[measured.length - 1];
265+
last.width = Math.max(50, last.width - overflow);
266+
}
267+
}
268+
269+
const updates = measured.map((m) => ({ accessor: m.accessor, width: `${m.width}px` }));
244270

245271
setTimeout(() => {
246272
if (updates.length) dragToggle.setMultipleColumnWidths(updates);

0 commit comments

Comments
 (0)