-
Notifications
You must be signed in to change notification settings - Fork 4k
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
perf(props): Remove propTypes from production build #731
Changes from all commits
87b9f75
4a69508
c16f88c
1740075
2ca537b
285e928
46b52e3
4da8359
bf191fc
947411a
9477c06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,6 @@ import React, { Component, PropTypes } from 'react' | |
|
||
import { Label, Table } from 'src' | ||
|
||
const descriptionExtraStyle = { | ||
fontSize: '0.95em', | ||
color: '#777', | ||
} | ||
|
||
/** | ||
* Displays a table of a Component's PropTypes. | ||
*/ | ||
|
@@ -25,26 +20,9 @@ export default class ComponentProps extends Component { | |
meta: PropTypes.object, | ||
} | ||
|
||
state = { | ||
showEnumsFor: {}, | ||
} | ||
|
||
toggleEnumsFor = (prop) => () => { | ||
this.setState({ | ||
showEnumsFor: { | ||
...this.state.showEnumsFor, | ||
[prop]: !this.state.showEnumsFor[prop], | ||
}, | ||
}) | ||
} | ||
|
||
renderName = (item) => <code>{item.name}</code> | ||
renderName = item => <code>{item.name}</code> | ||
|
||
requiredRenderer = (item) => { | ||
if (!item.required) return null | ||
|
||
return <Label size='mini' color='red' circular>required</Label> | ||
} | ||
requiredRenderer = item => item.required && <Label size='mini' color='red' circular>required</Label> | ||
|
||
renderDefaultValue = (item) => { | ||
let defaultValue = _.get(item, 'defaultValue.value') | ||
|
@@ -62,52 +40,6 @@ export default class ComponentProps extends Component { | |
) | ||
} | ||
|
||
renderEnums = (item) => { | ||
const { showEnumsFor } = this.state | ||
const { meta } = this.props | ||
|
||
if (item.type.indexOf('enum') === -1) return null | ||
|
||
const values = meta.props[item.name] | ||
const truncateAt = 30 | ||
|
||
if (!values) return null | ||
|
||
// show all if there are few | ||
if (values.length < truncateAt) { | ||
return ( | ||
<p style={descriptionExtraStyle}> | ||
<strong>Enums: </strong> | ||
{values.join(', ')} | ||
</p> | ||
) | ||
} | ||
|
||
// add button to show more when there are many values and it is not toggled | ||
if (!showEnumsFor[item.name]) { | ||
return ( | ||
<p style={descriptionExtraStyle}> | ||
<strong>Enums: </strong> | ||
<a style={{ cursor: 'pointer' }} onClick={this.toggleEnumsFor(item.name)}> | ||
Show all {values.length} | ||
</a> | ||
<div>{values.slice(0, truncateAt - 1).join(', ')}...</div> | ||
</p> | ||
) | ||
} | ||
|
||
// add "show more" button when there are many | ||
return ( | ||
<p style={descriptionExtraStyle}> | ||
<strong>Enums: </strong> | ||
<a style={{ cursor: 'pointer' }} onClick={this.toggleEnumsFor(item.name)}> | ||
Show less | ||
</a> | ||
<div>{values.join(', ')}</div> | ||
</p> | ||
) | ||
} | ||
|
||
render() { | ||
const { props: propsDefinition } = this.props | ||
const content = _.sortBy(_.map(propsDefinition, (config, name) => { | ||
|
@@ -129,6 +61,7 @@ export default class ComponentProps extends Component { | |
description: description && description.split('\n').map(l => ([l, <br key={l} />])), | ||
} | ||
}), 'name') | ||
|
||
return ( | ||
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. Bad merge? 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. ^ |
||
<Table data={content} className='very basic compact'> | ||
<Table.Header> | ||
|
@@ -145,14 +78,9 @@ export default class ComponentProps extends Component { | |
<Table.Row key={item.name}> | ||
<Table.Cell>{this.renderName(item)}</Table.Cell> | ||
<Table.Cell>{this.requiredRenderer(item)}</Table.Cell> | ||
<Table.Cell> | ||
{item.type} | ||
</Table.Cell> | ||
<Table.Cell>{item.type}</Table.Cell> | ||
<Table.Cell>{this.renderDefaultValue(item.defaultValue)}</Table.Cell> | ||
<Table.Cell> | ||
{item.description && <p>{item.description}</p>} | ||
{this.renderEnums(item)} | ||
</Table.Cell> | ||
<Table.Cell>{item.description && <p>{item.description}</p>}</Table.Cell> | ||
</Table.Row> | ||
))} | ||
</Table.Body> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,3 @@ | ||
/** | ||
* Push all `source` array elements to the `target` array if they don't already exist in `target`. | ||
* | ||
* @param {Array} source - An array of elements to add to the `target` | ||
* @param {Array} target - An array to receive unique elements from the `source` | ||
* @returns {Array} Mutated `target` array | ||
*/ | ||
const pushUnique = (source, target) => source.forEach(x => { | ||
if (target.indexOf(x) === -1) target.push(x) | ||
}) | ||
|
||
/** | ||
* Returns an object consisting of props beyond the scope of the Component. | ||
* Useful for getting and spreading unknown props from the user. | ||
|
@@ -17,25 +6,9 @@ const pushUnique = (source, target) => source.forEach(x => { | |
* @returns {{}} A shallow copy of the prop object | ||
*/ | ||
const getUnhandledProps = (Component, props) => { | ||
const { autoControlledProps, defaultProps, propTypes } = Component | ||
let { handledProps } = Component | ||
|
||
// ---------------------------------------- | ||
// Calculate handledProps once and cache | ||
// ---------------------------------------- | ||
if (!handledProps) { | ||
handledProps = [] | ||
|
||
if (autoControlledProps) pushUnique(autoControlledProps, handledProps) | ||
if (defaultProps) pushUnique(Object.keys(defaultProps), handledProps) | ||
if (propTypes) pushUnique(Object.keys(propTypes), handledProps) | ||
|
||
Component.handledProps = handledProps | ||
} | ||
// Note that `handledProps` are generated automatically during build with `babel-plugin-transform-react-handled-props` | ||
const { handledProps = [] } = Component | ||
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. How about an inline comment here noting that this is added by the plugin? 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. Good idea, I'll add comment immediately. |
||
|
||
// ---------------------------------------- | ||
// Return _unhandled_ props | ||
// ---------------------------------------- | ||
return Object.keys(props).reduce((acc, prop) => { | ||
if (prop === 'childKey') return acc | ||
if (handledProps.indexOf(prop) === -1) acc[prop] = props[prop] | ||
|
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.
Bad merge?
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.
^