Align tree grid header/footer padding + add initialExpanded prop
- Header and footer rows now use same CELL_PX padding as data rows, fixing horizontal misalignment of column values - New initialExpanded prop: 'roots' (default), 'all', 'none', or Set<number> for specific node IDs Made-with: Cursor
This commit is contained in:
@@ -19,6 +19,8 @@ export interface AccountTreeGridProps {
|
||||
title?: string
|
||||
onDownloadCsv?: () => void
|
||||
reverseSign?: boolean
|
||||
/** Which nodes start expanded. 'roots' (default) = top-level only, 'all' = everything, 'none' = all collapsed, or a Set of node IDs */
|
||||
initialExpanded?: 'roots' | 'all' | 'none' | Set<number>
|
||||
}
|
||||
|
||||
interface BuiltNode {
|
||||
@@ -86,14 +88,26 @@ function formatValue(cents: number, reverseSign: boolean): string {
|
||||
|
||||
export default function AccountTreeGrid({
|
||||
nodes, columnHeaders, title, onDownloadCsv, reverseSign = false,
|
||||
initialExpanded = 'roots',
|
||||
}: AccountTreeGridProps) {
|
||||
const colCount = columnHeaders.length
|
||||
const tree = useMemo(() => buildTree(nodes, colCount), [nodes, colCount])
|
||||
|
||||
const [expanded, setExpanded] = useState<Set<number>>(() => {
|
||||
const initial = new Set<number>()
|
||||
for (const root of tree) if (!root.isLeaf) initial.add(root.id)
|
||||
return initial
|
||||
if (initialExpanded instanceof Set) return new Set(initialExpanded)
|
||||
if (initialExpanded === 'none') return new Set<number>()
|
||||
|
||||
const result = new Set<number>()
|
||||
function collect(nodes: BuiltNode[], depth: number) {
|
||||
for (const n of nodes) {
|
||||
if (!n.isLeaf) {
|
||||
if (initialExpanded === 'all' || depth === 0) result.add(n.id)
|
||||
if (initialExpanded === 'all') collect(n.children, depth + 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
collect(tree, 0)
|
||||
return result
|
||||
})
|
||||
|
||||
const toggleExpand = useCallback((id: number) => {
|
||||
@@ -159,9 +173,9 @@ export default function AccountTreeGrid({
|
||||
</colgroup>
|
||||
<TableHead>
|
||||
<TableRow sx={{ bgcolor: 'primary.main' }}>
|
||||
<TableCell sx={{ color: 'white', fontWeight: 600 }}>Account</TableCell>
|
||||
<TableCell sx={{ p: `${CELL_PX} !important`, color: 'white', fontWeight: 600 }}>Account</TableCell>
|
||||
{columnHeaders.map((h, i) => (
|
||||
<TableCell key={i} align="right" sx={{ color: 'white', fontWeight: 600 }}>{h}</TableCell>
|
||||
<TableCell key={i} sx={{ p: `${CELL_PX} !important`, textAlign: 'right', color: 'white', fontWeight: 600 }}>{h}</TableCell>
|
||||
))}
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
@@ -199,9 +213,10 @@ export default function AccountTreeGrid({
|
||||
})}
|
||||
|
||||
<TableRow sx={{ bgcolor: 'grey.100', borderTop: '2px solid', borderColor: 'divider' }}>
|
||||
<TableCell sx={{ fontWeight: 700, fontSize: 13 }}>Total</TableCell>
|
||||
<TableCell sx={{ p: `${CELL_PX} !important`, fontWeight: 700, fontSize: 13 }}>Total</TableCell>
|
||||
{totals.map((val, i) => (
|
||||
<TableCell key={i} align="right" sx={{
|
||||
<TableCell key={i} sx={{
|
||||
p: `${CELL_PX} !important`, textAlign: 'right',
|
||||
fontWeight: 700, fontFamily: '"Roboto Mono", monospace', fontSize: 13,
|
||||
color: (reverseSign ? -val : val) < 0 ? 'error.main' : 'text.primary',
|
||||
}}>
|
||||
|
||||
Reference in New Issue
Block a user