Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 692ea76

Browse files
Merge pull request #6 from weslleyaraujo/master
Undefined propType breaks render
2 parents dd01861 + 530715a commit 692ea76

File tree

5 files changed

+68
-22
lines changed

5 files changed

+68
-22
lines changed

dist/index.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr
1919

2020
var knobResolvers = {};
2121
var addKnobResolver = exports.addKnobResolver = function addKnobResolver(_ref) {
22-
var name = _ref.name;
23-
var resolver = _ref.resolver;
24-
var _ref$weight = _ref.weight;
25-
var weight = _ref$weight === undefined ? 0 : _ref$weight;
22+
var name = _ref.name,
23+
resolver = _ref.resolver,
24+
_ref$weight = _ref.weight,
25+
weight = _ref$weight === undefined ? 0 : _ref$weight;
2626
return knobResolvers[name] = { name: name, resolver: resolver, weight: weight };
2727
};
2828

@@ -48,10 +48,10 @@ var propTypeKnobsMap = [{ name: 'string', knob: _storybookAddonKnobs.text }, { n
4848
} }, { name: 'object', knob: _storybookAddonKnobs.object }, { name: 'node', knob: _storybookAddonKnobs.text }, { name: 'element', knob: _storybookAddonKnobs.text }];
4949

5050
propTypeKnobsMap.forEach(function (_ref2, weight) {
51-
var name = _ref2.name;
52-
var knob = _ref2.knob;
53-
var _ref2$args = _ref2.args;
54-
var args = _ref2$args === undefined ? [] : _ref2$args;
51+
var name = _ref2.name,
52+
knob = _ref2.knob,
53+
_ref2$args = _ref2.args,
54+
args = _ref2$args === undefined ? [] : _ref2$args;
5555
return addKnobResolver({
5656
weight: weight * 10,
5757
name: 'PropTypes.' + name,
@@ -90,8 +90,18 @@ var withSmartKnobs = exports.withSmartKnobs = function withSmartKnobs(story, con
9090

9191
var defaultProps = _extends({}, component.type.defaultProps || {}, component.props);
9292

93+
var finalProps = Object.keys(props).reduce(function (acc, n) {
94+
var item = props[n];
95+
if (!item.type) {
96+
console.warn('There is a prop with defaultValue ' + item.defaultValue.value + ' but it wasnt specified on element.propTypes. Check story: "' + context.kind + '".');
97+
return acc;
98+
}
99+
100+
return _extends({}, acc, _defineProperty({}, n, item));
101+
}, {});
102+
93103
return (0, _storybookAddonKnobs.withKnobs)(function () {
94-
return (0, _react.cloneElement)(component, resolvePropValues(props, defaultProps));
104+
return (0, _react.cloneElement)(component, resolvePropValues(finalProps, defaultProps));
95105
}, context);
96106
};
97107

example/stories/SmartKnobedComponent.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ const SmartKnobedComponent = props => (
1212
<th>typeof</th>
1313
</tr>
1414
</thead>
15-
16-
{ Object.keys(props).map(prop => (
17-
<tr key={ prop }>
18-
<th>{ prop }</th>
19-
<td>{ SmartKnobedComponent.__docgenInfo.props[prop].type.name }</td>
20-
<td>{ typeof props[prop] === 'function' ? <i>function</i> : JSON.stringify(props[prop]) || '(empty)' }</td>
21-
<td>{ typeof props[prop] }</td>
22-
</tr>
23-
)) }
15+
<tbody>
16+
{Object.keys(props).map(prop => (
17+
<tr key={ prop }>
18+
<th>{ prop }</th>
19+
<td>{ SmartKnobedComponent.__docgenInfo.props[prop].type.name }</td>
20+
<td>{ typeof props[prop] === 'function' ? <i>function</i> : JSON.stringify(props[prop]) || '(empty)' }</td>
21+
<td>{ typeof props[prop] }</td>
22+
</tr>
23+
))}
24+
</tbody>
2425
</table>
2526
)
2627

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React, { PropTypes } from 'react';
2+
3+
const SmartKnobedComponentMissingProps = ({
4+
foo = '',
5+
bar = 'bar',
6+
}) => (
7+
<code>
8+
<p>You should see a console.warn about a prop with default value bar.</p>
9+
<p>{foo}</p>
10+
<p>{bar}</p>
11+
</code>
12+
);
13+
14+
SmartKnobedComponentMissingProps.propTypes = {
15+
foo: PropTypes.string.isRequired,
16+
};
17+
18+
export default SmartKnobedComponentMissingProps;

example/stories/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ import React from 'react'
22
import { storiesOf } from '@kadira/storybook'
33
import { withSmartKnobs } from '../../src'
44

5-
import SmartKnobedComponent from './SmartKnobedComponent'
5+
import SmartKnobedComponent from './SmartKnobedComponent';
6+
import SmartKnobedComponentMissingProps from './SmartKnobedComponentMissingProps';
67

78
storiesOf('Example of smart Knobs', module)
89
.addDecorator(withSmartKnobs)
910
.add('full example', () => (
1011
<SmartKnobedComponent />
1112
))
1213

13-
14-
console.log(SmartKnobedComponent.__docgenInfo)
14+
storiesOf('Smart Knobs missing props', module)
15+
.addDecorator(withSmartKnobs)
16+
.add('example', () => (
17+
<SmartKnobedComponentMissingProps foo="baz" />
18+
))

src/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,20 @@ export const withSmartKnobs = (story, context) => {
6262
...component.props
6363
}
6464

65-
return withKnobs(() => cloneElement(component, resolvePropValues(props, defaultProps)), context)
65+
const finalProps = Object.keys(props).reduce((acc, n) => {
66+
const item = props[n];
67+
if (!item.type) {
68+
console.warn(`There is a prop with defaultValue ${item.defaultValue.value} but it wasnt specified on element.propTypes. Check story: "${context.kind}".`);
69+
return acc;
70+
}
71+
72+
return {
73+
...acc,
74+
[n]: item,
75+
};
76+
}, {});
77+
78+
return withKnobs(() => cloneElement(component, resolvePropValues(finalProps, defaultProps)), context)
6679
}
6780

6881
const resolvePropValues = (propTypes, defaultProps) => {

0 commit comments

Comments
 (0)