-
Notifications
You must be signed in to change notification settings - Fork 41
feat: add PrismicTable component #219
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b9cebec
chore: update prismic deps
levimykel c9c0d97
feat: support PrismicTable
levimykel 7ad5a55
test: add tests for PrismicTable
levimykel 88f60b3
refactor: updates from PR review
levimykel 5d43f00
feat: remove `tableComponents` prop (#220)
angeloashmore 06419d8
docs: add TSDocs to PrismicTable
levimykel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { isFilled } from "@prismicio/client"; | ||
import { PrismicTable } from "@prismicio/react"; | ||
import assert from "assert"; | ||
|
||
import { createClient } from "@/prismicio"; | ||
|
||
export default async function Page() { | ||
const client = await createClient(); | ||
const { data: tests } = await client.getSingle("table_test"); | ||
|
||
assert(isFilled.table(tests.filled)); | ||
assert(!isFilled.table(tests.empty)); | ||
|
||
return ( | ||
<> | ||
<div data-testid="filled"> | ||
<PrismicTable field={tests.filled} /> | ||
</div> | ||
|
||
<div data-testid="empty"> | ||
<PrismicTable field={tests.empty} /> | ||
</div> | ||
|
||
<div data-testid="fallback"> | ||
<PrismicTable field={tests.empty} fallback={<div>Table</div>} /> | ||
</div> | ||
|
||
<div data-testid="custom-table"> | ||
<PrismicTable | ||
field={tests.filled} | ||
components={{ | ||
table: ({ children }) => <div className="table">{children}</div>, | ||
angeloashmore marked this conversation as resolved.
Show resolved
Hide resolved
|
||
thead: ({ children }) => <div className="head">{children}</div>, | ||
tbody: ({ children }) => <div className="body">{children}</div>, | ||
tr: ({ children }) => <div className="row">{children}</div>, | ||
th: ({ children }) => <div className="header">{children}</div>, | ||
td: ({ children }) => <div className="data">{children}</div>, | ||
}} | ||
/> | ||
</div> | ||
|
||
<div data-testid="custom-cell-content"> | ||
<PrismicTable | ||
field={tests.filled} | ||
components={{ | ||
table: ({ children }) => ( | ||
<table className="table">{children}</table> | ||
), | ||
paragraph: ({ children }) => ( | ||
<p className="paragraph">{children}</p> | ||
), | ||
strong: ({ children }) => ( | ||
<span className="strong">{children}</span> | ||
), | ||
em: ({ children }) => <span className="em">{children}</span>, | ||
}} | ||
/> | ||
</div> | ||
</> | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { ReactNode } from "react"; | ||
import { | ||
isFilled, | ||
TableField, | ||
TableFieldHead, | ||
TableFieldHeadRow, | ||
TableFieldBody, | ||
TableFieldBodyRow, | ||
TableFieldHeaderCell, | ||
TableFieldDataCell, | ||
} from "@prismicio/client"; | ||
|
||
import { JSXMapSerializer, PrismicRichText } from "./PrismicRichText.js"; | ||
|
||
type TableComponents = { | ||
table?: (props: { | ||
table: TableField<"filled">; | ||
children: ReactNode; | ||
}) => ReactNode; | ||
thead?: (props: { head: TableFieldHead; children: ReactNode }) => ReactNode; | ||
tbody?: (props: { body: TableFieldBody; children: ReactNode }) => ReactNode; | ||
tr?: (props: { | ||
row: TableFieldHeadRow | TableFieldBodyRow; | ||
children: ReactNode; | ||
}) => ReactNode; | ||
th?: (props: { | ||
cell: TableFieldHeaderCell; | ||
children: ReactNode; | ||
}) => ReactNode; | ||
td?: (props: { cell: TableFieldDataCell; children: ReactNode }) => ReactNode; | ||
}; | ||
|
||
const defaultComponents: Required<TableComponents> = { | ||
table: ({ children }) => <table>{children}</table>, | ||
thead: ({ children }) => <thead>{children}</thead>, | ||
tbody: ({ children }) => <tbody>{children}</tbody>, | ||
tr: ({ children }) => <tr>{children}</tr>, | ||
th: ({ children }) => <th>{children}</th>, | ||
td: ({ children }) => <td>{children}</td>, | ||
}; | ||
|
||
/** Props for `<PrismicTable>`. */ | ||
export type PrismicTableProps = { | ||
/** The Prismic table field to render. */ | ||
field: TableField; | ||
|
||
/** | ||
* An object that maps a table block to a React component. | ||
* | ||
* @example A map serializer. | ||
* | ||
* ```jsx | ||
* { | ||
* table: ({children}) => <table>{children}</table> | ||
* thead: ({children}) => <thead>{children}</thead> | ||
* } | ||
* ``` | ||
*/ | ||
components?: JSXMapSerializer & TableComponents; | ||
|
||
/** | ||
* The value to be rendered when the field is empty. If a fallback is not | ||
* given, `null` will be rendered. | ||
*/ | ||
fallback?: ReactNode; | ||
}; | ||
|
||
/** | ||
* React component that renders content from a Prismic table field. By default, | ||
* HTML elements are rendered for each piece of content. A `tbody` block will | ||
* render a `<tbody>` HTML element, for example. | ||
* | ||
* To customize the components that are rendered, provide a map serializer to | ||
* the `components` prop. | ||
* | ||
* @example Rendering a table field using the default HTMl elements. | ||
* | ||
* ```jsx | ||
* <PrismicTable field={document.data.my_table} />; | ||
* ``` | ||
* | ||
* @example Rendering a table field using a custom set of React components. | ||
* | ||
* ```jsx | ||
* <PrismicTable | ||
* field={document.data.my_table} | ||
* components={{ | ||
* tbody: ({ children }) => ( | ||
* <tbody className="my-class">{children}</tbody> | ||
* ), | ||
* }} | ||
* />; | ||
* ``` | ||
* | ||
* @param props - Props for the component. | ||
* | ||
* @returns The table field's content as React components. | ||
* | ||
* @see Learn about table fields {@link https://prismic.io/docs/core-concepts/table} | ||
*/ | ||
export function PrismicTable(props: PrismicTableProps) { | ||
const { field, components, fallback = null } = props; | ||
|
||
if (!isFilled.table(field)) { | ||
return fallback; | ||
} | ||
|
||
const { | ||
table: Table, | ||
thead: Thead, | ||
tbody: Tbody, | ||
} = { ...defaultComponents, ...components }; | ||
|
||
return ( | ||
<Table table={field}> | ||
{field.head && ( | ||
<Thead head={field.head}> | ||
{field.head.rows.map((row) => ( | ||
<TableRow | ||
key={JSON.stringify(row)} | ||
row={row} | ||
components={components} | ||
/> | ||
))} | ||
</Thead> | ||
)} | ||
<Tbody body={field.body}> | ||
{field.body.rows.map((row) => ( | ||
<TableRow | ||
key={JSON.stringify(row)} | ||
row={row} | ||
components={components} | ||
/> | ||
))} | ||
</Tbody> | ||
</Table> | ||
); | ||
} | ||
|
||
type TableRowProps = { | ||
row: TableFieldHeadRow | TableFieldBodyRow; | ||
components?: JSXMapSerializer & TableComponents; | ||
}; | ||
|
||
function TableRow(props: TableRowProps) { | ||
const { row, components } = props; | ||
|
||
const { tr: Tr, th: Th, td: Td } = { ...defaultComponents, ...components }; | ||
|
||
return ( | ||
<Tr row={row}> | ||
{row.cells.map((cell) => | ||
cell.type === "header" ? ( | ||
<Th key={JSON.stringify(cell)} cell={cell}> | ||
<PrismicRichText field={cell.content} components={components} /> | ||
</Th> | ||
) : ( | ||
<Td key={JSON.stringify(cell)} cell={cell}> | ||
<PrismicRichText field={cell.content} components={components} /> | ||
</Td> | ||
), | ||
)} | ||
</Tr> | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.