prevwong / craft.js

🚀 A React Framework for building extensible drag and drop page editors
https://craft.js.org
MIT License
7.56k stars 733 forks source link

User Component name doesn't change when specified in config #602

Open pavsidhu opened 8 months ago

pavsidhu commented 8 months ago

Describe the bug

User Component name doesn't reflect what is specified in the UserComponentConfig.

To Reproduce

import { Editor, Element, Frame, useNode } from "@craftjs/core";

function MyUserComponent() {
  const { node } = useNode((node) => ({ node }));
  return <h1>{node.data.name}</h1>;
}
MyUserComponent.craft = { name: "CustomName" };

export default function MyEditor() {
  return (
    <Editor resolver={{ MyUserComponent }}>
      <Frame>
        <Element is={MyUserComponent} />
      </Frame>
    </Editor>
  );
}

Expected behavior

The component shows MyUserComponent but I expect CustomName

Your environment

Software Version(s)
craft.js 0.2.3
React 18.2.0
TypeScript 4.0.1
Browser any
npm/Yarn any
Operating System any
shota-f commented 8 months ago

I found name, displayName and resolver-key(important). Document is heare(https://craft.js.org/docs/api/user-component). It seems like the updates are not keeping up. However, the type definitions(UserComponent) of code-base does not betray.

This is simple code.

import { Editor, Element, Frame, UserComponent, useNode } from "@craftjs/core"

const MyUserComponent: UserComponent = () => {
  const { node } = useNode((node) => ({ node }))
  return (
    <div>
      <div>Name: {node.data.name}</div>
      <div>DisplayName: {node.data.displayName}</div>
    </div>
  )
}

// Code completion works for both!
MyUserComponent.craft = { name: "RealName", displayName: "DisplayName" }

export default function MyEditor() {
  return (
    <Editor resolver={{ RealName: MyUserComponent }}>
      <Frame>
        <Element is={MyUserComponent} />
      </Frame>
    </Editor>
  )
}