-
Notifications
You must be signed in to change notification settings - Fork 114
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
Advanced prop type definitions #105
Changes from all commits
6e4875d
86e9e60
53c948e
072c6ec
87d6b7d
a26b994
990d0ad
9dcaeee
a549464
1d33f8f
110f52b
80485ed
f40c603
c833efd
b5a6c83
2701f51
a1d0ec3
8a4a540
0a785fb
873ae98
d390f5d
b458b4a
9a74761
004dde1
a1e86c2
650818a
b05948b
77022be
bd79ea7
9dd5137
918d6e0
09d8a69
eea8def
0ad9dbe
f973f9f
bc5e17a
44b55ce
dc8b14b
5a7c884
68f1944
a387e94
1e67d21
bd8399c
521801f
0d5fb69
7924a7e
dcdb3fa
97b7a1b
2e4b9d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,70 @@ | ||
import React from 'react'; | ||
import cx from 'classnames'; | ||
import PropTypes from 'prop-types'; | ||
import ReactMarkdown from 'react-markdown'; | ||
import FunctionDefinition from './FunctionDefinition'; | ||
import Markdown from 'react-markdown'; | ||
import ReferenceExample from './ReferenceExample'; | ||
import styles from './PropList.module.scss'; | ||
import { format } from 'date-fns'; | ||
|
||
const PropTypeInfo = ({ type }) => { | ||
switch (type.raw) { | ||
case 'func': | ||
return ( | ||
<FunctionDefinition | ||
returnValue={type.meta.returnValue} | ||
params={type.meta.params} | ||
/> | ||
); | ||
case 'arrayOf': { | ||
const { itemTypes } = type.meta; | ||
|
||
return itemTypes.raw === 'oneOf' ? ( | ||
<div className={styles.listLike}> | ||
<div>{'<Array of'}</div> | ||
<div className={styles.arg}> | ||
<PropTypeInfo type={itemTypes} /> | ||
</div> | ||
<div>{'>'}</div> | ||
</div> | ||
) : ( | ||
<PropTypeInfo type={itemTypes} /> | ||
); | ||
} | ||
case 'oneOf': | ||
return ( | ||
<div className={styles.listLike}> | ||
<div>{'<One of'}</div> | ||
<div className={styles.arg}> | ||
{type.meta.constants.map((constant) => ( | ||
<div key={constant}>{constant},</div> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might want to trim off the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only reason I didn't do that was only because One Core has trailing commas: This isn't to say this is our desired result for the new dev site, but just the reason I did this to begin with. Should we do the same? Or remove the last comma? FYI, |
||
))} | ||
</div> | ||
<div>{'>'}</div> | ||
</div> | ||
); | ||
case 'oneOfType': | ||
return type.meta.types.map((type, idx) => ( | ||
<PropTypeInfo key={idx} type={type} /> | ||
)); | ||
case 'shape': | ||
return ( | ||
<div className={styles.shape}> | ||
<h4>shape</h4> | ||
<PropList propTypes={type.meta.types} /> | ||
</div> | ||
); | ||
default: | ||
return null; | ||
} | ||
}; | ||
|
||
PropTypeInfo.propTypes = { | ||
type: PropTypes.shape({ | ||
raw: PropTypes.string.isRequired, | ||
meta: PropTypes.object, | ||
}), | ||
}; | ||
|
||
const PropList = ({ propTypes }) => { | ||
if (propTypes.length === 0) { | ||
|
@@ -11,26 +74,55 @@ const PropList = ({ propTypes }) => { | |
return ( | ||
<div> | ||
{propTypes.map( | ||
({ name, description, isRequired, type, defaultValue }) => { | ||
({ | ||
name, | ||
description, | ||
deprecation, | ||
examples, | ||
isRequired, | ||
type, | ||
defaultValue, | ||
}) => { | ||
return ( | ||
<div key={name} className={styles.container}> | ||
<div className={styles.info}> | ||
<h3> | ||
{name}{' '} | ||
{name} | ||
{isRequired && ( | ||
<span className={styles.required}>required</span> | ||
<span className={styles.flagged}>required</span> | ||
)} | ||
{deprecation && ( | ||
<span className={styles.flagged}>deprecated</span> | ||
)} | ||
</h3> | ||
<div className={styles.type}>{type}</div> | ||
<div className={styles.type}>{type.name}</div> | ||
{defaultValue !== undefined && ( | ||
<div className={styles.default}> | ||
<p>DEFAULT</p> | ||
<p>{String(defaultValue)}</p> | ||
</div> | ||
)} | ||
</div> | ||
<div className={styles.details}> | ||
<ReactMarkdown source={description} /> | ||
<div className={styles.propInfo}> | ||
{deprecation && ( | ||
<div className={styles.deprecation}> | ||
<div className={styles.deprecationDate}> | ||
Due {format(new Date(deprecation.date), 'MMMM do, yyyy')} | ||
</div> | ||
<Markdown | ||
className={styles.markdownContainer} | ||
source={deprecation.description} | ||
/> | ||
</div> | ||
)} | ||
<Markdown | ||
className={cx(styles.details, styles.markdownContainer)} | ||
source={description} | ||
/> | ||
<PropTypeInfo type={type} /> | ||
{examples.map((example, idx) => ( | ||
<ReferenceExample key={idx} example={example} /> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
|
@@ -45,8 +137,15 @@ PropList.propTypes = { | |
PropTypes.shape({ | ||
name: PropTypes.string.isRequired, | ||
description: PropTypes.string, | ||
deprecation: PropTypes.shape({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if a PropType doesn't take the shape value (for example there's no deprecation) will this value be null/undefined? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is correct! You can see it set explicitly to I don't set the |
||
date: PropTypes.number, | ||
description: PropTypes.string, | ||
}), | ||
isRequired: PropTypes.bool, | ||
type: PropTypes.string, | ||
type: PropTypes.shape({ | ||
...PropTypeInfo.propTypes.type, | ||
name: PropTypes.string.isRequired, | ||
}), | ||
defaultValue: PropTypes.string, | ||
}) | ||
), | ||
|
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.
recursive react! never thought about doing this but this is super cool
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.
Yes! Its not often that you need it (I can only think of 1 other time I've done this), but super useful when you do need it. It helped me so much here.