1+ 'use strict' ;
2+
3+ Object . defineProperty ( exports , "__esModule" , {
4+ value : true
5+ } ) ;
6+ exports . withSmartKnobs = exports . propTypeKnobResolver = exports . addKnobResolver = undefined ;
7+
8+ var _extends = Object . assign || function ( target ) { for ( var i = 1 ; i < arguments . length ; i ++ ) { var source = arguments [ i ] ; for ( var key in source ) { if ( Object . prototype . hasOwnProperty . call ( source , key ) ) { target [ key ] = source [ key ] ; } } } return target ; } ;
9+
10+ var _react = require ( 'react' ) ;
11+
12+ var _storybook = require ( '@kadira/storybook' ) ;
13+
14+ var _storybookAddonKnobs = require ( '@kadira/storybook-addon-knobs' ) ;
15+
16+ function _defineProperty ( obj , key , value ) { if ( key in obj ) { Object . defineProperty ( obj , key , { value : value , enumerable : true , configurable : true , writable : true } ) ; } else { obj [ key ] = value ; } return obj ; }
17+
18+ function _toConsumableArray ( arr ) { if ( Array . isArray ( arr ) ) { for ( var i = 0 , arr2 = Array ( arr . length ) ; i < arr . length ; i ++ ) { arr2 [ i ] = arr [ i ] ; } return arr2 ; } else { return Array . from ( arr ) ; } }
19+
20+ var knobResolvers = { } ;
21+ var addKnobResolver = exports . addKnobResolver = function addKnobResolver ( _ref ) {
22+ var name = _ref . name ;
23+ var resolver = _ref . resolver ;
24+ var _ref$weight = _ref . weight ;
25+ var weight = _ref$weight === undefined ? 0 : _ref$weight ;
26+ return knobResolvers [ name ] = { name : name , resolver : resolver , weight : weight } ;
27+ } ;
28+
29+ /*
30+ * Register default knob resolvers.
31+ * --------------------------------
32+ */
33+
34+ var propTypeKnobResolver = exports . propTypeKnobResolver = function propTypeKnobResolver ( test , knob ) {
35+ for ( var _len = arguments . length , args = Array ( _len > 2 ? _len - 2 : 0 ) , _key = 2 ; _key < _len ; _key ++ ) {
36+ args [ _key - 2 ] = arguments [ _key ] ;
37+ }
38+
39+ return function ( name , validate , value , propTypes , defaultProps ) {
40+ return ! validate ( { 'prop' : test } , 'prop' ) ? knob . apply ( undefined , [ name , value ] . concat ( args ) ) : undefined ;
41+ } ;
42+ } ;
43+
44+ /* eslint-disable no-multi-spaces */
45+ // 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 ) {
47+ return value || ( 0 , _storybook . action ) ( name ) ;
48+ } } , { name : 'object' , test : { } , knob : _storybookAddonKnobs . object } ] ;
49+
50+ propTypeKnobsMap . forEach ( function ( _ref2 ) {
51+ var name = _ref2 . name ;
52+ var test = _ref2 . test ;
53+ var knob = _ref2 . knob ;
54+ var _ref2$args = _ref2 . args ;
55+ var args = _ref2$args === undefined ? [ ] : _ref2$args ;
56+ return addKnobResolver ( {
57+ name : name ,
58+ resolver : propTypeKnobResolver . apply ( undefined , [ test , knob ] . concat ( _toConsumableArray ( args ) ) )
59+ } ) ;
60+ } ) ;
61+
62+ // Defalt oneOf PropType knob resolver.
63+ addKnobResolver ( {
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 ] ) ;
78+ }
79+ }
80+ } ) ;
81+
82+ var 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+
87+ return ( 0 , _react . cloneElement ) ( component , resolvePropValues ( propTypes , defaultProps ) ) ;
88+ } ;
89+
90+ var resolvePropValues = function resolvePropValues ( propTypes , defaultProps ) {
91+ var propKeys = Object . keys ( propTypes ) ;
92+ var resolvers = Object . keys ( knobResolvers ) . sort ( function ( a , b ) {
93+ return knobResolvers [ a ] . weight < knobResolvers [ b ] . weight ;
94+ } ) . map ( function ( name ) {
95+ return knobResolvers [ name ] . resolver ;
96+ } ) ;
97+
98+ return propKeys . map ( function ( prop ) {
99+ return resolvers . reduce ( function ( value , resolver ) {
100+ return value !== undefined ? value : resolver ( prop , propTypes [ prop ] , defaultProps [ prop ] , propTypes , defaultProps ) ;
101+ } , undefined ) ;
102+ } ) . reduce ( function ( props , value , i ) {
103+ return _extends ( { } , props , _defineProperty ( { } , propKeys [ i ] , value ) ) ;
104+ } , defaultProps ) ;
105+ } ;
0 commit comments