Skip to content

Commit 8314a54

Browse files
committed
refactor(action-sheet): use button map
1 parent 73149c9 commit 8314a54

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

core/src/components/action-sheet/action-sheet.tsx

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -385,12 +385,32 @@ export class ActionSheet implements ComponentInterface, OverlayInterface {
385385
const radios = Array.from(this.el.querySelectorAll('.action-sheet-button[role="radio"]')).filter(
386386
(el) => !(el as HTMLButtonElement).disabled
387387
) as HTMLButtonElement[];
388-
389388
const currentIndex = radios.findIndex((radio) => radio.id === target.id);
389+
390390
if (currentIndex === -1) {
391391
return;
392392
}
393393

394+
const allButtons = this.getButtons();
395+
const radioButtons = this.getRadioButtons();
396+
/**
397+
* Build a map of button element IDs to their ActionSheetButton
398+
* config objects.
399+
* This allows us to quickly look up which button config corresponds
400+
* to a DOM element when handling keyboard navigation
401+
* (e.g., whenuser presses Space/Enter or arrow keys).
402+
* The key is the ID that was set on the DOM element during render,
403+
* and the value is the ActionSheetButton config that contains the
404+
* handler and other properties.
405+
*/
406+
const buttonIdMap = new Map<string, ActionSheetButton>();
407+
408+
radioButtons.forEach((b) => {
409+
const allIndex = allButtons.indexOf(b);
410+
const buttonId = this.getButtonId(b, allIndex);
411+
buttonIdMap.set(buttonId, b);
412+
});
413+
394414
let nextEl: HTMLButtonElement | undefined;
395415

396416
if (['ArrowDown', 'ArrowRight'].includes(ev.key)) {
@@ -407,33 +427,20 @@ export class ActionSheet implements ComponentInterface, OverlayInterface {
407427
ev.preventDefault();
408428
ev.stopPropagation();
409429

410-
const allButtons = this.getButtons();
411-
const radioButtons = this.getRadioButtons();
412-
const buttonIndex = radioButtons.findIndex((b) => {
413-
const buttonId = this.getButtonId(b, allButtons.indexOf(b));
414-
return buttonId === target.id;
415-
});
416-
417-
if (buttonIndex !== -1) {
418-
this.selectRadioButton(radioButtons[buttonIndex]);
419-
this.buttonClick(radioButtons[buttonIndex]);
430+
const button = buttonIdMap.get(target.id);
431+
if (button) {
432+
this.selectRadioButton(button);
433+
this.buttonClick(button);
420434
}
421435

422436
return;
423437
}
424438

425439
// Focus the next radio button
426440
if (nextEl) {
427-
const allButtons = this.getButtons();
428-
const radioButtons = this.getRadioButtons();
429-
430-
const buttonIndex = radioButtons.findIndex((b) => {
431-
const buttonId = this.getButtonId(b, allButtons.indexOf(b));
432-
return buttonId === nextEl?.id;
433-
});
434-
435-
if (buttonIndex !== -1) {
436-
this.selectRadioButton(radioButtons[buttonIndex]);
441+
const button = buttonIdMap.get(nextEl.id);
442+
if (button) {
443+
this.selectRadioButton(button);
437444
nextEl.focus();
438445
}
439446
}

0 commit comments

Comments
 (0)