@@ -98,6 +98,7 @@ export class PickerBase extends SizedMixin(Focusable) {
9898 public menuItems : MenuItem [ ] = [ ] ;
9999 private restoreChildren ?: Function ;
100100
101+ @query ( 'sp-menu' , true ) // important to cache since this can get reparented
101102 public optionsMenu ! : Menu ;
102103
103104 /**
@@ -272,7 +273,7 @@ export class PickerBase extends SizedMixin(Focusable) {
272273 placement : this . placement ,
273274 receivesFocus : 'auto' ,
274275 } ) ;
275- await this . manageSelection ( ) ;
276+ this . manageSelection ( ) ;
276277 this . menuStateResolver ( ) ;
277278 }
278279
@@ -357,23 +358,20 @@ export class PickerBase extends SizedMixin(Focusable) {
357358 protected firstUpdated ( changedProperties : PropertyValues ) : void {
358359 super . firstUpdated ( changedProperties ) ;
359360
360- // Since the sp-menu gets reparented by the popover, initialize it here
361- this . optionsMenu = this . shadowRoot . querySelector ( 'sp-menu' ) as Menu ;
362-
363361 const deprecatedMenu = this . querySelector ( 'sp-menu' ) ;
364362 if ( deprecatedMenu ) {
365363 console . warn (
366364 `Deprecation Notice: You no longer need to provide an sp-menu child to ${ this . tagName . toLowerCase ( ) } . Any styling or attributes on the sp-menu will be ignored.`
367365 ) ;
368366 }
369- this . menuItems = [
370- ...this . querySelectorAll ( `sp-menu-item` ) ,
371- ] as MenuItem [ ] ;
372367 }
373368
374369 protected updated ( changedProperties : PropertyValues ) : void {
375370 super . updated ( changedProperties ) ;
376- if ( changedProperties . has ( 'value' ) && ! changedProperties . has ( 'selectedItem' ) ) {
371+ if (
372+ changedProperties . has ( 'value' ) &&
373+ ! changedProperties . has ( 'selectedItem' )
374+ ) {
377375 this . manageSelection ( ) ;
378376 }
379377 if ( changedProperties . has ( 'disabled' ) && this . disabled ) {
@@ -394,7 +392,7 @@ export class PickerBase extends SizedMixin(Focusable) {
394392 }
395393 }
396394
397- protected async manageSelection ( ) : Promise < void > {
395+ protected manageSelection ( ) : void {
398396 /* c8 ignore next 3 */
399397 if ( this . menuItems . length > 0 ) {
400398 let selectedItem : MenuItem | undefined ;
@@ -417,12 +415,6 @@ export class PickerBase extends SizedMixin(Focusable) {
417415 }
418416 return ;
419417 }
420- if ( this . open ) {
421- await this . optionsMenu . updateComplete ;
422- if ( this . optionsMenu . menuItems . length ) {
423- this . manageSelection ( ) ;
424- }
425- }
426418 }
427419
428420 private menuStatePromise = Promise . resolve ( ) ;
@@ -433,6 +425,15 @@ export class PickerBase extends SizedMixin(Focusable) {
433425 await this . menuStatePromise ;
434426 }
435427
428+ public connectedCallback ( ) : void {
429+ if ( ! this . open ) {
430+ this . menuItems = [
431+ ...this . querySelectorAll ( `sp-menu-item` ) ,
432+ ] as MenuItem [ ] ;
433+ }
434+ super . connectedCallback ( ) ;
435+ }
436+
436437 public disconnectedCallback ( ) : void {
437438 this . open = false ;
438439
@@ -451,32 +452,25 @@ export class Picker extends PickerBase {
451452 return ;
452453 }
453454 event . preventDefault ( ) ;
454- /* c8 ignore next 3 */
455- if ( ! this . optionsMenu ) {
456- return ;
457- }
458455 if ( code === 'ArrowUp' || code === 'ArrowDown' ) {
459456 this . open = true ;
460457 return ;
461458 }
462459 const selectedIndex = this . selectedItem
463- ? this . optionsMenu . menuItems . indexOf ( this . selectedItem )
460+ ? this . menuItems . indexOf ( this . selectedItem )
464461 : - 1 ;
465462 // use a positive offset to find the first non-disabled item when no selection is available.
466463 const nextOffset = ! this . value || code === 'ArrowRight' ? 1 : - 1 ;
467464 let nextIndex = selectedIndex + nextOffset ;
468465 while (
469- this . optionsMenu . menuItems [ nextIndex ] &&
470- this . optionsMenu . menuItems [ nextIndex ] . disabled
466+ this . menuItems [ nextIndex ] &&
467+ this . menuItems [ nextIndex ] . disabled
471468 ) {
472469 nextIndex += nextOffset ;
473470 }
474- nextIndex = Math . max (
475- Math . min ( nextIndex , this . optionsMenu . menuItems . length ) ,
476- 0
477- ) ;
471+ nextIndex = Math . max ( Math . min ( nextIndex , this . menuItems . length ) , 0 ) ;
478472 if ( ! this . value || nextIndex !== selectedIndex ) {
479- this . setValueFromItem ( this . optionsMenu . menuItems [ nextIndex ] ) ;
473+ this . setValueFromItem ( this . menuItems [ nextIndex ] ) ;
480474 }
481475 } ;
482476}
0 commit comments