Skip to content

Commit

Permalink
Merge pull request #3 from kadirahq/default-props
Browse files Browse the repository at this point in the history
Show default values for props
  • Loading branch information
Muhammed Thanish authored Jun 15, 2016
2 parents 2cd3850 + 15dd9b1 commit d21c4c1
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 24 deletions.
47 changes: 35 additions & 12 deletions dist/components/PropTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ Object.defineProperty(exports, "__esModule", {
value: true
});

var _values = require('babel-runtime/core-js/object/values');

var _values2 = _interopRequireDefault(_values);

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
Expand Down Expand Up @@ -60,16 +64,35 @@ var PropTable = function (_React$Component) {
return null;
}

var props = [];
for (var property in comp.propTypes) {
if (!comp.propTypes.hasOwnProperty(property)) {
continue;
var props = {};

if (comp.propTypes) {
for (var property in comp.propTypes) {
if (!comp.propTypes.hasOwnProperty(property)) {
continue;
}
var _type = comp.propTypes[property];
var propType = PropTypesMap.get(_type) || 'other';
var required = _type.isRequired === undefined ? 'yes' : 'no';
var defaultValue = '-';
props[property] = { property: property, propType: propType, required: required, defaultValue: defaultValue };
}
}

if (comp.defaultProps) {
for (var _property in comp.defaultProps) {
if (!comp.defaultProps.hasOwnProperty(_property)) {
continue;
}
var value = comp.defaultProps[_property];
if (value === undefined) {
continue;
}
if (!props[_property]) {
props[_property] = { property: _property };
}
props[_property].defaultValue = value;
}
var _type = comp.propTypes[property];
var propType = PropTypesMap.get(_type) || 'other';
var required = _type.isRequired === undefined ? 'yes' : 'no';
var defaults = '-';
props.push({ property: property, propType: propType, required: required, defaults: defaults });
}

return _react2.default.createElement(
Expand Down Expand Up @@ -99,14 +122,14 @@ var PropTable = function (_React$Component) {
_react2.default.createElement(
'th',
null,
'defaults'
'default'
)
)
),
_react2.default.createElement(
'tbody',
null,
props.map(function (row) {
(0, _values2.default)(props).map(function (row) {
return _react2.default.createElement(
'tr',
null,
Expand All @@ -128,7 +151,7 @@ var PropTable = function (_React$Component) {
_react2.default.createElement(
'td',
null,
row.defaults
row.defaultValue.toString()
)
);
})
Expand Down
43 changes: 31 additions & 12 deletions src/components/PropTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,35 @@ export default class PropTable extends React.Component {
return null;
}

const props = [];
for (let property in comp.propTypes) {
if (!comp.propTypes.hasOwnProperty(property)) {
continue
const props = {};

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

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

return (
Expand All @@ -41,16 +60,16 @@ export default class PropTable extends React.Component {
<th>property</th>
<th>propType</th>
<th>required</th>
<th>defaults</th>
<th>default</th>
</tr>
</thead>
<tbody>
{props.map(row => (
{Object.values(props).map(row => (
<tr>
<td>{row.property}</td>
<td>{row.propType}</td>
<td>{row.required}</td>
<td>{row.defaults}</td>
<td>{row.defaultValue.toString()}</td>
</tr>
))}
</tbody>
Expand Down

0 comments on commit d21c4c1

Please sign in to comment.