import { RenderFieldExtensionCtx } from 'datocms-plugin-sdk' import { Canvas, Button } from 'datocms-react-ui' import get from 'lodash/get' type Props = { ctx: RenderFieldExtensionCtx } const SelectorField = ({ ctx }: Props) => { const initialValueJSON = get(ctx.formValues, ctx.fieldPath || '') const initialValue = JSON.parse(initialValueJSON as string) const handleOpenModal = async () => { const result = await ctx.openModal({ id: 'selectorModal', title: 'Select an asset from Raster', width: 'fullWidth', parameters: { selectedPhotos: initialValueJSON, formValues: ctx.formValues, fieldPath: ctx.fieldPath, }, }) if (result) { ctx.setFieldValue(ctx.fieldPath, result) } } const handleClearSelection = () => { ctx.setFieldValue(ctx.fieldPath, null) } const handleRemoveImage = (imageId: string) => { const newImages = JSON.parse(initialValueJSON as string).filter( (image: any) => image.id !== imageId ) const newImagesJSON = newImages.length ? JSON.stringify(newImages) : null ctx.setFieldValue(ctx.fieldPath, newImagesJSON) } const isVideo = (image: any) => { return !image.height && !image.width } return (

Select an asset from Raster {Boolean(initialValue?.length) && ( {initialValue?.length} selected )}

{Boolean(initialValue?.length) && ( )}
{/* Display thumbnails */} {Boolean(initialValue?.length) && (
{initialValue?.map((image: any) => { return (
{isVideo(image) ? (
) })}
)}
) } export default SelectorField