-
Notifications
You must be signed in to change notification settings - Fork 183
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
janeklb
wants to merge
1
commit into
Skyscanner:master
from
janeklb:jlb_hasChildrenOfType-to-bpk-react-utils
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Backpack - Skyscanner's Design System | ||
* | ||
* Copyright 2017 Skyscanner Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import getDisplayName from 'recompose/getDisplayName'; | ||
import hasChildrenOfType from './hasChildrenOfType'; | ||
|
||
const Component = () => <div>Component</div>; | ||
const Child = () => <span>Child</span>; | ||
|
||
const runPropTypeValidatorWith = ( | ||
component, | ||
{ atLeast = 1, propName = 'children', type = Child } = {}, | ||
) => { | ||
const propTypeValidator = hasChildrenOfType(type, atLeast); | ||
return propTypeValidator( | ||
component.props, | ||
propName, | ||
getDisplayName(component.type), | ||
); | ||
}; | ||
|
||
describe('hasChildrenOfType', () => { | ||
it('should not return an error if conditions are satisfied', () => { | ||
const error = runPropTypeValidatorWith( | ||
<Component> | ||
<Child /> | ||
</Component>, | ||
); | ||
expect(error).toBeUndefined(); | ||
}); | ||
|
||
it('should return an error if it has less children than it should (0 < 1)', () => { | ||
const error = runPropTypeValidatorWith(<Component />); | ||
expect(error).toEqual( | ||
Error("Component requires at least 1 'Child' child."), | ||
); | ||
}); | ||
|
||
it('should return an error if it has less children than it should (1 < 2)', () => { | ||
const error = runPropTypeValidatorWith( | ||
<Component> | ||
<Child /> | ||
</Component>, | ||
{ atLeast: 2 }, | ||
); | ||
expect(error).toEqual( | ||
Error("Component requires at least 2 'Child' children."), | ||
); | ||
}); | ||
|
||
it('should not return an error if it does not require children', () => { | ||
const error = runPropTypeValidatorWith(<Component />, { atLeast: 0 }); | ||
expect(error).toBeUndefined(); | ||
}); | ||
|
||
it('should return an error if it has single non matching child ', () => { | ||
const error = runPropTypeValidatorWith( | ||
<Component> | ||
<div /> | ||
</Component>, | ||
); | ||
expect(error).toEqual( | ||
Error("Component only allows 'Child' as children. Found 'div'."), | ||
); | ||
}); | ||
|
||
it('should return an error if it has a mix of matching and non matching children ', () => { | ||
const error = runPropTypeValidatorWith( | ||
<Component> | ||
<Child /> | ||
<div /> | ||
</Component>, | ||
); | ||
expect(error).toEqual( | ||
Error("Component only allows 'Child' as children. Found 'div'."), | ||
); | ||
}); | ||
|
||
it(`should return an error if used on a prop other than 'children'`, () => { | ||
const error = runPropTypeValidatorWith(<Component something={Child} />, { | ||
propName: 'something', | ||
}); | ||
expect(error).toEqual( | ||
Error(`hasChildrenOfType can only be used on 'children' propTypes`), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* Backpack - Skyscanner's Design System | ||
* | ||
* Copyright 2017 Skyscanner Ltd | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { Children } from 'react'; | ||
import getDisplayName from 'recompose/getDisplayName'; | ||
|
||
const hasChildrenOfType = (type, atLeast = 1) => ( | ||
props, | ||
propType, | ||
componentName, | ||
) => { | ||
if (propType !== 'children') { | ||
return Error(`hasChildrenOfType can only be used on 'children' propTypes`); | ||
} | ||
|
||
if (Children.count(props[propType]) < atLeast) { | ||
const inflectedNoun = atLeast === 1 ? 'child' : 'children'; | ||
return Error( | ||
`${componentName} requires at least ${atLeast} '${getDisplayName( | ||
type, | ||
)}' ${inflectedNoun}.`, | ||
); | ||
} | ||
|
||
const children = Children.toArray(props[propType]); | ||
for (let i = 0; i < children.length; i += 1) { | ||
const child = children[i]; | ||
if (child.type !== type && !(child.type.prototype instanceof type)) { | ||
return Error( | ||
`${componentName} only allows '${getDisplayName(type)}' as children. ` + | ||
`Found '${getDisplayName(child.type)}'.`, | ||
); | ||
} | ||
} | ||
|
||
return undefined; | ||
}; | ||
|
||
export default hasChildrenOfType; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we explain how this is different to
PropTypes.instanceOf
? That was my first question upon seeing this.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.
Would adding 'Component' help? Totally open to suggestions if you have any ideas.
eg.
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.
@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.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.
PropTypes.instanceOf(...)
uses js'sinstanceof
operator to see if the prop value is an instance of the supplied class/type. It doesn't work with components (ie. jsx orReact.createElement
etc.) since those can't be operated on withinstanceof
.This type checker validates that the children of a component are all "instances" of a certain component.
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.
See the tests - they should give some more clarity :)
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.
tbh im not sure why this isn't a part of
prop-types
itself -- seems like a common enough requirementupdate: looks like it may be soon.. facebook/prop-types#146
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.
And airbnb's prop types seem to already support something like this https://www.npmjs.com/package/airbnb-prop-types