@@ -9,25 +9,45 @@ export default class EditableSelect extends React.Component {
9
9
name : React . PropTypes . string . isRequired ,
10
10
onUpdate : React . PropTypes . func . isRequired ,
11
11
options : React . PropTypes . array . isRequired ,
12
+ defaultOptionText : React . PropTypes . string ,
12
13
}
13
14
constructor ( props ) {
14
15
super ( props ) ;
15
- const text = this . props . options . map ( ( option ) => {
16
- return option . value === this . props . value ? option . text : '' ;
16
+ this . setState = this . setState . bind ( this ) ;
17
+
18
+ const options = this . convertOptions ( this . props . options ) ;
19
+ if ( this . props . defaultOptionText ) {
20
+ options . unshift ( { text : this . props . defaultOptionText , value : null } ) ;
21
+ }
22
+ const selected = options . find ( ( opt ) => {
23
+ if ( opt . value === this . props . value ) {
24
+ return opt ;
25
+ }
17
26
} ) ;
27
+
28
+ let text = 'Invalid value' ;
29
+
30
+ if ( selected ) {
31
+ text = selected . text ;
32
+ } else if ( this . props . defaultOptionText ) {
33
+ text = this . props . defaultOptionText ;
34
+ }
18
35
this . state = {
19
36
isEditing : false ,
37
+ options : options ,
20
38
text : text ,
39
+ value : this . props . value ,
21
40
} ;
22
- this . setState = this . setState . bind ( this ) ;
41
+
23
42
}
43
+
24
44
save = ( event ) => {
25
45
event . preventDefault ( ) ;
26
- const obj = this . refs . select ;
27
- this . props . onUpdate ( this . props . name , obj . value ) ;
46
+ this . props . onUpdate ( this . props . name , this . refs . el . value ) ;
28
47
this . setState ( {
29
48
isEditing : false ,
30
- text : obj . options [ obj . selectedIndex ] . text ,
49
+ text : this . refs . el . options [ this . refs . el . selectedIndex ] . text ,
50
+ value : this . refs . el . value ,
31
51
} ) ;
32
52
}
33
53
cancel = ( ) => {
@@ -36,14 +56,25 @@ export default class EditableSelect extends React.Component {
36
56
handleLinkClick = ( ) => {
37
57
this . setState ( { isEditing : true } ) ;
38
58
}
59
+
60
+ convertOptions = ( options ) => {
61
+ return options . map ( ( opt ) => {
62
+ if ( typeof opt === 'string'
63
+ || typeof opt === 'number'
64
+ || typeof opt === 'boolean' ) {
65
+ return { text : opt , value : opt } ;
66
+ }
67
+ return { text : opt . text , value : opt . value } ;
68
+ } ) ;
69
+ }
39
70
render ( ) {
40
71
if ( this . state . isEditing ) {
41
- const options = this . props . options . map ( ( option , index ) => {
42
- return < option key = { index } value = { option . value } > { option . text } </ option > ;
72
+ const options = this . state . options . map ( ( opt , index ) => {
73
+ return < option key = { index } value = { opt . value } > { opt . text } </ option > ;
43
74
} ) ;
44
75
return (
45
76
< XEditable isLoading = { false } save = { this . save } cancel = { this . cancel } >
46
- < select ref = 'select ' className = 'form-control input-sm' name = { this . props . name } defaultValue = { this . props . value } >
77
+ < select ref = 'el ' className = 'form-control input-sm' name = { this . props . name } defaultValue = { this . state . value } >
47
78
{ options }
48
79
</ select >
49
80
</ XEditable >
0 commit comments