Skip to content

Commit ed4b9a7

Browse files
committed
Headers (ColTitles, RowTitles) and varying CellWidths
1 parent 30dc9e1 commit ed4b9a7

2 files changed

Lines changed: 86 additions & 3 deletions

File tree

src/components/NuGrid/NuGrid.css

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@
3535
font-size: 13px;
3636
}
3737

38+
/* Header styles */
39+
.nugrid-header-row {
40+
background-color: #f5f5f5;
41+
}
42+
43+
.nugrid-col-header,
44+
.nugrid-row-header {
45+
border: 1px solid #ccc;
46+
padding: 4px 8px;
47+
font-weight: 600;
48+
font-family: system-ui, -apple-system, sans-serif;
49+
font-size: 13px;
50+
background-color: #f5f5f5;
51+
text-align: center;
52+
overflow: hidden;
53+
text-overflow: ellipsis;
54+
white-space: nowrap;
55+
}
56+
57+
.nugrid-corner-cell {
58+
border: 1px solid #ccc;
59+
background-color: #f5f5f5;
60+
}
61+
3862
.nugrid-empty {
3963
display: flex;
4064
align-items: center;

src/components/NuGrid/index.jsx

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,31 @@ const NuGrid = ({ data }) => {
1515
Visible,
1616
Posn,
1717
Values,
18+
ColTitles,
19+
RowTitles,
20+
TitleWidth = 50,
21+
TitleHeight = 24,
22+
CellWidths = 100,
23+
CellHeights = 24,
1824
CSS,
1925
} = data?.Properties || {};
2026

27+
// Helper to get width for a column (scalar or per-column array)
28+
const getCellWidth = (colIndex) => {
29+
if (Array.isArray(CellWidths)) {
30+
return CellWidths[colIndex] ?? CellWidths[0] ?? 100;
31+
}
32+
return CellWidths || 100;
33+
};
34+
35+
// Helper to get height for a row (scalar or per-row array)
36+
const getCellHeight = (rowIndex) => {
37+
if (Array.isArray(CellHeights)) {
38+
return CellHeights[rowIndex] ?? CellHeights[0] ?? 24;
39+
}
40+
return CellHeights || 24;
41+
};
42+
2143
const customStyles = parseFlexStyles(CSS);
2244
const baseStyles = setStyle(data?.Properties);
2345

@@ -32,6 +54,12 @@ const NuGrid = ({ data }) => {
3254
...customStyles,
3355
};
3456

57+
// Normalize titles to arrays
58+
const colTitlesArray = ColTitles ? (Array.isArray(ColTitles) ? ColTitles : [ColTitles]) : [];
59+
const rowTitlesArray = RowTitles ? (Array.isArray(RowTitles) ? RowTitles : [RowTitles]) : [];
60+
const hasColTitles = colTitlesArray.length > 0;
61+
const hasRowTitles = rowTitlesArray.length > 0;
62+
3563
return (
3664
<div
3765
id={data?.ID}
@@ -41,21 +69,52 @@ const NuGrid = ({ data }) => {
4169
<div className="nugrid-container">
4270
{Values && Values.length > 0 ? (
4371
<table className="nugrid-table">
72+
{hasColTitles && (
73+
<thead>
74+
<tr className="nugrid-header-row" style={{ height: TitleHeight }}>
75+
{hasRowTitles && (
76+
<th
77+
className="nugrid-corner-cell"
78+
style={{ width: TitleWidth, height: TitleHeight }}
79+
/>
80+
)}
81+
{colTitlesArray.map((title, colIndex) => (
82+
<th
83+
key={colIndex}
84+
className="nugrid-col-header"
85+
style={{ width: getCellWidth(colIndex), height: TitleHeight }}
86+
>
87+
{title !== null && title !== undefined ? String(title) : ''}
88+
</th>
89+
))}
90+
</tr>
91+
</thead>
92+
)}
4493
<tbody>
4594
{Values.map((row, rowIndex) => (
46-
<tr key={rowIndex} className="nugrid-row">
95+
<tr key={rowIndex} className="nugrid-row" style={{ height: getCellHeight(rowIndex) }}>
96+
{hasRowTitles && (
97+
<th
98+
className="nugrid-row-header"
99+
style={{ width: TitleWidth, height: getCellHeight(rowIndex) }}
100+
>
101+
{rowTitlesArray[rowIndex] !== null && rowTitlesArray[rowIndex] !== undefined
102+
? String(rowTitlesArray[rowIndex])
103+
: ''}
104+
</th>
105+
)}
47106
{Array.isArray(row) ? (
48107
row.map((cell, colIndex) => (
49108
<td
50109
key={colIndex}
51110
className="nugrid-cell"
111+
style={{ width: getCellWidth(colIndex), height: getCellHeight(rowIndex) }}
52112
>
53113
{cell !== null && cell !== undefined ? String(cell) : ''}
54114
</td>
55115
))
56116
) : (
57-
// Handle case where row is a single value (1-column grid)
58-
<td className="nugrid-cell">
117+
<td className="nugrid-cell" style={{ width: getCellWidth(0), height: getCellHeight(rowIndex) }}>
59118
{row !== null && row !== undefined ? String(row) : ''}
60119
</td>
61120
)}

0 commit comments

Comments
 (0)