-
Notifications
You must be signed in to change notification settings - Fork 64
Add table spanning support #420
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
Changes from 4 commits
ae12e5b
5988fc2
3c514d4
a51c311
62d483f
0d675c1
d01e537
2ef92f4
e0cfb3f
48e10de
9224354
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 |
---|---|---|
|
@@ -100,29 +100,35 @@ function renderNode(createElement, references) { | |
)) | ||
)); | ||
|
||
const renderTableChildren = (rows, headerStyle = TableHeaderStyle.none) => { | ||
const renderTableCell = ( | ||
element, attrs, data, cellIndex, rowIndex, extendedData, | ||
) => { | ||
const { colspan, rowspan } = extendedData[`${rowIndex}_${cellIndex}`] || {}; | ||
// if either is `0`, then its spanned over and should not be rendered | ||
if (colspan === 0 || rowspan === 0) return null; | ||
return createElement(element, { attrs: { ...attrs, colspan, rowspan } }, ( | ||
renderChildren(data) | ||
)); | ||
}; | ||
|
||
const renderTableChildren = (rows, headerStyle = TableHeaderStyle.none, extendedData = {}) => { | ||
// build the matrix for the array | ||
switch (headerStyle) { | ||
// thead with first row and th for each first row cell | ||
// tbody with rows where first cell in each row is th, others are td | ||
case TableHeaderStyle.both: { | ||
const [firstRow, ...otherRows] = rows; | ||
return [ | ||
createElement('thead', {}, [ | ||
createElement('tr', {}, firstRow.map(cell => ( | ||
createElement('th', { attrs: { scope: 'col' } }, ( | ||
renderChildren(cell) | ||
)) | ||
createElement('tr', {}, firstRow.map((cell, ci) => ( | ||
renderTableCell('th', { scope: 'col' }, cell, ci, 0, extendedData) | ||
))), | ||
]), | ||
createElement('tbody', {}, otherRows.map(([firstCell, ...otherCells]) => ( | ||
createElement('tbody', {}, otherRows.map(([firstCell, ...otherCells], ri) => ( | ||
dobromir-hristov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
createElement('tr', {}, [ | ||
createElement('th', { attrs: { scope: 'row' } }, ( | ||
renderChildren(firstCell) | ||
)), | ||
...otherCells.map(cell => ( | ||
createElement('td', {}, ( | ||
renderChildren(cell) | ||
)) | ||
renderTableCell('th', { scope: 'row' }, firstCell, 0, ri + 1, extendedData), | ||
...otherCells.map((cell, ci) => ( | ||
renderTableCell('td', {}, cell, ci + 1, ri + 1, extendedData) | ||
)), | ||
]) | ||
))), | ||
|
@@ -131,15 +137,11 @@ function renderNode(createElement, references) { | |
// tbody with rows, th for first cell of each row, td for other cells | ||
case TableHeaderStyle.column: | ||
return [ | ||
createElement('tbody', {}, rows.map(([firstCell, ...otherCells]) => ( | ||
createElement('tbody', {}, rows.map(([firstCell, ...otherCells], ri) => ( | ||
createElement('tr', {}, [ | ||
createElement('th', { attrs: { scope: 'row' } }, ( | ||
renderChildren(firstCell) | ||
)), | ||
...otherCells.map(cell => ( | ||
createElement('td', {}, ( | ||
renderChildren(cell) | ||
)) | ||
renderTableCell('th', { scope: 'row' }, firstCell, 0, ri, extendedData), | ||
...otherCells.map((cell, ci) => ( | ||
renderTableCell('td', {}, cell, ci + 1, ri, extendedData) | ||
)), | ||
]) | ||
))), | ||
|
@@ -150,17 +152,13 @@ function renderNode(createElement, references) { | |
const [firstRow, ...otherRows] = rows; | ||
return [ | ||
createElement('thead', {}, [ | ||
createElement('tr', {}, firstRow.map(cell => ( | ||
createElement('th', { attrs: { scope: 'col' } }, ( | ||
renderChildren(cell) | ||
)) | ||
createElement('tr', {}, firstRow.map((cell, ci) => renderTableCell( | ||
'th', { scope: 'col' }, cell, ci, 0, extendedData, | ||
))), | ||
]), | ||
createElement('tbody', {}, otherRows.map(row => ( | ||
createElement('tr', {}, row.map(cell => ( | ||
createElement('td', {}, ( | ||
renderChildren(cell) | ||
)) | ||
createElement('tbody', {}, otherRows.map((row, ri) => ( | ||
createElement('tr', {}, row.map((cell, cellIndex) => ( | ||
renderTableCell('td', {}, cell, cellIndex, ri + 1, extendedData) | ||
))) | ||
))), | ||
]; | ||
|
@@ -169,12 +167,10 @@ function renderNode(createElement, references) { | |
// tbody with all rows and every cell is td | ||
return [ | ||
createElement('tbody', {}, ( | ||
rows.map(row => ( | ||
rows.map((row, ri) => ( | ||
createElement('tr', {}, ( | ||
row.map(cell => ( | ||
createElement('td', {}, ( | ||
renderChildren(cell) | ||
)) | ||
row.map((cell, ci) => ( | ||
renderTableCell('td', {}, cell, ci, ri, extendedData) | ||
)) | ||
)) | ||
)) | ||
|
@@ -252,8 +248,12 @@ function renderNode(createElement, references) { | |
return renderFigure(node); | ||
} | ||
|
||
return createElement(Table, {}, ( | ||
renderTableChildren(node.rows, node.header) | ||
return createElement(Table, { | ||
props: { | ||
spanned: !!node.extendedData, | ||
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. Is this still needed since the class isn't being used anymore? 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. Probably not, just thought it might help future devs, if they want to style these tables in some way with custom styling 🤔 |
||
}, | ||
}, ( | ||
renderTableChildren(node.rows, node.header, node.extendedData) | ||
)); | ||
case BlockType.termList: | ||
return createElement('dl', {}, node.items.map(({ term, definition }) => [ | ||
|
Uh oh!
There was an error while loading. Please reload this page.