Skip to content

Commit d460026

Browse files
author
Hweinstock
committed
fix(tui): zip column width into column data structure to avoid undefined assertions
1 parent 42be1f3 commit d460026

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

src/components/ui/data-table/DataTable.tsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,14 @@ export function DataTable<T extends Record<string, unknown>>({
154154
{ isActive: focus },
155155
);
156156

157-
// Column widths
158-
const colWidths = columns.map((col) => {
159-
if (col.width) return col.width;
157+
// default the width of each column
158+
const columnsWithWidths = columns.map((col) => {
159+
if (col.width !== undefined) return { ...col, width: col.width };
160160
const maxData = pageData.reduce((max, row) => {
161161
const val = col.render ? col.render(row[col.key], row) : String(row[col.key] ?? "");
162162
return Math.max(max, val.length);
163163
}, 0);
164-
return Math.max(col.header.length + 2, maxData + 2, 6);
164+
return { ...col, width: Math.max(col.header.length + 2, maxData + 2, 6) };
165165
});
166166

167167
const pad = (s: string, w: number, align: "left" | "center" | "right" = "left") => {
@@ -214,12 +214,12 @@ export function DataTable<T extends Record<string, unknown>>({
214214
{/* Header */}
215215
<Box flexDirection="row">
216216
{selectable && <Text color={theme.colors.muted}>{" "}</Text>}
217-
{columns.map((col, i) => {
217+
{columnsWithWidths.map((col) => {
218218
const sortArrow = sortColumn === col.key ? (sortDirection === "asc" ? " ▲" : " ▼") : "";
219219
return (
220-
<Box key={col.key} width={colWidths[i]}>
220+
<Box key={col.key} width={col.width}>
221221
<Text bold color={theme.colors.primary}>
222-
{pad(col.header + sortArrow, colWidths[i], col.align)}
222+
{pad(col.header + sortArrow, col.width, col.align)}
223223
</Text>
224224
</Box>
225225
);
@@ -230,7 +230,9 @@ export function DataTable<T extends Record<string, unknown>>({
230230
<Box flexDirection="row">
231231
<Text color={theme.colors.border}>
232232
{showDivider
233-
? "─".repeat(colWidths.reduce((a, b) => a + b, 0) + (selectable ? 2 : 0))
233+
? "─".repeat(
234+
columnsWithWidths.reduce((acc, col) => acc + col.width, 0) + (selectable ? 2 : 0),
235+
)
234236
: " "}
235237
</Text>
236238
</Box>
@@ -250,17 +252,17 @@ export function DataTable<T extends Record<string, unknown>>({
250252
{isSelected ? "❯ " : " "}
251253
</Text>
252254
)}
253-
{columns.map((col, ci) => {
255+
{columnsWithWidths.map((col) => {
254256
const val = col.render
255257
? col.render(row[col.key], row)
256258
: String(row[col.key] ?? "");
257259
return (
258-
<Box key={col.key} width={colWidths[ci]}>
260+
<Box key={col.key} width={col.width}>
259261
<Text
260262
color={isSelected ? theme.colors.text : theme.colors.muted}
261263
bold={isSelected}
262264
>
263-
{pad(val, colWidths[ci], col.align)}
265+
{pad(val, col.width, col.align)}
264266
</Text>
265267
</Box>
266268
);

0 commit comments

Comments
 (0)