Skip to content

Commit

Permalink
Use transformation to provide isHeader property
Browse files Browse the repository at this point in the history
  • Loading branch information
kudlajz committed Jul 23, 2024
1 parent f12256f commit 0b4aaf7
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 67 deletions.
24 changes: 3 additions & 21 deletions src/elements/Table/Table.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@
import type { TableCellNode, TableNode } from '@prezly/story-content-format';
import type { TableNode } from '@prezly/story-content-format';
import classNames from 'classnames';
import { Children, cloneElement, type HTMLAttributes, isValidElement } from 'react';

import { isHeaderCell } from './TableContext';
import type { HTMLAttributes } from 'react';

interface Props extends HTMLAttributes<HTMLTableElement> {
node: TableNode;
}

export function Table({ children, node }: Props) {
function isHeader(cell: TableCellNode): boolean {
return isHeaderCell(node, cell);
}

return (
<table
className={classNames('prezly-slate-table', {
'prezly-slate-table--withBorders': node.border,
})}
>
<tbody>
{Children.map(children, (child) => {
if (isValidElement(child)) {
return cloneElement<{ isHeader?: (node: TableCellNode) => boolean }>(
// @ts-expect-error I don't know how to type this :|
child,
{ isHeader },
);
}

return child;
})}
</tbody>
<tbody>{children}</tbody>
</table>
);
}
10 changes: 4 additions & 6 deletions src/elements/Table/TableCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ import classNames from 'classnames';
import type { HTMLAttributes } from 'react';

interface Props extends HTMLAttributes<HTMLTableCellElement> {
isHeader?: (node: TableCellNode) => boolean;
node: TableCellNode;
node: TableCellNode & { isHeader?: boolean };
}

export function TableCell({ children, isHeader, node }: Props) {
const isHeaderCell = isHeader?.(node);
const Cell = isHeaderCell ? 'th' : 'td';
export function TableCell({ children, node }: Props) {
const Cell = node.isHeader ? 'th' : 'td';

return (
<Cell
className={classNames('prezly-slate-table-cell', {
'prezly-slate-table-cell--header': isHeaderCell,
'prezly-slate-table-cell--header': node.isHeader,
})}
colSpan={node.colspan}
rowSpan={node.rowspan}
Expand Down
20 changes: 0 additions & 20 deletions src/elements/Table/TableContext.tsx

This file was deleted.

23 changes: 3 additions & 20 deletions src/elements/Table/TableRow.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import type { TableCellNode, TableRowNode } from '@prezly/story-content-format';
import type { TableRowNode } from '@prezly/story-content-format';
import type { HTMLAttributes } from 'react';
import { Children, cloneElement, isValidElement } from 'react';

import { TableCell as TableCellComponent } from './TableCell';

interface Props extends HTMLAttributes<HTMLTableCellElement> {
isHeader?: (node: TableCellNode) => boolean;
node: TableRowNode;
}

export function TableRow({ children, isHeader }: Props) {
return (
<tr className="prezly-slate-table-row">
{Children.map(children, (child) => {
if (isValidElement(child) && child.type === TableCellComponent) {
// @ts-expect-error I don't know how to type this :|
return cloneElement<{ isHeader?: (node: TableCellNode) => boolean }>(child, {
isHeader,
});
}

return child;
})}
</tr>
);
export function TableRow({ children }: Props) {
return <tr className="prezly-slate-table-row">{children}</tr>;
}
1 change: 1 addition & 0 deletions src/transformations/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { preventEmptyBlockCollapsing } from './preventEmptyBlockCollapsing';
export { preventWhitespaceCollapsing } from './preventWhitepaceCollapsing';
export { provideIsHeaderToTableCells } from './provideIsHeaderToTableCells';
30 changes: 30 additions & 0 deletions src/transformations/provideIsHeaderToTableCells.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { type Node, TableNode } from '@prezly/story-content-format';

export function provideIsHeaderToTableCells<T extends Node>(node: T): T {
if (TableNode.isTableNode(node)) {
const isHeaderFirstRow =
node.header &&
node.header.length === 1 &&
node.header[0] === TableNode.TableHeader.FIRST_ROW;

const isHeaderFirstColumn =
node.header &&
node.header.length === 1 &&
node.header[0] === TableNode.TableHeader.FIRST_COLUMN;

return {
...node,
children: node.children.map((row, rowIndex) => ({
...row,
children: row.children.map((cell, cellIndex) => ({
...cell,
isHeader:
(isHeaderFirstRow && rowIndex === 0) ||
(isHeaderFirstColumn && cellIndex === 0),
})),
})),
};
}

return node;
}

0 comments on commit 0b4aaf7

Please sign in to comment.