@@ -42,14 +42,15 @@ typeKnobsMap.forEach(({ name, knob, args = [] }, weight) => addKnobResolver({
4242} ) )
4343
4444const 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} )
0 commit comments