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(react): allow proptype displayname match in childOf checks (WIP) #2785

Closed
wants to merge 7 commits into from

Conversation

finken2
Copy link
Contributor

@finken2 finken2 commented May 20, 2019

Allow displayname match for proptype checks in:
prop-types/childrenOf
prop-types/childrenOfType

The impetus for this was react-hot-loader proptype checks failing. The react-hot-loader wraps classes in a proxy wrapper so the strict class match doesn't work:
https://github.com/gaearon/react-hot-loader#checking-element-types
gaearon/react-hot-loader#304
This causes a react warning in the chrome dev console during development when using the carbon-react dropdown components.

It seems reasonable to allow displayname to count for these checks. Currently, it appears that these childOf functions are only used in the ListBox set of components.

Allow displayname match to count for proptype checks in:
prop-types/childrenOf
prop-types/childrenOfType
The impetus for this was react-hot-loader proptype checks failing.
The hot loader wraps classes in a proxy wrapper so
the strict class match doesn't work.
https://github.com/gaearon/react-hot-loader#checking-element-types
@project-bot project-bot bot added this to PR: needs review 🕵️ in Carbon support May 20, 2019
@netlify
Copy link

netlify bot commented May 20, 2019

Deploy preview for the-carbon-components ready!

Built with commit 1033645

https://deploy-preview-2785--the-carbon-components.netlify.com

@netlify
Copy link

netlify bot commented May 20, 2019

Deploy preview for carbon-components-react ready!

Built with commit 1033645

https://deploy-preview-2785--carbon-components-react.netlify.com

Copy link
Contributor

@asudoh asudoh left a comment

Choose a reason for hiding this comment

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

Hi 👋 thank you for jumping in! We have had some discussion on this problem space, and one thing we have seen wrt displayName is it's brittle. That said, would you want to try React.createElement(expectedChildType).type for unwrapping RHL proxy?

@finken2 finken2 changed the title feat(react): allow proptype displayname match in childOf checks feat(react): allow proptype displayname match in childOf checks (WIP) May 21, 2019
@dakahn dakahn moved this from PR: needs review 🕵️ to PR: Draft/WIP 🚧 in Carbon support May 21, 2019
@finken2
Copy link
Contributor Author

finken2 commented May 21, 2019

@asudoh Thanks for taking a look and providing feedback. I see what you're saying about displayName, but that mostly looks to be a problem in production. The validator won't even be running there, so it shouldn't be an issue. The displayName seems to stay consistent in development mode, at least for the components i looked at.

i've looked at some of the other attempts including:
#2641
https://github.com/carbon-design-system/carbon-components-react/pull/1253/files
That one looks interesting, but depends on the internal implementation of react-hot-loader which is problematic.

I tried the createElement approach a little this morning, and while it works for comparison, it leads to the problem of warnings for missing required properties.

I like the displayName approach since it works for the simple development case currently, has minimal impact, and shouldn't make things any worse. Let me know what you think. Another alternative could be just making the proptypes in the ListBox components more general (PropTypes.object). I can continue down any of these paths if we want to. Let me know.

@asudoh
Copy link
Contributor

asudoh commented May 21, 2019

Thank you for exploring some of the different options - The concern around displayName is that we don't make it an API contract (may break even in minor release), rather than development vs. production. Required properties thing is a bit cumbersome, but probably we can still do React.createElement(expectedChildType, requiredProps).type.

@@ -21,14 +22,20 @@ const childrenOf = expectedChildTypes => {
const expectedDisplayNames = expectedChildTypes
.map(child => getDisplayName(child.type || child))
.join(', ');
const expectedChildElementTypes = expectedChildTypes.map(
child => React.createElement(child).type
Copy link
Contributor

Choose a reason for hiding this comment

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

We often go down this road but get stumped when props are marked as required for a child and this call will trigger warnings as a result 😞 Does this PR offer a way around that, or is this not a problem anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nope, still a problem as of now. See my previous comment. and @asudoh 's reply.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @finken2!

@finken2
Copy link
Contributor Author

finken2 commented May 22, 2019

Thank you for exploring some of the different options - The concern around displayName is that we don't make it an API contract (may break even in minor release), rather than development vs. production. Required properties thing is a bit cumbersome, but probably we can still do React.createElement(expectedChildType, requiredProps).type.

I'll try out createElement a bit more as time allows. The thing with displayName, we're not really depending on it in a way that would break. So worst case, the check doesn't work, and you're right back where you started, seeing a react warning in dev console. But best case, checking displayname as an additional matching possibility gets rid of those warnings.

@joshblack
Copy link
Contributor

Hi @finken2! 👋 Wanted to check back in on the status of this PR. Do you think you'll have any time to continue your explorations, or should we close this PR in the meantime? Please note that closing a PR doesn't mean that we don't want to accept it! Just means that it'll be revisited when you get the chance 😄

@finken2
Copy link
Contributor Author

finken2 commented Jun 10, 2019

@joshblack hey josh, i've been meaning to ping you guys on Slack to continue the discussion. I'd really still like to get this in with the displayName check. I think that adds some value, and shouldn't hurt anything.

@asudoh
Copy link
Contributor

asudoh commented Jun 10, 2019

Hi @finken2 if you can explore an alternate approach it'll be great. The concern around displayName is still active and I don't want to see us going forward with it.

@netlify
Copy link

netlify bot commented Jun 10, 2019

Deploy preview for carbon-elements ready!

Built with commit 1033645

https://deploy-preview-2785--carbon-elements.netlify.com

@finken2
Copy link
Contributor Author

finken2 commented Jun 11, 2019

@asudoh I wasn't able to find any solution for the required props issue mentioned previously when going the component creation route. I'll close this PR for now and hopefully get back to it at some point if/when things change.

@finken2 finken2 closed this Jun 11, 2019
Carbon support automation moved this from PR: Draft/WIP 🚧 to Issue/PR: Closed 🙌 Jun 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants