Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(utils): allow object props to make it to the component #99

Merged
merged 1 commit into from
Mar 16, 2023

Conversation

robin-ray
Copy link
Contributor

@robin-ray robin-ray commented Mar 15, 2023

Hello!

Summary

This PR changes the behavior of convertPropValueToMjml to give users of this library more flexibility, at the expense of allowing some unintentional mistakes to happen (ex: accidentally rendering some-prop="[Object object]").

Motivation

I have built a small library that uses objects and toString() to build up expressions that can be rendered to an MJML template. This works great for props on ordinary React HTML elements (this is a contrived example):

const MyImage = () => {
  const imgURL = { toString: () => "/path/to/some/resource" };
  return <img src={imgURL} />;
};

React renders <img src="/path/to/some/resource" /> for the snippet above.

However, I have to explicitly convert the value to a string for it to pass through mjml-react:

const MyImage = () => {
  const imgURL = { toString: () => "/path/to/some/resource" };
  return <MjmlImage src={`${imgURL}`} />;
};

Without the explicit conversion, React will render <mj-image /> because convertPropValueToMjml does not allow object props through (except for dangerouslySetInnerHTML). It would be convenient if mjml-react components behaved more like React HTML components in this regard so that I don't have to do the extra string conversion.

Thanks for considering this PR!

@emmclaughlin
Copy link
Collaborator

Hi @robin-ray, and thank you for the idea.

Unfortunately I am not sure giving more flexibility at the cost of unintentional mistakes is something we want to do with regards to the type safety on the mjml-react components. However, it seems as though you are using javascript? I noticed that the src prop on the html img tag also throws a type error when trying to pass the imgURL object.

Essentially what I am thinking is it may be reasonable to relax checks in convertPropValueToMjml so that all props can pass through in javascript, but in typescript

const MyImage = () => {
  const imgURL = { toString: () => "/path/to/some/resource" };
  return <img src={imgURL} />;
};

will still throw an error.

@robin-ray
Copy link
Contributor Author

robin-ray commented Mar 16, 2023

That's right! I'm using JavaScript for my components to avoid that issue.

@robin-ray
Copy link
Contributor Author

I'm all for having this be a compile-time error in TypeScript! My concern is about the runtime check.

@emmclaughlin
Copy link
Collaborator

emmclaughlin commented Mar 16, 2023

In that case I completely agree, it is not a good development experience for props to magically disappear. Just running CI checks now

Copy link
Contributor

@IanEdington IanEdington left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me.

In general we are a little to strict about the props coming into components IMO. This is a related ticket that I think we should do in v4: #43

This might be a braking change since anything that was being silently made null will now be passed through. Very unlikely though and it only breaks things that don't conform to the API so I think it's fine to add to v3. I've updated the title to a feat though.

@IanEdington IanEdington changed the title fix(utils): allow object props to make it to the component feat(utils): allow object props to make it to the component Mar 16, 2023
Copy link
Collaborator

@emmclaughlin emmclaughlin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thank you for the PR

@emmclaughlin emmclaughlin merged commit f872ccc into Faire:main Mar 16, 2023
@robin-ray robin-ray deleted the allow-object-props branch March 16, 2023 15:34
@github-actions
Copy link

🎉 This PR is included in version 3.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants