@@ -31,75 +31,83 @@ var addKnobResolver = exports.addKnobResolver = function addKnobResolver(_ref) {
3131 * --------------------------------
3232 */
3333
34- var propTypeKnobResolver = exports . propTypeKnobResolver = function propTypeKnobResolver ( test , knob ) {
34+ var propTypeKnobResolver = exports . propTypeKnobResolver = function propTypeKnobResolver ( name , knob ) {
3535 for ( var _len = arguments . length , args = Array ( _len > 2 ? _len - 2 : 0 ) , _key = 2 ; _key < _len ; _key ++ ) {
3636 args [ _key - 2 ] = arguments [ _key ] ;
3737 }
3838
39- return function ( name , validate , value , propTypes , defaultProps ) {
40- return ! validate ( { 'prop' : test } , 'prop' ) ? knob . apply ( undefined , [ name , value ] . concat ( args ) ) : undefined ;
39+ return function ( propName , propType , value , propTypes , defaultProps ) {
40+ return propType . type . name === name ? knob . apply ( undefined , [ propName , value ] . concat ( args ) ) : undefined ;
4141 } ;
4242} ;
4343
4444/* eslint-disable no-multi-spaces */
4545// Default simple PropType based knob map.
46- var propTypeKnobsMap = [ { name : 'string' , test : '' , knob : _storybookAddonKnobs . text } , { name : 'number' , test : 0 , knob : _storybookAddonKnobs . number } , { name : 'bool' , test : true , knob : _storybookAddonKnobs . boolean } , { name : 'func' , test : function test ( ) { } , knob : function knob ( name , value ) {
46+ var propTypeKnobsMap = [ { name : 'string' , knob : _storybookAddonKnobs . text } , { name : 'number' , knob : _storybookAddonKnobs . number } , { name : 'bool' , knob : _storybookAddonKnobs . boolean } , { name : 'func' , knob : function knob ( name , value ) {
4747 return value || ( 0 , _storybook . action ) ( name ) ;
48- } } , { name : 'object' , test : { } , knob : _storybookAddonKnobs . object } ] ;
48+ } } , { name : 'object' , knob : _storybookAddonKnobs . object } , { name : 'node' , knob : _storybookAddonKnobs . text } , { name : 'element' , knob : _storybookAddonKnobs . text } ] ;
4949
50- propTypeKnobsMap . forEach ( function ( _ref2 ) {
50+ propTypeKnobsMap . forEach ( function ( _ref2 , weight ) {
5151 var name = _ref2 . name ;
52- var test = _ref2 . test ;
5352 var knob = _ref2 . knob ;
5453 var _ref2$args = _ref2 . args ;
5554 var args = _ref2$args === undefined ? [ ] : _ref2$args ;
5655 return addKnobResolver ( {
57- name : name ,
58- resolver : propTypeKnobResolver . apply ( undefined , [ test , knob ] . concat ( _toConsumableArray ( args ) ) )
56+ weight : weight * 10 ,
57+ name : 'PropTypes.' + name ,
58+ resolver : propTypeKnobResolver . apply ( undefined , [ name , knob ] . concat ( _toConsumableArray ( args ) ) )
5959 } ) ;
6060} ) ;
6161
62- // Defalt oneOf PropType knob resolver.
62+ // Register ' oneOf' PropType knob resolver.
6363addKnobResolver ( {
64- name : 'oneOf' ,
65- resolver : function resolver ( name , validate , value , propTypes , defaultProps ) {
66- var error = validate ( { 'prop' : '' } , 'prop' ) ;
67- var match = error ? / e x p e c t e d o n e o f ( \[ .* \] ) / . exec ( error . message ) : null ;
68-
69- if ( match && match [ 1 ] ) {
70- var parsedOptions = JSON . parse ( match [ 1 ] ) ;
71- var options = parsedOptions . reduce ( function ( res , value ) {
72- return _extends ( { } , res , _defineProperty ( { } , value , value ) ) ;
73- } , { } ) ;
74-
75- return ( 0 , _storybookAddonKnobs . select ) ( name , _extends ( {
76- '' : '--'
77- } , options ) , defaultProps [ name ] ) ;
64+ name : 'PropTypes.oneOf' ,
65+ resolver : function resolver ( propName , propType , value , propTypes , defaultProps ) {
66+ /* eslint-disable quotes */
67+ if ( propType . type . name === 'enum' && propType . type . value . length ) {
68+ try {
69+ var options = propType . type . value . map ( function ( value ) {
70+ return value . value ;
71+ } )
72+ // Cleanup string quotes, if any.
73+ . map ( function ( value ) {
74+ return value [ 0 ] === "'" && value [ value . length - 1 ] === "'" ? '"' + value . replace ( / ' " ' / g, '\\"' ) . slice ( 1 , value . length - 1 ) + '"' : value ;
75+ } ) . map ( JSON . parse ) . reduce ( function ( res , value ) {
76+ return _extends ( { } , res , _defineProperty ( { } , value , value ) ) ;
77+ } , { } ) ;
78+
79+ return ( 0 , _storybookAddonKnobs . select ) ( propName , _extends ( { '' : '--' } , options ) , defaultProps [ propName ] ) ;
80+ } catch ( e ) { }
7881 }
7982 }
8083} ) ;
8184
8285var withSmartKnobs = exports . withSmartKnobs = function withSmartKnobs ( story , context ) {
83- var component = story ( ) ;
84- var propTypes = component . type . propTypes ;
85- var defaultProps = _extends ( { } , component . type . defaultProps , component . props ) ;
86+ var component = story ( context ) ;
87+ var _component$type$__doc = component . type . __docgenInfo ;
88+ _component$type$__doc = _component$type$__doc === undefined ? { props : { } } : _component$type$__doc ;
89+ var props = _component$type$__doc . props ;
8690
87- return ( 0 , _react . cloneElement ) ( component , resolvePropValues ( propTypes , defaultProps ) ) ;
91+ var defaultProps = _extends ( { } , component . type . defaultProps || { } , component . props ) ;
92+
93+ return ( 0 , _storybookAddonKnobs . withKnobs ) ( function ( ) {
94+ return ( 0 , _react . cloneElement ) ( component , resolvePropValues ( props , defaultProps ) ) ;
95+ } , context ) ;
8896} ;
8997
9098var resolvePropValues = function resolvePropValues ( propTypes , defaultProps ) {
91- var propKeys = Object . keys ( propTypes ) ;
99+ var propNames = Object . keys ( propTypes ) ;
92100 var resolvers = Object . keys ( knobResolvers ) . sort ( function ( a , b ) {
93101 return knobResolvers [ a ] . weight < knobResolvers [ b ] . weight ;
94102 } ) . map ( function ( name ) {
95103 return knobResolvers [ name ] . resolver ;
96104 } ) ;
97105
98- return propKeys . map ( function ( prop ) {
106+ return propNames . map ( function ( propName ) {
99107 return resolvers . reduce ( function ( value , resolver ) {
100- return value !== undefined ? value : resolver ( prop , propTypes [ prop ] , defaultProps [ prop ] , propTypes , defaultProps ) ;
108+ return value !== undefined ? value : resolver ( propName , propTypes [ propName ] , defaultProps [ propName ] , propTypes , defaultProps ) ;
101109 } , undefined ) ;
102110 } ) . reduce ( function ( props , value , i ) {
103- return _extends ( { } , props , _defineProperty ( { } , propKeys [ i ] , value ) ) ;
111+ return _extends ( { } , props , _defineProperty ( { } , propNames [ i ] , value ) ) ;
104112 } , defaultProps ) ;
105113} ;
0 commit comments