@@ -17,13 +17,22 @@ class SimpleSelect extends React.Component {
1717 this . openSelect = this . openSelect . bind ( this ) ;
1818 this . handleOutsideOptionsClick = this . handleOutsideOptionsClick . bind ( this ) ;
1919 this . renderOption = this . renderOption . bind ( this ) ;
20+ this . renderOptionLabel = this . renderOptionLabel . bind ( this ) ;
2021
2122 const optionValidation = this . checkOptionType ( props . options ) ;
23+
24+
25+ //add index to each option for ease of rendering and value retrieval (this is what allows us to let value be any data type)
26+ const initialOptions = props . options ? props . options : [ ] ;
27+ const optionsWithIndexProp = initialOptions . map ( ( x , idx ) => {
28+ x . _idx = idx ;
29+ return x ;
30+ } ) ;
2231 const blankValue = { value : "" , label : "" } ;
2332 const defaultValue =
2433 props . defaultValue &&
25- props . options . find ( x => x . value === props . defaultValue )
26- ? props . options . find ( x => x . value === props . defaultValue )
34+ optionsWithIndexProp . find ( x => x . value === props . defaultValue )
35+ ? optionsWithIndexProp . find ( x => x . value === props . defaultValue )
2736 : blankValue ;
2837
2938 this . state = {
@@ -34,7 +43,7 @@ class SimpleSelect extends React.Component {
3443 currentOptionSelected : defaultValue ,
3544 legendLabel : props . legendLabel ? props . legendLabel : "Select..." ,
3645 selectOpen : false ,
37- options : props . options ? props . options : [ ] ,
46+ options : optionsWithIndexProp ,
3847 width : props . width ? props . width : "" ,
3948 } ;
4049 }
@@ -95,9 +104,14 @@ class SimpleSelect extends React.Component {
95104 }
96105 }
97106
107+ findOptionWrapper ( el ) {
108+ if ( el . getAttribute ( "index" ) ) return el ;
109+ return this . findOptionWrapper ( el . parentNode ) ;
110+ }
111+
98112 optionSelected ( e ) {
99- console . log ( e . target ) ;
100- const optionIndex = parseInt ( e . target . getAttribute ( "index" ) , 10 ) ;
113+ const optionWrapper = this . findOptionWrapper ( e . target )
114+ const optionIndex = parseInt ( optionWrapper . getAttribute ( "index" ) , 10 ) ;
101115 const optionByIndex = this . state . options [ optionIndex ] ;
102116 if ( this . props . onChange ) {
103117 this . props . onChange ( e , optionByIndex . value , optionByIndex ) ;
@@ -119,7 +133,6 @@ class SimpleSelect extends React.Component {
119133 }
120134
121135 cancelSelection ( e ) {
122- console . log ( "cancel button" ) ;
123136 if ( this . props . onChange ) {
124137 this . props . onChange ( e ) ;
125138 }
@@ -131,24 +144,32 @@ class SimpleSelect extends React.Component {
131144 e . stopPropagation ( ) ; //since cancel and dropdown are part of same dom tree we don't want the open/close to fire as well.
132145 }
133146
134- /* We could eliminate the need for the index if we just matched the first option that has the exact value... */
135- renderOption ( opt , idx ) {
147+ renderOptionLabel ( opt ) {
148+ if ( this . props . renderOptionLabel && opt != this . state . blankValue ) {
149+ return this . props . renderOptionLabel ( opt ) ;
150+ } else {
151+ return opt . label ;
152+ }
153+ }
154+
155+ renderOption ( opt ) {
136156 return (
137157 < div
138158 onClick = { this . optionSelected }
139- key = { `_select_opts${ idx } ` }
140- index = { idx }
159+ key = { `_select_opts${ opt . _idx } ` }
160+ index = { opt . _idx }
161+ className = { `select_option` }
141162 >
142- { opt . label }
163+ { this . renderOptionLabel ( opt ) }
143164 </ div >
144165 ) ;
145166 }
146167
147168 render ( ) {
148169 if ( ! this . state . validOptions )
149170 return < div > Invalid Options: { this . state . invalidReason } </ div > ;
150- const opts = this . state . options . map ( ( x , idx ) => {
151- return this . renderOption ( x , idx ) ;
171+ const opts = this . state . options . map ( x => {
172+ return this . renderOption ( x ) ;
152173 } ) ;
153174
154175 const style = {
@@ -181,7 +202,7 @@ class SimpleSelect extends React.Component {
181202 < div className = "legend" > { this . state . legendLabel } </ div >
182203 < div className = "mainSectionWrapper" onClick = { this . openSelect } ref = { this . dropdownButton } >
183204 < div className = "selectedDisplay" >
184- { this . state . currentOptionSelected . label }
205+ { this . renderOptionLabel ( this . state . currentOptionSelected ) }
185206 </ div >
186207 { cancelSection }
187208 < div className = "downArrowContainer" >
0 commit comments