Giter VIP home page Giter VIP logo

Comments (1)

tabdon avatar tabdon commented on August 25, 2024

I found a solution using React state mgmt.

Here it is using simpler components to demonstrate:

MyText.jsx:

import { useNode, useEditor } from '@craftjs/core';
import ContentEditable from 'react-contenteditable';

export const MyText = ({ text, setText }) => {
  const {
    connectors: { connect },
  } = useNode();
  const { enabled } = useEditor((state) => ({
    enabled: state.options.enabled,
  }));

  return (
    <ContentEditable
      innerRef={connect}
      html={text}
      disabled={!enabled}
      onChange={(e) => setText(e.target.value)}
      tagName="h2"
      style={{
        width: '100%',
        color: '#000000',
        fontSize: '16px', // Adjusted for better visibility
      }}
    />
  );
};

Text.craft = {
  props: {
    text: 'Edit Me',
    color: '#FFFFFF',
  },
  displayName: 'Text',
};

MyContainer.jsx

import React, { useState, useEffect } from 'react';
import { useNode, Element } from '@craftjs/core';
import { MyText } from './MyText.jsx';

export const MyContainer = ({ buttonList: initialButtonList }) => {
  const {
    connectors: { connect, drag },
  } = useNode();

  // State to track text values of buttons
  const [buttonList, setButtonList] = useState(initialButtonList);

  // Function to handle text change
  const handleTextChange = (text, index) => {
    const newList = buttonList.map((button, idx) => {
      if (idx === index) {
        return { ...button, text: text };
      }
      return button;
    });
    setButtonList(newList);
  };

  return (
    <div ref={connect} style={{ display: 'flex', flexDirection: 'column', gap: '10px', padding: '20px', backgroundColor: '#f0f0f0' }}>
      {buttonList.map((button, index) => (
        <div key={index} style={{ display: 'flex' }}>
          <MyText text={button.text} setText={(text) => handleTextChange(text, index)} />
        </div>
      ))}
    </div>
  );
};

MyContainer.craft = {
  props: {
    buttonList: [
      { text: 'Button 1', backgroundColor: '#007BFF', textColor: '#FFFFFF' },
      { text: 'Button 2', backgroundColor: '#28a745', textColor: '#FFFFFF' }
    ],
  },
  isCanvas: true,
  displayName: 'Container',
};

And in my toolbox:

<MyContainer buttonList={[
                    { text: 'Dynamic Button', backgroundColor: '#007BFF', textColor: '#FFFFFF' },
              { text: 'Another Button', backgroundColor: '#666666', textColor: '#FFFFFF' }
    ]} />

from craft.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.