@prismicio/react 3.0.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,87 @@
1
+ import { ReactNode } from "react";
2
+ import { TableField, TableFieldHead, TableFieldHeadRow, TableFieldBody, TableFieldBodyRow, TableFieldHeaderCell, TableFieldDataCell } from "@prismicio/client";
3
+ import { JSXMapSerializer } from "./PrismicRichText.js";
4
+ type TableComponents = {
5
+ table?: (props: {
6
+ table: TableField<"filled">;
7
+ children: ReactNode;
8
+ }) => ReactNode;
9
+ thead?: (props: {
10
+ head: TableFieldHead;
11
+ children: ReactNode;
12
+ }) => ReactNode;
13
+ tbody?: (props: {
14
+ body: TableFieldBody;
15
+ children: ReactNode;
16
+ }) => ReactNode;
17
+ tr?: (props: {
18
+ row: TableFieldHeadRow | TableFieldBodyRow;
19
+ children: ReactNode;
20
+ }) => ReactNode;
21
+ th?: (props: {
22
+ cell: TableFieldHeaderCell;
23
+ children: ReactNode;
24
+ }) => ReactNode;
25
+ td?: (props: {
26
+ cell: TableFieldDataCell;
27
+ children: ReactNode;
28
+ }) => ReactNode;
29
+ };
30
+ /** Props for `<PrismicTable>`. */
31
+ export type PrismicTableProps = {
32
+ /** The Prismic table field to render. */
33
+ field: TableField;
34
+ /**
35
+ * An object that maps a table block to a React component.
36
+ *
37
+ * @example A map serializer.
38
+ *
39
+ * ```jsx
40
+ * {
41
+ * table: ({children}) => <table>{children}</table>
42
+ * thead: ({children}) => <thead>{children}</thead>
43
+ * }
44
+ * ```
45
+ */
46
+ components?: JSXMapSerializer & TableComponents;
47
+ /**
48
+ * The value to be rendered when the field is empty. If a fallback is not
49
+ * given, `null` will be rendered.
50
+ */
51
+ fallback?: ReactNode;
52
+ };
53
+ /**
54
+ * React component that renders content from a Prismic table field. By default,
55
+ * HTML elements are rendered for each piece of content. A `tbody` block will
56
+ * render a `<tbody>` HTML element, for example.
57
+ *
58
+ * To customize the components that are rendered, provide a map serializer to
59
+ * the `components` prop.
60
+ *
61
+ * @example Rendering a table field using the default HTMl elements.
62
+ *
63
+ * ```jsx
64
+ * <PrismicTable field={document.data.my_table} />;
65
+ * ```
66
+ *
67
+ * @example Rendering a table field using a custom set of React components.
68
+ *
69
+ * ```jsx
70
+ * <PrismicTable
71
+ * field={document.data.my_table}
72
+ * components={{
73
+ * tbody: ({ children }) => (
74
+ * <tbody className="my-class">{children}</tbody>
75
+ * ),
76
+ * }}
77
+ * />;
78
+ * ```
79
+ *
80
+ * @param props - Props for the component.
81
+ *
82
+ * @returns The table field's content as React components.
83
+ *
84
+ * @see Learn about table fields {@link https://prismic.io/docs/core-concepts/table}
85
+ */
86
+ export declare function PrismicTable(props: PrismicTableProps): string | number | bigint | boolean | Iterable<ReactNode> | Promise<string | number | bigint | boolean | import("react").ReactPortal | import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | Iterable<ReactNode> | null | undefined> | import("react/jsx-runtime").JSX.Element | null;
87
+ export {};
@@ -0,0 +1,28 @@
1
+ import { jsxs, jsx } from "react/jsx-runtime";
2
+ import { isFilled } from "@prismicio/client";
3
+ import { PrismicRichText } from "./PrismicRichText.js";
4
+ const defaultComponents = {
5
+ table: ({ children }) => jsx("table", { children }),
6
+ thead: ({ children }) => jsx("thead", { children }),
7
+ tbody: ({ children }) => jsx("tbody", { children }),
8
+ tr: ({ children }) => jsx("tr", { children }),
9
+ th: ({ children }) => jsx("th", { children }),
10
+ td: ({ children }) => jsx("td", { children })
11
+ };
12
+ function PrismicTable(props) {
13
+ const { field, components, fallback = null } = props;
14
+ if (!isFilled.table(field)) {
15
+ return fallback;
16
+ }
17
+ const { table: Table, thead: Thead, tbody: Tbody } = { ...defaultComponents, ...components };
18
+ return jsxs(Table, { table: field, children: [field.head && jsx(Thead, { head: field.head, children: field.head.rows.map((row) => jsx(TableRow, { row, components }, row.key)) }), jsx(Tbody, { body: field.body, children: field.body.rows.map((row) => jsx(TableRow, { row, components }, row.key)) })] });
19
+ }
20
+ function TableRow(props) {
21
+ const { row, components } = props;
22
+ const { tr: Tr, th: Th, td: Td } = { ...defaultComponents, ...components };
23
+ return jsx(Tr, { row, children: row.cells.map((cell) => cell.type === "header" ? jsx(Th, { cell, children: jsx(PrismicRichText, { field: cell.content, components }) }, cell.key) : jsx(Td, { cell, children: jsx(PrismicRichText, { field: cell.content, components }) }, cell.key)) });
24
+ }
25
+ export {
26
+ PrismicTable
27
+ };
28
+ //# sourceMappingURL=PrismicTable.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PrismicTable.js","sources":["../src/PrismicTable.tsx"],"sourcesContent":["import { ReactNode } from \"react\";\nimport {\n\tisFilled,\n\tTableField,\n\tTableFieldHead,\n\tTableFieldHeadRow,\n\tTableFieldBody,\n\tTableFieldBodyRow,\n\tTableFieldHeaderCell,\n\tTableFieldDataCell,\n} from \"@prismicio/client\";\n\nimport { JSXMapSerializer, PrismicRichText } from \"./PrismicRichText.js\";\n\ntype TableComponents = {\n\ttable?: (props: {\n\t\ttable: TableField<\"filled\">;\n\t\tchildren: ReactNode;\n\t}) => ReactNode;\n\tthead?: (props: { head: TableFieldHead; children: ReactNode }) => ReactNode;\n\ttbody?: (props: { body: TableFieldBody; children: ReactNode }) => ReactNode;\n\ttr?: (props: {\n\t\trow: TableFieldHeadRow | TableFieldBodyRow;\n\t\tchildren: ReactNode;\n\t}) => ReactNode;\n\tth?: (props: {\n\t\tcell: TableFieldHeaderCell;\n\t\tchildren: ReactNode;\n\t}) => ReactNode;\n\ttd?: (props: { cell: TableFieldDataCell; children: ReactNode }) => ReactNode;\n};\n\nconst defaultComponents: Required<TableComponents> = {\n\ttable: ({ children }) => <table>{children}</table>,\n\tthead: ({ children }) => <thead>{children}</thead>,\n\ttbody: ({ children }) => <tbody>{children}</tbody>,\n\ttr: ({ children }) => <tr>{children}</tr>,\n\tth: ({ children }) => <th>{children}</th>,\n\ttd: ({ children }) => <td>{children}</td>,\n};\n\n/** Props for `<PrismicTable>`. */\nexport type PrismicTableProps = {\n\t/** The Prismic table field to render. */\n\tfield: TableField;\n\n\t/**\n\t * An object that maps a table block to a React component.\n\t *\n\t * @example A map serializer.\n\t *\n\t * ```jsx\n\t * {\n\t * table: ({children}) => <table>{children}</table>\n\t * thead: ({children}) => <thead>{children}</thead>\n\t * }\n\t * ```\n\t */\n\tcomponents?: JSXMapSerializer & TableComponents;\n\n\t/**\n\t * The value to be rendered when the field is empty. If a fallback is not\n\t * given, `null` will be rendered.\n\t */\n\tfallback?: ReactNode;\n};\n\n/**\n * React component that renders content from a Prismic table field. By default,\n * HTML elements are rendered for each piece of content. A `tbody` block will\n * render a `<tbody>` HTML element, for example.\n *\n * To customize the components that are rendered, provide a map serializer to\n * the `components` prop.\n *\n * @example Rendering a table field using the default HTMl elements.\n *\n * ```jsx\n * <PrismicTable field={document.data.my_table} />;\n * ```\n *\n * @example Rendering a table field using a custom set of React components.\n *\n * ```jsx\n * <PrismicTable\n * \tfield={document.data.my_table}\n * \tcomponents={{\n * \t\ttbody: ({ children }) => (\n * \t\t\t<tbody className=\"my-class\">{children}</tbody>\n * \t\t),\n * \t}}\n * />;\n * ```\n *\n * @param props - Props for the component.\n *\n * @returns The table field's content as React components.\n *\n * @see Learn about table fields {@link https://prismic.io/docs/core-concepts/table}\n */\nexport function PrismicTable(props: PrismicTableProps) {\n\tconst { field, components, fallback = null } = props;\n\n\tif (!isFilled.table(field)) {\n\t\treturn fallback;\n\t}\n\n\tconst {\n\t\ttable: Table,\n\t\tthead: Thead,\n\t\ttbody: Tbody,\n\t} = { ...defaultComponents, ...components };\n\n\treturn (\n\t\t<Table table={field}>\n\t\t\t{field.head && (\n\t\t\t\t<Thead head={field.head}>\n\t\t\t\t\t{field.head.rows.map((row) => (\n\t\t\t\t\t\t<TableRow key={row.key} row={row} components={components} />\n\t\t\t\t\t))}\n\t\t\t\t</Thead>\n\t\t\t)}\n\t\t\t<Tbody body={field.body}>\n\t\t\t\t{field.body.rows.map((row) => (\n\t\t\t\t\t<TableRow key={row.key} row={row} components={components} />\n\t\t\t\t))}\n\t\t\t</Tbody>\n\t\t</Table>\n\t);\n}\n\ntype TableRowProps = {\n\trow: TableFieldHeadRow | TableFieldBodyRow;\n\tcomponents?: JSXMapSerializer & TableComponents;\n};\n\nfunction TableRow(props: TableRowProps) {\n\tconst { row, components } = props;\n\n\tconst { tr: Tr, th: Th, td: Td } = { ...defaultComponents, ...components };\n\n\treturn (\n\t\t<Tr row={row}>\n\t\t\t{row.cells.map((cell) =>\n\t\t\t\tcell.type === \"header\" ? (\n\t\t\t\t\t<Th key={cell.key} cell={cell}>\n\t\t\t\t\t\t<PrismicRichText field={cell.content} components={components} />\n\t\t\t\t\t</Th>\n\t\t\t\t) : (\n\t\t\t\t\t<Td key={cell.key} cell={cell}>\n\t\t\t\t\t\t<PrismicRichText field={cell.content} components={components} />\n\t\t\t\t\t</Td>\n\t\t\t\t),\n\t\t\t)}\n\t\t</Tr>\n\t);\n}\n"],"names":["_jsx","_jsxs"],"mappings":";;;AAgCA,MAAM,oBAA+C;AAAA,EACpD,OAAO,CAAC,EAAE,eAAeA,IAAQ,SAAA,EAAA,UAAiB;AAAA,EAClD,OAAO,CAAC,EAAE,eAAeA,IAAQ,SAAA,EAAA,UAAiB;AAAA,EAClD,OAAO,CAAC,EAAE,eAAeA,IAAQ,SAAA,EAAA,UAAiB;AAAA,EAClD,IAAI,CAAC,EAAE,eAAeA,IAAK,MAAA,EAAA,UAAc;AAAA,EACzC,IAAI,CAAC,EAAE,eAAeA,IAAK,MAAA,EAAA,UAAc;AAAA,EACzC,IAAI,CAAC,EAAE,SAAA,MAAeA,IAAK,MAAA,EAAA,SAAc,CAAA;;AA8DpC,SAAU,aAAa,OAAwB;AACpD,QAAM,EAAE,OAAO,YAAY,WAAW,KAAS,IAAA;AAE/C,MAAI,CAAC,SAAS,MAAM,KAAK,GAAG;AACpB,WAAA;AAAA,EAAA;AAGR,QAAM,EACL,OAAO,OACP,OAAO,OACP,OAAO,UACJ,EAAE,GAAG,mBAAmB,GAAG,WAAU;AAEzC,SACCC,KAAC,OAAK,EAAC,OAAO,kBACZ,MAAM,QACND,IAAC,OAAK,EAAC,MAAM,MAAM,MACjB,UAAA,MAAM,KAAK,KAAK,IAAI,CAAC,QACrBA,IAAC,UAAQ,EAAe,KAAU,WAAsB,GAAzC,IAAI,GAAG,CACtB,EAAA,CACM,GAETA,IAAC,OAAM,EAAA,MAAM,MAAM,MAAI,UACrB,MAAM,KAAK,KAAK,IAAI,CAAC,QACrBA,IAAC,YAAuB,KAAU,WAAnB,GAAA,IAAI,GAAG,CACtB,EAAC,CAAA,CACK,EAAA,CACD;AAEV;AAOA,SAAS,SAAS,OAAoB;AAC/B,QAAA,EAAE,KAAK,WAAA,IAAe;AAE5B,QAAM,EAAE,IAAI,IAAI,IAAI,IAAI,IAAI,OAAO,EAAE,GAAG,mBAAmB,GAAG,WAAU;AAGvE,SAAAA,IAAC,MAAG,KACF,UAAA,IAAI,MAAM,IAAI,CAAC,SACf,KAAK,SAAS,WACbA,IAAC,IAAE,EAAgB,gBAClBA,IAAC,mBAAgB,OAAO,KAAK,SAAS,WAAsB,CAAA,KADpD,KAAK,GAAG,IAIjBA,IAAC,MAAkB,MAClB,UAAAA,IAAC,iBAAe,EAAC,OAAO,KAAK,SAAS,iBAD9B,KAAK,GAAG,CAGjB,GACD;AAGJ;"}
package/dist/index.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export { PrismicLink } from "./PrismicLink.js";
2
2
  export type { PrismicLinkProps, LinkProps } from "./PrismicLink.js";
