Skip to content

Commit 5762c69

Browse files
Copilothotlong
andcommitted
Fix dynamic Tailwind class names and add colSpan validation in ReportViewer
Co-authored-by: hotlong <[email protected]>
1 parent ce48257 commit 5762c69

File tree

1 file changed

+70
-34
lines changed

1 file changed

+70
-34
lines changed

packages/ui/src/components/report/ReportViewer.tsx

Lines changed: 70 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -152,28 +152,46 @@ function TabularReportView({
152152
<table className="min-w-full bg-white border">
153153
<thead className="bg-gray-50 border-b">
154154
<tr>
155-
{visibleColumns.map((column, index) => (
156-
<th
157-
key={index}
158-
className={`px-4 py-3 text-${column.align || 'left'} text-xs font-medium text-gray-700 uppercase tracking-wider`}
159-
style={{ width: column.width }}
160-
>
161-
{column.label}
162-
</th>
163-
))}
155+
{visibleColumns.map((column, index) => {
156+
const alignmentClass =
157+
column.align === 'center'
158+
? 'text-center'
159+
: column.align === 'right'
160+
? 'text-right'
161+
: 'text-left';
162+
163+
return (
164+
<th
165+
key={index}
166+
className={`px-4 py-3 text-xs font-medium text-gray-700 uppercase tracking-wider ${alignmentClass}`}
167+
style={{ width: column.width }}
168+
>
169+
{column.label}
170+
</th>
171+
);
172+
})}
164173
</tr>
165174
</thead>
166175
<tbody className="divide-y divide-gray-200">
167176
{data.map((row, rowIndex) => (
168177
<tr key={rowIndex} className="hover:bg-gray-50">
169-
{visibleColumns.map((column, colIndex) => (
170-
<td
171-
key={colIndex}
172-
className={`px-4 py-3 text-sm text-${column.align || 'left'}`}
173-
>
174-
{getNestedValue(row, column.field)}
175-
</td>
176-
))}
178+
{visibleColumns.map((column, colIndex) => {
179+
const alignmentClass =
180+
column.align === 'center'
181+
? 'text-center'
182+
: column.align === 'right'
183+
? 'text-right'
184+
: 'text-left';
185+
186+
return (
187+
<td
188+
key={colIndex}
189+
className={`px-4 py-3 text-sm ${alignmentClass}`}
190+
>
191+
{getNestedValue(row, column.field)}
192+
</td>
193+
);
194+
})}
177195
</tr>
178196
))}
179197
</tbody>
@@ -215,34 +233,52 @@ function SummaryReportView({
215233
<table className="min-w-full bg-white">
216234
<thead className="bg-gray-50 border-b">
217235
<tr>
218-
{report.columns.map((column, index) => (
219-
<th
220-
key={index}
221-
className={`px-4 py-2 text-${column.align || 'left'} text-xs font-medium text-gray-700`}
222-
>
223-
{column.label}
224-
</th>
225-
))}
236+
{report.columns.map((column, index) => {
237+
const alignmentClass =
238+
column.align === 'right'
239+
? 'text-right'
240+
: column.align === 'center'
241+
? 'text-center'
242+
: 'text-left';
243+
244+
return (
245+
<th
246+
key={index}
247+
className={`px-4 py-2 text-xs font-medium text-gray-700 ${alignmentClass}`}
248+
>
249+
{column.label}
250+
</th>
251+
);
252+
})}
226253
</tr>
227254
</thead>
228255
<tbody className="divide-y divide-gray-200">
229256
{(groupRows as any[]).map((row, rowIndex) => (
230257
<tr key={rowIndex} className="hover:bg-gray-50">
231-
{report.columns.map((column, colIndex) => (
232-
<td
233-
key={colIndex}
234-
className={`px-4 py-2 text-sm text-${column.align || 'left'}`}
235-
>
236-
{getNestedValue(row, column.field)}
237-
</td>
238-
))}
258+
{report.columns.map((column, colIndex) => {
259+
const alignmentClass =
260+
column.align === 'right'
261+
? 'text-right'
262+
: column.align === 'center'
263+
? 'text-center'
264+
: 'text-left';
265+
266+
return (
267+
<td
268+
key={colIndex}
269+
className={`px-4 py-2 text-sm ${alignmentClass}`}
270+
>
271+
{getNestedValue(row, column.field)}
272+
</td>
273+
);
274+
})}
239275
</tr>
240276
))}
241277
</tbody>
242278
{report.aggregations && (
243279
<tfoot className="bg-gray-50 border-t font-semibold">
244280
<tr>
245-
<td className="px-4 py-2 text-sm" colSpan={report.columns.length - report.aggregations.length}>
281+
<td className="px-4 py-2 text-sm" colSpan={Math.max(1, report.columns.length - report.aggregations.length)}>
246282
Subtotal
247283
</td>
248284
{report.aggregations.map((agg, index) => (

0 commit comments

Comments
 (0)