@@ -79,19 +79,8 @@ export class UUIComboboxListElement extends LitElement {
79
79
@state ( )
80
80
private _value : FormDataEntryValue | FormData = '' ;
81
81
82
- private __activeElement : UUIComboboxListOptionElement | undefined ;
83
- private get _activeElement ( ) : UUIComboboxListOptionElement | undefined {
84
- return this . __activeElement ;
85
- }
86
- private set _activeElement ( el : UUIComboboxListOptionElement | undefined ) {
87
- if ( this . __activeElement ) {
88
- this . __activeElement . active = false ;
89
- }
90
- if ( el ) {
91
- el . active = true ;
92
- this . __activeElement = el ;
93
- }
94
- }
82
+ @state ( )
83
+ private _activeElementValue : string | null = null ;
95
84
96
85
private _selectedElement : UUIComboboxListOptionElement | undefined ;
97
86
@@ -129,30 +118,35 @@ export class UUIComboboxListElement extends LitElement {
129
118
}
130
119
131
120
private _onSlotChange = ( ) => {
132
- this . _activeElement = undefined ;
133
121
// Get index from first active, remove active from the rest.
134
- for ( let i = 0 ; i < this . _activeOptions . length ; i ++ ) {
135
- if ( i === 0 ) {
136
- this . _activeElement = this . _activeOptions [ i ] ;
137
- } else {
138
- this . _activeOptions [ i ] . active = false ;
139
- }
140
- }
122
+ this . #updateActiveElement( ) ;
141
123
142
124
this . _updateSelection ( ) ;
143
125
this . dispatchEvent (
144
126
new UUIComboboxListEvent ( UUIComboboxListEvent . INNER_SLOT_CHANGE )
145
127
) ;
146
128
} ;
147
129
130
+ #updateActiveElement( ) {
131
+ for ( let i = 0 ; i < this . _activeOptions . length ; i ++ ) {
132
+ this . _activeOptions [ i ] . active = false ;
133
+ }
134
+
135
+ const activeElement = this . _getActiveElement ;
136
+ if ( activeElement ) {
137
+ activeElement . active = true ;
138
+ } else {
139
+ this . _goToIndex ( 0 ) ;
140
+ }
141
+ }
142
+
148
143
private _onSelected = ( e : Event ) => {
149
144
if ( this . _selectedElement ) {
150
145
this . _selectedElement . selected = false ;
151
146
this . _selectedElement . active = false ;
152
147
this . _selectedElement = undefined ;
153
148
}
154
149
this . _selectedElement = e . composedPath ( ) [ 0 ] as UUIComboboxListOptionElement ;
155
- this . _activeElement = this . _selectedElement ;
156
150
157
151
this . value = this . _selectedElement . value || '' ;
158
152
this . displayValue = this . _selectedElement . displayValue || '' ;
@@ -161,37 +155,48 @@ export class UUIComboboxListElement extends LitElement {
161
155
} ;
162
156
private _onDeselected = ( e : Event ) => {
163
157
const el = e . composedPath ( ) [ 0 ] as UUIComboboxListOptionElement ;
164
- if ( this . _activeElement === el ) {
165
- this . _activeElement = undefined ;
166
- }
167
158
if ( this . _selectedElement === el ) {
168
159
this . value = '' ;
169
160
this . displayValue = '' ;
170
161
this . dispatchEvent ( new UUIComboboxListEvent ( UUIComboboxListEvent . CHANGE ) ) ;
171
162
}
172
163
} ;
173
164
174
- private _getActiveIndex ( ) : number {
175
- return this . _activeElement
176
- ? this . _options . indexOf ( this . _activeElement )
177
- : - 1 ;
165
+ private get _getActiveIndex ( ) : number {
166
+ if ( this . _activeElementValue === null ) return - 1 ;
167
+
168
+ return this . _options . findIndex (
169
+ element => element . value === this . _activeElementValue
170
+ ) ;
171
+ }
172
+
173
+ private get _getActiveElement ( ) {
174
+ if ( this . _activeElementValue === null ) return null ;
175
+
176
+ return this . _options . find (
177
+ element => element . value === this . _activeElementValue
178
+ ) ;
178
179
}
179
180
180
181
private _moveIndex = ( distance : number ) => {
181
182
const newIndex = Math . min (
182
- Math . max ( this . _getActiveIndex ( ) + distance , 0 ) ,
183
+ Math . max ( this . _getActiveIndex + distance , 0 ) ,
183
184
this . _options . length - 1
184
185
) ;
185
186
186
187
this . _goToIndex ( newIndex ) ;
187
188
} ;
188
189
189
190
private _goToIndex ( index : number ) {
191
+ if ( this . _options . length === 0 ) return ;
192
+
190
193
index = Math . min ( Math . max ( index , 0 ) , this . _options . length - 1 ) ; // Makes sure the index stays within array length
191
- this . _activeElement = this . _options [ index ] ;
194
+ const activeElement = this . _options [ index ] ;
195
+ this . _activeElementValue = activeElement . value ;
196
+ this . #updateActiveElement( ) ;
192
197
193
- if ( this . _activeElement ) {
194
- this . _activeElement . scrollIntoView ( {
198
+ if ( activeElement ) {
199
+ activeElement . scrollIntoView ( {
195
200
behavior : 'auto' ,
196
201
block : 'nearest' ,
197
202
inline : 'nearest' ,
@@ -222,7 +227,7 @@ export class UUIComboboxListElement extends LitElement {
222
227
223
228
case 'Enter' : {
224
229
e . preventDefault ( ) ;
225
- this . _activeElement ?. click ( ) ;
230
+ this . _getActiveElement ?. click ( ) ;
226
231
break ;
227
232
}
228
233
0 commit comments