-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use getFieldHash() for getting the prop types as well, passing in convertToPropType.
- Loading branch information
1 parent
6ffd9fb
commit 45c53b1
Showing
3 changed files
with
41 additions
and
41 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const PropTypes = require('prop-types'); | ||
|
||
const PropTypeLookup = { | ||
text: 'string', | ||
integer: 'number', | ||
decimal: 'number', | ||
boolean: 'bool', | ||
}; | ||
|
||
module.exports = function convertToPropType(field) { | ||
const { name, type, isParam, enumValues, required } = field; | ||
// if the field isn't something we want to convert to a propType, then return an empty array so | ||
// getFieldHash() won't include this field | ||
let result = []; | ||
|
||
if (isParam || type === 'enum') { | ||
const reactType = PropTypeLookup[type]; | ||
let propType = PropTypes[reactType]; | ||
|
||
if (type === 'enum') { | ||
propType = PropTypes.oneOf(enumValues); | ||
} | ||
|
||
if (required) { | ||
propType = propType.isRequired; | ||
} | ||
|
||
result = [name, propType]; | ||
} | ||
|
||
return result; | ||
}; |