-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DOP-5110: Render sub rows for ListTable #1342
Changes from 7 commits
f1fa9f0
6595877
a33c946
b7e0202
48f87a9
2bfa335
c752c16
47c0714
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import React, { useMemo, useRef } from 'react'; | ||
import React, { useMemo, useRef, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { | ||
Cell, | ||
|
@@ -32,17 +32,24 @@ const styleTable = ({ customAlign, customWidth }) => css` | |
${customAlign && `text-align: ${align(customAlign)}`}; | ||
${customWidth && `width: ${customWidth}`}; | ||
margin: ${theme.size.medium} 0; | ||
|
||
tbody[data-expanded='true'] { | ||
// Avoid flash of light mode | ||
.dark-theme & { | ||
background-color: ${palette.gray.dark4}; | ||
} | ||
} | ||
`; | ||
|
||
const theadStyle = css` | ||
// Allows its box shadow to appear above stub cell's background color | ||
position: relative; | ||
color: var(--font-color-primary); | ||
background-color: ${palette.white}; | ||
// Allow nested tables to inherit background of the current row | ||
background-color: inherit; | ||
box-shadow: 0 ${theme.size.tiny} ${palette.gray.light2}; | ||
|
||
.dark-theme & { | ||
background-color: ${palette.black}; | ||
box-shadow: 0 ${theme.size.tiny} ${palette.gray.dark2}; | ||
} | ||
`; | ||
|
@@ -57,13 +64,14 @@ const baseCellStyle = css` | |
* { | ||
// Wrap in selector to ensure it cascades down to every element | ||
font-size: ${theme.fontSize.small} !important; | ||
line-height: inherit; | ||
line-height: 20px !important; | ||
} | ||
|
||
// Ensure each cell is no higher than the highest content in row | ||
& > div { | ||
height: unset; | ||
min-height: unset; | ||
max-height: unset; | ||
} | ||
`; | ||
|
||
|
@@ -72,29 +80,16 @@ const bodyCellStyle = css` | |
word-break: break-word; | ||
align-content: flex-start; | ||
|
||
& > div { | ||
min-height: unset; | ||
max-height: unset; | ||
flex-direction: column; | ||
align-items: flex-start; | ||
} | ||
|
||
*, | ||
p, | ||
a { | ||
line-height: 20px; | ||
} | ||
|
||
// Target any nested components (paragraphs, admonitions, tables) and any paragraphs within those nested components | ||
& > div > *, | ||
& > div > div p { | ||
margin: 0 0 12px; | ||
& > *, | ||
& > div p { | ||
margin: 0 0 12px !important; | ||
} | ||
|
||
// Prevent extra margin below last element (such as when multiple paragraphs are present) | ||
& > div > div *:last-child, | ||
& > div > *:last-child { | ||
margin-bottom: 0; | ||
& > div *:last-child, | ||
& > *:last-child { | ||
margin-bottom: 0 !important; | ||
} | ||
`; | ||
|
||
|
@@ -116,6 +111,14 @@ const stubCellStyle = css` | |
} | ||
`; | ||
|
||
const subRowStyle = css` | ||
// For some reason, collapsed subrows were causing the row to still take up space, | ||
// so this is our workaround for now | ||
&[aria-hidden='true'] { | ||
display: none; | ||
} | ||
`; | ||
|
||
const zebraStripingStyle = css` | ||
&:nth-of-type(even) { | ||
background-color: ${palette.gray.light3}; | ||
|
@@ -209,24 +212,46 @@ const generateColumns = (headerRow, bodyRows) => { | |
}); | ||
}; | ||
|
||
const generateRowsData = (bodyRowNodes, columns) => { | ||
const rowNodes = bodyRowNodes.map((node) => node?.children[0]?.children ?? []); | ||
const rows = rowNodes.map((rowNode) => { | ||
return rowNode.reduce((res, columnNode, colIndex) => { | ||
const column = columns[colIndex]; | ||
const generateRowsData = (bodyRowNodes, columns, isNested = false) => { | ||
// The shape of list-table's "rows" are slightly different from a traditional row directive, so we need to | ||
// account for that | ||
const rowNodes = isNested ? bodyRowNodes : bodyRowNodes.map((node) => node?.children[0]?.children ?? []); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we handle this in where we call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the children lookup will still have to exist (224-231) and it would be an improvement if we had the uniform structure vs (current mix of list+listitem and row), but the above comment can at least reduce the difference |
||
const rows = rowNodes.map((row) => { | ||
const res = {}; | ||
const cells = []; | ||
const nestedRows = []; | ||
|
||
const potentialCells = isNested ? row.children : row; | ||
for (const item of potentialCells) { | ||
if (item.type === 'directive' && item.name === 'row') { | ||
nestedRows.push(item); | ||
} else { | ||
cells.push(item); | ||
} | ||
} | ||
|
||
cells.forEach((cell, colIdx) => { | ||
const column = columns[colIdx]; | ||
if (!column) { | ||
console.warn(`Row has too many items (index ${colIndex}) for table with ${columns.length} columns`); | ||
console.warn(`Row has too many items (index ${colIdx}) for table with ${columns.length} columns`); | ||
return res; | ||
} | ||
res[column?.accessorKey ?? colIndex] = ( | ||
|
||
const skipPTag = hasOneChild(cell.children); | ||
res[column.accessorKey ?? colIdx] = ( | ||
<> | ||
{columnNode.children.map((cellNode, index) => ( | ||
<ComponentFactory key={index} nodeData={cellNode} /> | ||
{cell.children.map((contentNode, index) => ( | ||
<ComponentFactory key={index} nodeData={contentNode} skipPTag={skipPTag} /> | ||
))} | ||
</> | ||
); | ||
return res; | ||
}, {}); | ||
}); | ||
|
||
if (nestedRows.length > 0) { | ||
res['subRows'] = generateRowsData(nestedRows, columns, true); | ||
} | ||
|
||
return res; | ||
}); | ||
|
||
return rows; | ||
|
@@ -260,13 +285,17 @@ const ListTable = ({ nodeData: { children, options }, ...rest }) => { | |
const tableRef = useRef(); | ||
const columns = useMemo(() => generateColumns(headerRows[0], bodyRows), [bodyRows, headerRows]); | ||
const data = useMemo(() => generateRowsData(bodyRows, columns), [bodyRows, columns]); | ||
const [expanded, setExpanded] = useState(true); | ||
const table = useLeafyGreenTable({ | ||
containerRef: tableRef, | ||
columns: columns, | ||
data: data, | ||
state: { | ||
expanded, | ||
}, | ||
onExpandedChange: setExpanded, | ||
}); | ||
const { rows } = table.getRowModel(); | ||
|
||
const columnCount = columns.length; | ||
|
||
let widths = null; | ||
|
@@ -285,6 +314,7 @@ const ListTable = ({ nodeData: { children, options }, ...rest }) => { | |
<div className="header-buffer" key={id} id={id} /> | ||
))} | ||
<Table | ||
table={table} | ||
className={cx( | ||
styleTable({ | ||
customAlign: options?.align, | ||
|
@@ -322,11 +352,24 @@ const ListTable = ({ nodeData: { children, options }, ...rest }) => { | |
const isStub = colIndex <= stubColumnCount - 1; | ||
const role = isStub ? 'rowheader' : null; | ||
return ( | ||
<Cell key={cell.id} className={cx(baseCellStyle, bodyCellStyle, isStub && stubCellStyle)} role={role}> | ||
{cell.renderValue()} | ||
<Cell key={cell.id} className={cx(baseCellStyle, isStub && stubCellStyle)} role={role}> | ||
<div className={cx(bodyCellStyle)}>{cell.renderValue()}</div> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had to wrap cell content within a new |
||
</Cell> | ||
); | ||
})} | ||
|
||
{row.subRows && | ||
row.subRows.map((subRow) => ( | ||
<Row key={subRow.id} row={subRow} className={cx(subRowStyle)}> | ||
{subRow.getVisibleCells().map((cell) => { | ||
return ( | ||
<Cell key={cell.id} className={cx(baseCellStyle)}> | ||
<div className={cx(bodyCellStyle)}>{cell.renderValue()}</div> | ||
</Cell> | ||
); | ||
})} | ||
</Row> | ||
))} | ||
</Row> | ||
))} | ||
</TableBody> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️