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

update tabs child validation #112

Merged
merged 6 commits into from
Mar 21, 2017
Merged

update tabs child validation #112

merged 6 commits into from
Mar 21, 2017

Conversation

akdetrick
Copy link
Contributor

Fun fact:

React.createClass returns an Object, not a Class, which means you can't validate in propTypes using React.PropTypes.instanceOf(Component)


This fixes the validation issue in Tabs propTypes.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.1%) to 70.406% when pulling d41c26f on bug/tabs_child_validation into 55a2210 on master.

Copy link
Contributor

@mmcgahan mmcgahan left a comment

Choose a reason for hiding this comment

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

Did you find some docs on this? The propTypes docs have an instanceOf example that looks similar to the previous implementation, so I'm concerned that this might be working around a different issue

src/Tabs.jsx Outdated

React.Children.forEach(props[propName], child => {
if (typeof child === 'undefined' || child.type.name !== expectedName) {
error = new Error(`Children must be React elements of type ${expectedName}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

just return the error immediately rather than assigning it to a let.

@@ -63,9 +63,17 @@ export class Tabs extends React.Component {
}
}
Tabs.propTypes = {
children: React.PropTypes.arrayOf(
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the problem might be that children is not a true array, so when it tries to check the type of children[0], it finds an object that is not the actual child.

That means that you can use React.Children.forEach in your custom function, but also use instanceof TabsTab

Copy link
Contributor Author

Choose a reason for hiding this comment

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

simplified this to child.type !== TabsTab, which seems to work

@coveralls
Copy link

Coverage Status

Coverage increased (+0.07%) to 70.349% when pulling a047b15 on bug/tabs_child_validation into 55a2210 on master.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.07%) to 70.349% when pulling 324c088 on bug/tabs_child_validation into 55a2210 on master.

src/Tabs.jsx Outdated
let error;
React.Children.forEach(props[propName], child => {
if (typeof child === 'undefined' || child.type !== TabsTab) {
error = new Error('Children must be React elements of type TabsTab');
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see why you kept the let error setup since it is set inside a callback.

Just for fun, you can still avoid the let error (and mutable state) with:

const validChildren = React.Children.map(
  children,
  child => child instanceof TabsTab
).every(x => x);  // check that all children are TabsTab

if (!validChildren) {
  return new Error(...);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nice!

src/Tabs.jsx Outdated
children: (props, propName, componentName) => {
let error;
React.Children.forEach(props[propName], child => {
if (typeof child === 'undefined' || child.type !== TabsTab) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I recommend separating out your two child validation conditions - use React.Children.count to return an Error about passing in no children, and use the forEach that you have here to check the type of the children.

The separation will make it a little more clear what is needed and simplify the code a bit

const children = props[propName];
if (!React.Children.count(children)) {
  return new Error(...);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is good stuff. I can actually use this to require that at least 2 TabsTab children are present.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.08%) to 70.192% when pulling e0547c3 on bug/tabs_child_validation into 55a2210 on master.

@eilinora
Copy link

bump, is there anything outstanding on this one? Would be nice to cleanup the unnecessary error messaging that this would fix

@akdetrick akdetrick merged commit f295a9d into master Mar 21, 2017
@chenrui333 chenrui333 deleted the bug/tabs_child_validation branch September 16, 2019 22:07
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.

4 participants