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

Commit c6df2d8

Browse files
committed
fix(withSmartKnobs): check for undefined types
relates to #5
1 parent dd01861 commit c6df2d8

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

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)