keystone icon indicating copy to clipboard operation
keystone copied to clipboard

Component block with prop relationship - bugs and inconsistency between editor and document renderer

Open larryhudson opened this issue 4 years ago • 1 comments

I have a Post list with a content document field. I also have an Image list with an imageFile image field.

I'm trying to add a prop relationship on a document field, with an image component block, that will make it possible to insert images into the post content, sort of like a media library.

I've found that there are a couple of bugs that are stopping this from working:

The document renderer doesn't set the output React component properly

Around line 167 of packages/document-renderer/src/index.tsx, it sets the React component output to the main object in the component-blocks.jsx file (which includes the keys component, label, props etc.).

This causes React to throw this error:

Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.

I've managed to fix this by changing line 167 to:

const Comp = componentBlocks[node.component].component

Inconsistency between the editor and the document renderer in rendering the props

When the component block is rendered in the editor, the image prop is an object with a value key:

{
  "value": {
    "id": "ckznd9zjt00801ku597g7lmhi",
    "data": { // hydrated relationship data
      "id": "ckznd9zjt00801ku597g7lmhi",
      "altText": "A man raising his hand with a lightbulb next to him",
      "imageFile": {
        "url": "/images/d018e49b-7611-4dfc-844c-fe875b7d9791.png",
        "height": 372,
        "width": 371
      }
    }
  }
}

But when it is rendered by the document renderer, it has this structure without the value key:

{
    "id": "ckznd9zjt00801ku597g7lmhi",
    "data": { // hydrated relationship data
      "id": "ckznd9zjt00801ku597g7lmhi",
      "altText": "A man raising his hand with a lightbulb next to him",
      "imageFile": {
        "url": "/images/d018e49b-7611-4dfc-844c-fe875b7d9791.png",
        "height": 372,
        "width": 371
      }
    }
}

This means that I need to duplicate the components and use one version for the editor, and another for the document renderer.

Please add steps to reproduce the problem

I've made an example repo here, based on the document fields example.

You can test it by creating an image, and then creating a post with the 'image component' block using that image.

Please describe what you expected to happen

I should be able to use the same component-blocks.jsx when defining the relationship and component block in my schema, as well as when I'm telling the document renderer what to use.

Please add contextual information such as your node version (node -v), or the web browser you used

Node version v14.18.1 on Windows 10. Using Firefox.

Thanks for making Keystone, I'm loving it so far!

larryhudson avatar Feb 17 '22 05:02 larryhudson

@larryhudson the renderer and editor preview components accept different props by design. For more info take a peek at this part of our docs:

https://keystonejs.com/docs/guides/document-fields#rendering-component-blocks

Hope this helps!

raveling avatar Feb 21 '22 23:02 raveling