@@ -75,41 +75,37 @@ export function show (items, { active, emptyMessage, errorMessage, infoMessage,
75
75
}
76
76
}
77
77
}
78
- selector . props . filterKeyForItem = ( items . length > 0 && items [ 0 ] instanceof Object ) ?
79
- item => item . primary : item => item
80
- if ( items . constructor == Promise ) {
81
- items . then ( items => {
82
- selector . update ( {
83
- items,
84
- emptyMessage,
85
- errorMessage,
86
- infoMessage,
87
- loadingMessage : ''
88
- } )
89
- if ( active ) {
90
- const index = ( active instanceof Number ) ? active :
91
- ( active instanceof Function ) ? items . findIndex ( active ) :
92
- items . findIndex ( item => item == active )
93
- selector . selectIndex ( index )
94
- }
95
- } ) . catch ( err => {
96
- reject ( err )
97
- selector . cancelSelection ( )
98
- } )
99
- } else {
78
+ // for handling `Promise`
79
+ function updateSelector ( items ) {
80
+ selector . props . filterKeyForItem = ( items . length > 0 && items [ 0 ] instanceof Object ) ?
81
+ item => item . primary : item => item
100
82
selector . update ( {
101
83
items,
102
84
emptyMessage,
103
85
errorMessage,
104
86
infoMessage,
105
87
loadingMessage : ''
106
88
} )
107
- if ( active ) {
108
- const index = ( active instanceof Number ) ? active :
109
- ( active instanceof Function ) ? items . findIndex ( active ) :
110
- items . findIndex ( item => item == active )
111
- selector . selectIndex ( index )
112
- }
89
+ if ( active ) selectActiveItem ( selector , items , active )
90
+ }
91
+ if ( items . constructor == Promise ) {
92
+ items . then ( items => {
93
+ updateSelector ( items )
94
+ } ) . catch ( err => {
95
+ reject ( err )
96
+ selector . cancelSelection ( )
97
+ } )
98
+ } else {
99
+ updateSelector ( items )
113
100
}
114
101
} )
115
102
}
103
+
104
+ function selectActiveItem ( selector , items , active ) {
105
+ const index = ( active instanceof Number ) ? active :
106
+ ( active instanceof Function ) ? items . findIndex ( active ) :
107
+ ( items . length > 0 && items [ 0 ] . primary ) ? items . findIndex ( item => item . primary === active ) :
108
+ items . indexOf ( active )
109
+ if ( index === - 1 ) return // do nothing
110
+ selector . selectIndex ( index )
111
+ }
0 commit comments