Skip to content

How to apply style object to to react component from modify test? #5

@chrisdraws

Description

@chrisdraws

I'm attempting to pass the styles that are part of of the dom object I'm converting into the react element. Here is my function ->

d2r = () => {
return new Dom2react([
{
condition: (node, key, level, parser) => {
return true;
},
modify: (node, key, level, parser) => {

      const style2object = (styleString) => {
        const styleObject = {};
        if (styleString) {
          styleString.split(';').forEach((styles) => {
            const styleSplit = styles.split(':');
            const newName = styleSplit[0]
              .split('-')
              .reduce((totalWord, value, i) => {
                if (i !== 0) {
                  value = value.charAt(0).toUpperCase() + value.slice(1);
                }

                return totalWord + value;
              });
            if (newName) {
              styleObject[newName] = styleSplit[1];
            }
          });
           
        }
        return styleObject;
      };

      style2object(node.getAttribute && node.getAttribute('style'));

      return node;
       
  }
]);

};

How can I pass the style object into the props of the returned node? This is returning a dom node, but the function as a whole is returning an object that looks sort of like this ->

{$$typeof, key, props, ref, type}

at which point to pass in as a react object I do this ->

const IconDom = d2rGetReact.prepareNode(icon);

          return <IconDom.type {...IconDom.props} />;

Which works and the object is now in JSX. However I can not figure out for the life of me how to pass the styleobject I've created in the modify function back to the node so that it will appear in the props.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions