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

[NO JIRA] add 'hasChildrenOfType' to bpk-react-utils #407

Closed
wants to merge 1 commit into from
Closed

[NO JIRA] add 'hasChildrenOfType' to bpk-react-utils #407

wants to merge 1 commit into from

Conversation

janeklb
Copy link
Contributor

@janeklb janeklb commented Dec 26, 2017

Currently lives in bpk-component-datatable (added here: bf00596) but makes sense to move to bpk-react-utils.

This PR is just to add the functionality to bpk-react-utils, then will submit a separate one for bpk-component-datatable.

@backpackbot
Copy link

backpackbot commented Dec 26, 2017

Messages
📖

Thanks for the PR 🎉.

Generated by 🚫 dangerJS

@janeklb janeklb changed the title [NO JIRA] move 'hasChildrenOfType' bpk-react-utils [NO JIRA] add 'hasChildrenOfType' to bpk-react-utils Dec 26, 2017

## hasChildrenOfType.js

A prop type checker that allows you to restrict a component's children to a specific type.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we explain how this is different to PropTypes.instanceOf? That was my first question upon seeing this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would adding 'Component' help? Totally open to suggestions if you have any ideas.
eg.

A prop type checker that allows you to restrict a component's children to a specific (Component) type.

Copy link
Contributor

Choose a reason for hiding this comment

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

@janeklb I second @k0nserv's question, what's the difference between using hasChildrenOfType(MyComponent) & PropTypes.instanceOf(MyComponent)? I.e. i think we are questioning whether this new prop type is redundant or not.

Copy link
Contributor Author

@janeklb janeklb Jan 2, 2018

Choose a reason for hiding this comment

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

PropTypes.instanceOf(...) uses js's instanceof operator to see if the prop value is an instance of the supplied class/type. It doesn't work with components (ie. jsx or React.createElement etc.) since those can't be operated on with instanceof.

This type checker validates that the children of a component are all "instances" of a certain component.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See the tests - they should give some more clarity :)

Copy link
Contributor Author

@janeklb janeklb Jan 2, 2018

Choose a reason for hiding this comment

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

tbh im not sure why this isn't a part of prop-types itself -- seems like a common enough requirement

update: looks like it may be soon.. facebook/prop-types#146

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And airbnb's prop types seem to already support something like this https://www.npmjs.com/package/airbnb-prop-types

@matthewdavidson
Copy link
Contributor

@janeklb where do you see this being used in backpack? Im tempted to close this as it can safely exist in userland and will most likely be available in prop-types at some point as you point out.

@janeklb
Copy link
Contributor Author

janeklb commented Jan 3, 2018

@matthewdavidson This is being used in bpk-component-datatable :)

It was inspired by react-virtualized's Table children validator

@matthewdavidson matthewdavidson self-assigned this Jan 5, 2018
@matthewdavidson
Copy link
Contributor

@janeklb Lets keep it inlined within the data table component package then dude - we don't think bpk-react-utils is necessarily the best place for it. We can revisit it as per rule of three if the times comes - hopefully it'll be in prop-types by then.

@janeklb
Copy link
Contributor Author

janeklb commented Jan 5, 2018

sounds good! cheers

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

Successfully merging this pull request may close these issues.

None yet

4 participants