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

Commit 68c799f

Browse files
authored
fix: support for nested structure (#62)
fix: support for nested structure
2 parents 6303f37 + ca12e41 commit 68c799f

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

example/stories/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react'
22
import { storiesOf } from '@storybook/react'
33
import { withSmartKnobs } from '../../src'
44
import { withKnobs, select } from '@storybook/addon-knobs'
5+
import { withInfo } from '@storybook/addon-info'
56

67
import SmartKnobedComponent from './SmartKnobedComponent'
78
import SmartKnobedComponentMissingProps from './SmartKnobedComponentMissingProps'
@@ -14,6 +15,18 @@ storiesOf('Basic', module)
1415
.addDecorator(withKnobs)
1516
.add('proptypes', () => <SmartKnobedComponent />)
1617
.add('flow', () => <SmartKnobedComponentWithFlow />)
18+
.add('nested example', () => (
19+
<div>
20+
<span />
21+
<SmartKnobedComponent />
22+
</div>
23+
))
24+
25+
storiesOf('withInfo', module)
26+
.addDecorator(withSmartKnobs)
27+
.addDecorator(withKnobs)
28+
.addDecorator(withInfo)
29+
.add('proptypes', () => <SmartKnobedComponent />)
1730

1831
storiesOf('Missing props', module)
1932
.addDecorator(withSmartKnobs)

src/index.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { cloneElement } from 'react'
1+
import { cloneElement, Children } from 'react'
22
import { action } from '@storybook/addon-actions'
33
import { logger } from '@storybook/client-logger'
44
import { text, boolean, number, object, select } from '@storybook/addon-knobs'
@@ -69,13 +69,8 @@ addKnobResolver({
6969
})
7070

7171
const ensureType = (item) => item.flowType ? ({ ...item, type: item.flowType }) : item
72-
export const withSmartKnobs = (story, context) => {
73-
const component = story(context)
74-
75-
let target = component.props.components && component.props.propTables && component.props.propTablesExclude
76-
? component.props.children
77-
: component
7872

73+
const getNewProps = (target, context) => {
7974
const { __docgenInfo: { props } = { props: {} } } = target.type
8075
const defaultProps = {
8176
...(target.type.defaultProps || {}),
@@ -97,12 +92,34 @@ export const withSmartKnobs = (story, context) => {
9792
}
9893
}, {}) : {}
9994

100-
const newProps = resolvePropValues(finalProps, defaultProps)
95+
return resolvePropValues(finalProps, defaultProps)
96+
}
97+
98+
const mutateChildren = (component) => {
99+
return cloneElement(component, { children: Children.map(component.props.children, (child) => {
100+
if (child.type.__docgenInfo) {
101+
const newProps = getNewProps(child)
102+
103+
return cloneElement(child, { ...child.props, ...newProps })
104+
}
101105

102-
if (component.props.components) {
103-
return cloneElement(component, { ...component.props, children: cloneElement(component.props.children, newProps) })
106+
if (child.props.children) {
107+
return mutateChildren(child)
108+
}
109+
110+
return child
111+
}) })
112+
}
113+
114+
export const withSmartKnobs = (story, context) => {
115+
const component = story(context)
116+
117+
if (!component.type.__docgenInfo && component.props.children) {
118+
return mutateChildren(component)
104119
}
105120

121+
const newProps = getNewProps(component, context)
122+
106123
return cloneElement(component, newProps)
107124
}
108125

0 commit comments

Comments
 (0)