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

Commit 72b9dbf

Browse files
committed
Merge remote-tracking branch 'upstream/master' into feat/ignore-props
2 parents b46fb33 + b819d9b commit 72b9dbf

File tree

4 files changed

+1458
-1308
lines changed

4 files changed

+1458
-1308
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
dist
33
settings.json
4+
.idea

example/stories/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ storiesOf('Basic', module)
1717
.add('flow', () => <SmartKnobedComponentWithFlow />)
1818
.add('nested example', () => (
1919
<div>
20-
<span />
20+
<h1>Title</h1>
2121
<SmartKnobedComponent />
2222
</div>
2323
))

src/index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,15 @@ typeKnobsMap.forEach(({ name, knob, args = [] }, weight) => addKnobResolver({
4242
}))
4343

4444
const optionsReducer = (res, value) => ({ ...res, [value]: value })
45-
const withDefaultOption = (options) => ({ '': '--', ...options })
46-
const createSelect = (propName, elements, defaultProps) => {
45+
const withDefaultOption = (options) => ({ '--': undefined, ...options })
46+
const createSelect = (propName, elements, defaultProps, isRequired) => {
4747
try {
4848
const options = elements
4949
// Cleanup string quotes, if any.
5050
.map(value => value.value.replace(/^['"](.*)['"]$/, '$1'))
5151
.reduce(optionsReducer, {})
52-
return select(propName, withDefaultOption(options), defaultProps[propName])
52+
const value = defaultProps[propName] || (isRequired && Object.values(options)[0]) || undefined
53+
return select(propName, isRequired ? options : withDefaultOption(options), value)
5354
}
5455
catch (e) { }
5556
}
@@ -59,11 +60,11 @@ addKnobResolver({
5960
name: 'PropTypes.oneOf',
6061
resolver: (propName, propType, value, propTypes, defaultProps) => {
6162
if (propType.type.name === 'enum' && propType.type.value.length) {
62-
return createSelect(propName, propType.type.value, defaultProps)
63+
return createSelect(propName, propType.type.value, defaultProps, propType.required)
6364
}
6465
// for flow support
6566
if (propType.type.name === 'union' && propType.type.elements) {
66-
return createSelect(propName, propType.type.elements, defaultProps)
67+
return createSelect(propName, propType.type.elements, defaultProps, propType.required)
6768
}
6869
}
6970
})
@@ -102,13 +103,13 @@ const getNewProps = (target, context, opts) => {
102103

103104
const mutateChildren = (component, context, opts) => {
104105
return cloneElement(component, { children: Children.map(component.props.children, (child) => {
105-
if (child.type.__docgenInfo) {
106+
if (child.type && child.type.__docgenInfo) {
106107
const newProps = getNewProps(child, context, opts)
107108

108109
return cloneElement(child, { ...child.props, ...newProps })
109110
}
110111

111-
if (child.props.children) {
112+
if (child.props && child.props.children) {
112113
return mutateChildren(child, context, opts)
113114
}
114115

0 commit comments

Comments
 (0)