-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
TableComponent
option for addon-info
- Loading branch information
1 parent
0988d0c
commit 7b8f765
Showing
7 changed files
with
275 additions
and
84 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
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,94 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
|
||
import PropTypes from 'prop-types'; | ||
import React from 'react'; | ||
|
||
const PropTypesMap = new Map(); | ||
|
||
Object.keys(PropTypes).forEach(typeName => { | ||
const type = PropTypes[typeName]; | ||
|
||
PropTypesMap.set(type, typeName); | ||
PropTypesMap.set(type.isRequired, typeName); | ||
}); | ||
|
||
const isNotEmpty = obj => obj && obj.props && Object.keys(obj.props).length > 0; | ||
|
||
const hasDocgen = type => isNotEmpty(type.__docgenInfo); | ||
|
||
const propsFromDocgen = type => { | ||
const props = {}; | ||
const docgenInfoProps = type.__docgenInfo.props; | ||
|
||
Object.keys(docgenInfoProps).forEach(property => { | ||
const docgenInfoProp = docgenInfoProps[property]; | ||
const defaultValueDesc = docgenInfoProp.defaultValue || {}; | ||
const propType = docgenInfoProp.flowType || docgenInfoProp.type || 'other'; | ||
|
||
props[property] = { | ||
property, | ||
propType, | ||
required: docgenInfoProp.required, | ||
description: docgenInfoProp.description, | ||
defaultValue: defaultValueDesc.value, | ||
}; | ||
}); | ||
|
||
return props; | ||
}; | ||
|
||
const propsFromPropTypes = type => { | ||
const props = {}; | ||
|
||
if (type.propTypes) { | ||
Object.keys(type.propTypes).forEach(property => { | ||
const typeInfo = type.propTypes[property]; | ||
const required = typeInfo.isRequired === undefined; | ||
const docgenInfo = | ||
type.__docgenInfo && type.__docgenInfo.props && type.__docgenInfo.props[property]; | ||
const description = docgenInfo ? docgenInfo.description : null; | ||
let propType = PropTypesMap.get(typeInfo) || 'other'; | ||
|
||
if (propType === 'other') { | ||
if (docgenInfo && docgenInfo.type) { | ||
propType = docgenInfo.type.name; | ||
} | ||
} | ||
|
||
props[property] = { property, propType, required, description }; | ||
}); | ||
} | ||
|
||
if (type.defaultProps) { | ||
Object.keys(type.defaultProps).forEach(property => { | ||
const value = type.defaultProps[property]; | ||
|
||
if (value === undefined) { | ||
return; | ||
} | ||
|
||
if (!props[property]) { | ||
props[property] = { property }; | ||
} | ||
|
||
props[property].defaultValue = value; | ||
}); | ||
} | ||
|
||
return props; | ||
}; | ||
|
||
export default function makeTableComponent(Component) { | ||
return props => { | ||
if (!props.type) { // eslint-disable-line | ||
return null; | ||
} | ||
|
||
const propDefinitionsMap = hasDocgen(props.type) | ||
? propsFromDocgen(props.type) | ||
: propsFromPropTypes(props.type); | ||
const propDefinitions = Object.values(propDefinitionsMap); | ||
|
||
return <Component propDefinitions={propDefinitions} {...props} />; | ||
}; | ||
} |
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
Oops, something went wrong.