Custom Preview Component #3607
-
From the docs, migrating a custom Preview component from v2 to v3 seems very straightforward (docs link). However, I feel like something is definitely missing from the Docs, as I have my preview defined as such:
However, the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
I'm also running into this issue, struggling to figure out how to pass the preview.select fields into a custom preview component. Did you have any luck figuring this out @alexbchr? |
Beta Was this translation helpful? Give feedback.
-
I have pretty much the same issue with sanity v3.2.0. //richTextStory.tsx
import { defineField, defineType } from 'sanity'
import React from 'react'
const RichTextImagePreview = (props:any) => {
console.log("RichTextImagePreview props:", props)
return (
<>
<button>
{props.value?.text || 'Read more'}
</button>
</>
)
}
export default defineType({
title: "'Read More Button",
name: 'richTextStory',
type: 'object',
fields: [
defineField({
name: 'testField',
title: 'A test field',
type: 'string',
description:
"Just for testing.",
}),
defineField({
name: 'text',
title: 'local string field',
type: 'localeString',
description:
"foo bar",
})
],
preview: {
select: { text: 'text.de' }
},
components: {
preview: RichTextImagePreview
}
}) Turns out the |
Beta Was this translation helpful? Give feedback.
-
Hi all, I got this to work with the code below. It looks like the fields given in // File: schemas/bodyContent/bodyComponents/callout.ts
import { defineType, defineField } from 'sanity'
import { blockPreview } from 'sanity-pills'
import { GoInfo } from 'react-icons/go'
import { Card, Text } from '@sanity/ui'
const CalloutPreview = (props: any) => {
// Check the console to see!
console.log(props)
return (
<Card padding={[2, 3, 3, 3]} border={true}>
{props.renderDefault({
...props,
title: `${props.heading} (${props.intent})`,
subtitle: props.anotherField,
media: GoInfo,
})}
<Card padding={2}>
<Text>{props.subtitle}</Text>
</Card>
</Card>
)
}
export default defineType({
title: 'Callout',
name: 'callout',
type: 'object',
icon: GoInfo,
fields: [
defineField({
title: 'Heading',
name: 'heading',
type: 'string',
}),
defineField({
title: 'Content',
name: 'content',
type: 'array',
of: [{ type: 'block' }],
}),
defineField({
title: 'AnotherField',
name: 'anotherField',
type: 'string',
}),
defineField({
title: 'Intent',
name: 'intent',
type: 'string',
options: {
list: [
{ title: 'info', value: 'info' },
{ title: 'success', value: 'success' },
{ title: 'warning', value: 'warning' },
{ title: 'danger', value: 'danger' },
],
layout: 'radio',
},
}),
],
initialValue: {
intent: 'info',
},
preview: {
select: {
heading: 'heading',
content: 'content',
intent: 'intent',
anotherField: 'anotherField',
},
prepare: ({ heading, content, intent, anotherField }) => ({
heading: heading,
subtitle: blockPreview(content),
intent: intent,
anotherField: anotherField,
}),
},
components: {
preview: CalloutPreview,
},
}) |
Beta Was this translation helpful? Give feedback.
Hi all, I got this to work with the code below. It looks like the fields given in
preview.select
are passed to the function you specify inpreview.prepare
.preview.prepare
should return on object with the fields you need, the elements in which are accessible to the component you give tocomponents.preview
as direct children ofprops
(notprops.value
).