3
+ export { PrismicTable } from "./PrismicTable.js";
4
+ export type { PrismicTableProps } from "./PrismicTable.js";
3
5
  export { PrismicText } from "./PrismicText.js";
4
6
  export type { PrismicTextProps } from "./PrismicText.js";
5
7
  export { PrismicRichText } from "./PrismicRichText.js";
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { PrismicLink } from "./PrismicLink.js";
2
+ import { PrismicTable } from "./PrismicTable.js";
2
3
  import { PrismicText } from "./PrismicText.js";
3
4
  import { PrismicRichText } from "./PrismicRichText.js";
4
5
  import { Element } from "@prismicio/client/richtext";
@@ -10,6 +11,7 @@ export {
10
11
  PrismicImage,
11
12
  PrismicLink,
12
13
  PrismicRichText,
14
+ PrismicTable,
13
15
  PrismicText,
14
16
  PrismicToolbar,
15
17
  SliceZone,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;"}
@@ -1,4 +1,4 @@
1
- const version = "3.0.0";
1
+ const version = "3.2.0";
2
2
  export {
3
3
  version
4
4
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/react",
3
- "version": "3.0.0",
3
+ "version": "3.2.0",
4
4
  "description": "React components and hooks to fetch and present Prismic content",
5
5
  "keywords": [
6
6
  "typescript",
@@ -59,7 +59,7 @@
59
59
  "devDependencies": {
60
60
  "@eslint/js": "^9.19.0",
61
61
  "@playwright/test": "^1.50.0",
62
- "@prismicio/client": "^7.12.0",
62
+ "@prismicio/client": "^7.17.0",
63
63
  "@rollup/plugin-typescript": "^12.1.2",
64
64
  "@size-limit/preset-small-lib": "^11.1.6",
65
65
  "@types/react": "^19.0.8",
@@ -0,0 +1,157 @@
1
+ import { ReactNode } from "react";
2
+ import {
3
+ isFilled,
4
+ TableField,
5
+ TableFieldHead,
6
+ TableFieldHeadRow,
7
+ TableFieldBody,
8
+ TableFieldBodyRow,
9
+ TableFieldHeaderCell,
10
+ TableFieldDataCell,
11
+ } from "@prismicio/client";
12
+
13
+ import { JSXMapSerializer, PrismicRichText } from "./PrismicRichText.js";
14
+
15
+ type TableComponents = {
16
+ table?: (props: {
17
+ table: TableField<"filled">;
18
+ children: ReactNode;
19
+ }) => ReactNode;
20
+ thead?: (props: { head: TableFieldHead; children: ReactNode }) => ReactNode;
21
+ tbody?: (props: { body: TableFieldBody; children: ReactNode }) => ReactNode;
22
+ tr?: (props: {
23
+ row: TableFieldHeadRow | TableFieldBodyRow;
24
+ children: ReactNode;
25
+ }) => ReactNode;
26
+ th?: (props: {
27
+ cell: TableFieldHeaderCell;
28
+ children: ReactNode;
29
+ }) => ReactNode;
30
+ td?: (props: { cell: TableFieldDataCell; children: ReactNode }) => ReactNode;
31
+ };
32
+
33
+ const defaultComponents: Required<TableComponents> = {
34
+ table: ({ children }) => <table>{children}</table>,
35
+ thead: ({ children }) => <thead>{children}</thead>,
36
+ tbody: ({ children }) => <tbody>{children}</tbody>,
37
+ tr: ({ children }) => <tr>{children}</tr>,
38
+ th: ({ children }) => <th>{children}</th>,
39
+ td: ({ children }) => <td>{children}</td>,
40
+ };
41
+
42
+ /** Props for `<PrismicTable>`. */
43
+ export type PrismicTableProps = {
44
+ /** The Prismic table field to render. */
45
+ field: TableField;
46
+
47
+ /**
48
+ * An object that maps a table block to a React component.
49
+ *
50
+ * @example A map serializer.
51
+ *
52
+ * ```jsx
53
+ * {
54
+ * table: ({children}) => <table>{children}</table>
55
+ * thead: ({children}) => <thead>{children}</thead>
56
+ * }
57
+ * ```
58
+ */
59
+ components?: JSXMapSerializer & TableComponents;
60
+
61
+ /**
62
+ * The value to be rendered when the field is empty. If a fallback is not
63
+ * given, `null` will be rendered.
64
+ */
65
+ fallback?: ReactNode;
66
+ };
67
+
68
+ /**
69
+ * React component that renders content from a Prismic table field. By default,
70
+ * HTML elements are rendered for each piece of content. A `tbody` block will
71
+ * render a `<tbody>` HTML element, for example.
72
+ *
73
+ * To customize the components that are rendered, provide a map serializer to
74
+ * the `components` prop.
75
+ *
76
+ * @example Rendering a table field using the default HTMl elements.
77
+ *
78
+ * ```jsx
79
+ * <PrismicTable field={document.data.my_table} />;
80
+ * ```
81
+ *
82
+ * @example Rendering a table field using a custom set of React components.
83
+ *
84
+ * ```jsx
85
+ * <PrismicTable
86
+ * field={document.data.my_table}
87
+ * components={{
88
+ * tbody: ({ children }) => (
89
+ * <tbody className="my-class">{children}</tbody>
90
+ * ),
91
+ * }}
92
+ * />;
93
+ * ```
94
+ *
95
+ * @param props - Props for the component.
96
+ *
97
+ * @returns The table field's content as React components.
98
+ *
99
+ * @see Learn about table fields {@link https://prismic.io/docs/core-concepts/table}
100
+ */
101
+ export function PrismicTable(props: PrismicTableProps) {
102
+ const { field, components, fallback = null } = props;
103
+
104
+ if (!isFilled.table(field)) {
105
+ return fallback;
106
+ }
107
+
108
+ const {
109
+ table: Table,
110
+ thead: Thead,
111
+ tbody: Tbody,
112
+ } = { ...defaultComponents, ...components };
113
+
114
+ return (
115
+ <Table table={field}>
116
+ {field.head && (
117
+ <Thead head={field.head}>
118
+ {field.head.rows.map((row) => (
119
+ <TableRow key={row.key} row={row} components={components} />
120
+ ))}
121
+ </Thead>
122
+ )}
123
+ <Tbody body={field.body}>
124
+ {field.body.rows.map((row) => (
125
+ <TableRow key={row.key} row={row} components={components} />
126
+ ))}
127
+ </Tbody>
128
+ </Table>
129
+ );
130
+ }
131
+
132
+ type TableRowProps = {
133
+ row: TableFieldHeadRow | TableFieldBodyRow;
134
+ components?: JSXMapSerializer & TableComponents;
135
+ };
136
+
137
+ function TableRow(props: TableRowProps) {
138
+ const { row, components } = props;
139
+
140
+ const { tr: Tr, th: Th, td: Td } = { ...defaultComponents, ...components };
141
+
142
+ return (
143
+ <Tr row={row}>
144
+ {row.cells.map((cell) =>
145
+ cell.type === "header" ? (
146
+ <Th key={cell.key} cell={cell}>
147
+ <PrismicRichText field={cell.content} components={components} />
148
+ </Th>
149
+ ) : (
150
+ <Td key={cell.key} cell={cell}>
151
+ <PrismicRichText field={cell.content} components={components} />
152
+ </Td>
153
+ ),
154
+ )}
155
+ </Tr>
156
+ );
157
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,9 @@
1
1
  export { PrismicLink } from "./PrismicLink.js";
2
2
  export type { PrismicLinkProps, LinkProps } from "./PrismicLink.js";
3
3
 
4
+ export { PrismicTable } from "./PrismicTable.js";
5
+ export type { PrismicTableProps } from "./PrismicTable.js";
6
+
4
7
  export { PrismicText } from "./PrismicText.js";
5
8
  export type { PrismicTextProps } from "./PrismicText.js";
6
9