1- import { cloneElement } from 'react'
1+ import { cloneElement , Children } from 'react'
22import { action } from '@storybook/addon-actions'
33import { logger } from '@storybook/client-logger'
44import { text , boolean , number , object , select } from '@storybook/addon-knobs'
@@ -69,13 +69,8 @@ addKnobResolver({
6969} )
7070
7171const 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