Skip to content

Commit

Permalink
Merge pull request #49 from lukyth/hotfix/proptypes-isrequired
Browse files Browse the repository at this point in the history
Fix wrong detection of propType when isRequired is set
  • Loading branch information
Muhammed Thanish authored Aug 11, 2016
2 parents df7e3fb + 03527f1 commit fbd5118
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/components/PropTable.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import React from 'react';
import PropVal from './PropVal'
import PropVal from './PropVal';

const PropTypesMap = new Map();
for (let typeName in React.PropTypes) {
for (const typeName in React.PropTypes) {
if (!React.PropTypes.hasOwnProperty(typeName)) {
continue
continue;
}
const type = React.PropTypes[typeName];
PropTypesMap.set(type, typeName);
PropTypesMap.set(type.isRequired, typeName);
}

const stylesheet = {
propTable: {
marginLeft: -10,
borderSpacing: '10px 5px',
borderCollapse: 'separate',
}
},
};

export default class PropTable extends React.Component {
render () {
render() {
const type = this.props.type;

if (!type) {
Expand All @@ -29,28 +30,28 @@ export default class PropTable extends React.Component {
const props = {};

if (type.propTypes) {
for (let property in type.propTypes) {
for (const property in type.propTypes) {
if (!type.propTypes.hasOwnProperty(property)) {
continue
continue;
}
const typeInfo = type.propTypes[property];
const propType = PropTypesMap.get(typeInfo) || 'other';
const required = typeInfo.isRequired === undefined ? 'yes' : 'no';
props[property] = {property, propType, required};
props[property] = { property, propType, required };
}
}

if (type.defaultProps) {
for (let property in type.defaultProps) {
for (const property in type.defaultProps) {
if (!type.defaultProps.hasOwnProperty(property)) {
continue
continue;
}
const value = type.defaultProps[property];
if (value === undefined) {
continue;
}
if (!props[property]) {
props[property] = {property};
props[property] = { property };
}
props[property].defaultValue = value;
}
Expand Down Expand Up @@ -80,7 +81,7 @@ export default class PropTable extends React.Component {
<td>{row.property}</td>
<td>{row.propType}</td>
<td>{row.required}</td>
<td>{row.defaultValue === undefined ? '-' : <PropVal val={row.defaultValue} />}</td>
<td>{row.defaultValue === undefined ? '-' : <PropVal val={row.defaultValue} />}</td>
</tr>
))}
</tbody>
Expand All @@ -91,5 +92,5 @@ export default class PropTable extends React.Component {

PropTable.displayName = 'PropTable';
PropTable.propTypes = {
type: React.PropTypes.func
type: React.PropTypes.func,
};

0 comments on commit fbd5118

Please sign in to comment.