@prismicio/react 2.4.1 → 2.5.0-alpha.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.
- package/dist/index.cjs +7 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1254 -1251
- package/dist/index.js +7 -7
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
- package/src/PrismicLink.tsx +17 -11
- package/src/SliceZone.tsx +41 -23
- package/src/index.ts +2 -0
package/src/PrismicLink.tsx
CHANGED
|
@@ -138,30 +138,36 @@ const _PrismicLink = <
|
|
|
138
138
|
|
|
139
139
|
if (!__PRODUCTION__) {
|
|
140
140
|
if ("field" in props && props.field) {
|
|
141
|
-
if (
|
|
142
|
-
!("link_type" in props.field) ||
|
|
143
|
-
!("url" in props.field || "id" in props.field)
|
|
144
|
-
) {
|
|
141
|
+
if (!props.field.link_type) {
|
|
145
142
|
console.error(
|
|
146
143
|
`[PrismicLink] This "field" prop value caused an error to be thrown.\n`,
|
|
147
144
|
props.field,
|
|
148
145
|
);
|
|
149
146
|
throw new Error(
|
|
150
|
-
`[PrismicLink] The provided field is missing required properties to properly render a link. The link
|
|
147
|
+
`[PrismicLink] The provided field is missing required properties to properly render a link. The link will not render. For more details, see ${devMsg(
|
|
148
|
+
"missing-link-properties",
|
|
149
|
+
)}`,
|
|
150
|
+
);
|
|
151
|
+
} else if (
|
|
152
|
+
!(
|
|
153
|
+
prismicH.isFilled.link(props.field) &&
|
|
154
|
+
("url" in props.field || "id" in props.field)
|
|
155
|
+
)
|
|
156
|
+
) {
|
|
157
|
+
console.warn(
|
|
158
|
+
`[PrismicLink] The provided field is missing required properties to properly render a link. The link may not render correctly. For more details, see ${devMsg(
|
|
151
159
|
"missing-link-properties",
|
|
152
160
|
)}`,
|
|
161
|
+
props.field,
|
|
153
162
|
);
|
|
154
163
|
}
|
|
155
164
|
} else if ("document" in props && props.document) {
|
|
156
165
|
if (!("url" in props.document || "id" in props.document)) {
|
|
157
|
-
console.
|
|
158
|
-
`[PrismicLink]
|
|
159
|
-
props.document,
|
|
160
|
-
);
|
|
161
|
-
throw new Error(
|
|
162
|
-
`[PrismicLink] The provided document is missing required properties to properly render a link. The link may not render. For more details, see ${devMsg(
|
|
166
|
+
console.warn(
|
|
167
|
+
`[PrismicLink] The provided document is missing required properties to properly render a link. The link may not render correctly. For more details, see ${devMsg(
|
|
163
168
|
"missing-link-properties",
|
|
164
169
|
)}`,
|
|
170
|
+
props.document,
|
|
165
171
|
);
|
|
166
172
|
}
|
|
167
173
|
}
|
package/src/SliceZone.tsx
CHANGED
|
@@ -26,6 +26,7 @@ type ExtractSliceType<Slice extends SliceLike> = Slice extends SliceLikeRestV2
|
|
|
26
26
|
*/
|
|
27
27
|
export type SliceLikeRestV2<SliceType extends string = string> = {
|
|
28
28
|
slice_type: prismicT.Slice<SliceType>["slice_type"];
|
|
29
|
+
slice_id?: string;
|
|
29
30
|
};
|
|
30
31
|
|
|
31
32
|
/**
|
|
@@ -59,7 +60,8 @@ export type SliceLike<SliceType extends string = string> =
|
|
|
59
60
|
*
|
|
60
61
|
* @typeParam TSlice - The type(s) of a Slice in the Slice Zone.
|
|
61
62
|
*/
|
|
62
|
-
export type SliceZoneLike<TSlice extends SliceLike
|
|
63
|
+
export type SliceZoneLike<TSlice extends SliceLike = SliceLike> =
|
|
64
|
+
readonly TSlice[];
|
|
63
65
|
|
|
64
66
|
/**
|
|
65
67
|
* React props for a component rendering content from a Prismic Slice using the
|
|
@@ -70,7 +72,8 @@ export type SliceZoneLike<TSlice extends SliceLike> = readonly TSlice[];
|
|
|
70
72
|
* available to all Slice components.
|
|
71
73
|
*/
|
|
72
74
|
export type SliceComponentProps<
|
|
73
|
-
|
|
75
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
76
|
+
TSlice extends SliceLike = any,
|
|
74
77
|
TContext = unknown,
|
|
75
78
|
> = {
|
|
76
79
|
/**
|
|
@@ -105,7 +108,8 @@ export type SliceComponentProps<
|
|
|
105
108
|
* @typeParam TContext - Arbitrary data made available to all Slice components.
|
|
106
109
|
*/
|
|
107
110
|
export type SliceComponentType<
|
|
108
|
-
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
112
|
+
TSlice extends SliceLike = any,
|
|
109
113
|
TContext = unknown,
|
|
110
114
|
> = React.ComponentType<SliceComponentProps<TSlice, TContext>>;
|
|
111
115
|
|
|
@@ -113,6 +117,8 @@ export type SliceComponentType<
|
|
|
113
117
|
* A record of Slice types mapped to a React component. The component will be
|
|
114
118
|
* rendered for each instance of its Slice.
|
|
115
119
|
*
|
|
120
|
+
* @deprecated This type is no longer used by `@prismicio/react`. Prefer using
|
|
121
|
+
* `Record<string, SliceComponentType<any>>` instead.
|
|
116
122
|
* @typeParam TSlice - The type(s) of a Slice in the Slice Zone.
|
|
117
123
|
* @typeParam TContext - Arbitrary data made available to all Slice components.
|
|
118
124
|
*/
|
|
@@ -169,7 +175,10 @@ export const TODOSliceComponent = __PRODUCTION__
|
|
|
169
175
|
/**
|
|
170
176
|
* Arguments for a `<SliceZone>` `resolver` function.
|
|
171
177
|
*/
|
|
172
|
-
type SliceZoneResolverArgs<
|
|
178
|
+
type SliceZoneResolverArgs<
|
|
179
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
180
|
+
TSlice extends SliceLike = any,
|
|
181
|
+
> = {
|
|
173
182
|
/**
|
|
174
183
|
* The Slice to resolve to a React component.
|
|
175
184
|
*/
|
|
@@ -198,11 +207,17 @@ type SliceZoneResolverArgs<TSlice extends SliceLike = SliceLike> = {
|
|
|
198
207
|
* @returns The React component to render for a Slice.
|
|
199
208
|
*/
|
|
200
209
|
export type SliceZoneResolver<
|
|
201
|
-
|
|
210
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
211
|
+
TSlice extends SliceLike = any,
|
|
202
212
|
TContext = unknown,
|
|
203
|
-
> = (
|
|
204
|
-
|
|
205
|
-
|
|
213
|
+
> = (args: SliceZoneResolverArgs<TSlice>) =>
|
|
214
|
+
| SliceComponentType<
|
|
215
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
216
|
+
any,
|
|
217
|
+
TContext
|
|
218
|
+
>
|
|
219
|
+
| undefined
|
|
220
|
+
| null;
|
|
206
221
|
|
|
207
222
|
/**
|
|
208
223
|
* React props for the `<SliceZone>` component.
|
|
@@ -210,19 +225,17 @@ export type SliceZoneResolver<
|
|
|
210
225
|
* @typeParam TSlice - The type(s) of a Slice in the Slice Zone.
|
|
211
226
|
* @typeParam TContext - Arbitrary data made available to all Slice components.
|
|
212
227
|
*/
|
|
213
|
-
export type SliceZoneProps<
|
|
214
|
-
TSlice extends SliceLike = SliceLike,
|
|
215
|
-
TContext = unknown,
|
|
216
|
-
> = {
|
|
228
|
+
export type SliceZoneProps<TContext = unknown> = {
|
|
217
229
|
/**
|
|
218
230
|
* List of Slice data from the Slice Zone.
|
|
219
231
|
*/
|
|
220
|
-
slices?: SliceZoneLike
|
|
232
|
+
slices?: SliceZoneLike;
|
|
221
233
|
|
|
222
234
|
/**
|
|
223
235
|
* A record mapping Slice types to React components.
|
|
224
236
|
*/
|
|
225
|
-
|
|
237
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
238
|
+
components?: Record<string, SliceComponentType<any, TContext>>;
|
|
226
239
|
|
|
227
240
|
/**
|
|
228
241
|
* A function that determines the rendered React component for each Slice in
|
|
@@ -234,13 +247,15 @@ export type SliceZoneProps<
|
|
|
234
247
|
*
|
|
235
248
|
* @returns The React component to render for a Slice.
|
|
236
249
|
*/
|
|
237
|
-
|
|
250
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
251
|
+
resolver?: SliceZoneResolver<any, TContext>;
|
|
238
252
|
|
|
239
253
|
/**
|
|
240
254
|
* The React component rendered if a component mapping from the `components`
|
|
241
255
|
* prop cannot be found.
|
|
242
256
|
*/
|
|
243
|
-
|
|
257
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
258
|
+
defaultComponent?: SliceComponentType<any, TContext>;
|
|
244
259
|
|
|
245
260
|
/**
|
|
246
261
|
* Arbitrary data made available to all Slice components.
|
|
@@ -263,19 +278,19 @@ export type SliceZoneProps<
|
|
|
263
278
|
*
|
|
264
279
|
* @see Learn about Prismic Slices and Slice Zones {@link https://prismic.io/docs/core-concepts/slices}
|
|
265
280
|
*/
|
|
266
|
-
export const SliceZone = <
|
|
281
|
+
export const SliceZone = <TContext,>({
|
|
267
282
|
slices = [],
|
|
268
|
-
components = {}
|
|
283
|
+
components = {},
|
|
269
284
|
resolver,
|
|
270
285
|
defaultComponent = TODOSliceComponent,
|
|
271
286
|
context = {} as TContext,
|
|
272
|
-
}: SliceZoneProps<
|
|
287
|
+
}: SliceZoneProps<TContext>): JSX.Element => {
|
|
273
288
|
const renderedSlices = React.useMemo(() => {
|
|
274
289
|
return slices.map((slice, index) => {
|
|
275
290
|
const type = "slice_type" in slice ? slice.slice_type : slice.type;
|
|
276
291
|
|
|
277
|
-
let Comp =
|
|
278
|
-
|
|
292
|
+
let Comp =
|
|
293
|
+
components[type as keyof typeof components] || defaultComponent;
|
|
279
294
|
|
|
280
295
|
// TODO: Remove `resolver` in v3 in favor of `components`.
|
|
281
296
|
if (resolver) {
|
|
@@ -286,11 +301,14 @@ export const SliceZone = <TSlice extends SliceLike, TContext>({
|
|
|
286
301
|
});
|
|
287
302
|
|
|
288
303
|
if (resolvedComp) {
|
|
289
|
-
Comp = resolvedComp;
|
|
304
|
+
Comp = resolvedComp as typeof Comp;
|
|
290
305
|
}
|
|
291
306
|
}
|
|
292
307
|
|
|
293
|
-
const key =
|
|
308
|
+
const key =
|
|
309
|
+
"slice_id" in slice && slice.slice_id
|
|
310
|
+
? slice.slice_id
|
|
311
|
+
: `${index}-${JSON.stringify(slice)}`;
|
|
294
312
|
|
|
295
313
|
return (
|
|
296
314
|
<Comp
|