Skip to content

Commit 9d6f4f0

Browse files
committed
fix: simplify optionsMenu usage and fix tests
1 parent 177c887 commit 9d6f4f0

File tree

2 files changed

+32
-33
lines changed

2 files changed

+32
-33
lines changed

packages/picker/src/Picker.ts

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

packages/split-button/src/SplitButton.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
CSSResultArray,
1515
TemplateResult,
1616
property,
17+
PropertyValues,
1718
html,
1819
query,
1920
ifDefined,
@@ -158,7 +159,14 @@ export class SplitButton extends SizedMixin(PickerBase) {
158159
`;
159160
}
160161

161-
protected async manageSelection(): Promise<void> {
162+
protected updated(changedProperties: PropertyValues): void {
163+
super.updated(changedProperties);
164+
if (changedProperties.has('value')) {
165+
this.manageSplitButtonItems();
166+
}
167+
}
168+
169+
protected manageSelection(): void {
162170
super.manageSelection();
163171
this.manageSplitButtonItems();
164172
}
@@ -167,14 +175,11 @@ export class SplitButton extends SizedMixin(PickerBase) {
167175
if (this.menuItems.length) {
168176
if (this.type === 'more') {
169177
this.menuItems[0].hidden = true;
170-
this.menuItems.forEach(
171-
(el) => (el.selected = false)
172-
);
178+
this.menuItems.forEach((el) => (el.selected = false));
173179
this.menuItems[0].selected = true;
174180
this.selectedItem = this.menuItems[0];
175181
} else {
176-
this.selectedItem =
177-
this.selectedItem || this.menuItems[0];
182+
this.selectedItem = this.selectedItem || this.menuItems[0];
178183
this.selectedItem.selected = true;
179184
}
180185
return;

0 commit comments

Comments
 (0)