-
Notifications
You must be signed in to change notification settings - Fork 7
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
Conversation
There was a problem hiding this 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}`); |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
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'); |
There was a problem hiding this comment.
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(...);
}
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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(...);
}
There was a problem hiding this comment.
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.
bump, is there anything outstanding on this one? Would be nice to cleanup the unnecessary error messaging that this would fix |
Fun fact:
React.createClass
returns anObject
, not aClass
, which means you can't validate in propTypes usingReact.PropTypes.instanceOf(Component)
This fixes the validation issue in
Tabs
propTypes.