Created
November 21, 2016 14:24
-
-
Save mgtitimoli/e4c7e1d8fc00e02f66441d637418a31f to your computer and use it in GitHub Desktop.
shapeOf propType
This file contains hidden or 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
import {PropTypes} from 'react'; | |
const setValueOn = (keys, object, value) => keys.reduce( | |
(result, curKey) => Object.assign(result, {[curKey]: value}), | |
object | |
); | |
const shapeOf = (type, keys) => PropTypes.shape( | |
setValueOn(keys, {}, type) | |
); | |
// Usage | |
// ----- | |
// | |
// shapeOf(PropTypes.bool, ['field1', 'field2', 'field3']) | |
// | |
// is the same as: | |
// | |
// PropTypes.shape({ | |
// field1: PropTypes.bool, | |
// field2: PropTypes.bool, | |
// field3: PropTypes.bool, | |
// }) | |
export default shapeOf; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment