From 48892d0d24b3200650a64889dcaba3a706b50912 Mon Sep 17 00:00:00 2001 From: Tomoya Kashifuku Date: Thu, 25 May 2023 21:03:23 +0900 Subject: [PATCH 1/4] add more radio buttons for test --- tests/event/behavior/keydown.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/event/behavior/keydown.ts b/tests/event/behavior/keydown.ts index 42feb6c0..847e4215 100644 --- a/tests/event/behavior/keydown.ts +++ b/tests/event/behavior/keydown.ts @@ -317,6 +317,8 @@ cases( + + `, From 563f8f8d8a4e128fb339313b6a0b161a48290448 Mon Sep 17 00:00:00 2001 From: Tomoya Kashifuku Date: Thu, 25 May 2023 21:14:17 +0900 Subject: [PATCH 2/4] fix the test casess as they should be --- tests/event/behavior/keydown.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/event/behavior/keydown.ts b/tests/event/behavior/keydown.ts index 847e4215..deb5f6a6 100644 --- a/tests/event/behavior/keydown.ts +++ b/tests/event/behavior/keydown.ts @@ -362,14 +362,14 @@ cases( expectedTarget: '//input[@value="a"]', }, 'forward around the corner': { - focus: '//input[@value="d"]', + focus: '//input[@value="f"]', key: 'ArrowRight', expectedTarget: '//input[@value="a"]', }, 'backward around the corner': { focus: '//input[@value="a"]', key: 'ArrowUp', - expectedTarget: '//input[@value="d"]', + expectedTarget: '//input[@value="f"]', }, 'do nothing on single radio': { focus: '//input[@name="solo"]', From 24539b482818829c0305cbfeca27ca7d7f2045e3 Mon Sep 17 00:00:00 2001 From: Tomoya Kashifuku Date: Thu, 25 May 2023 22:26:41 +0900 Subject: [PATCH 3/4] fix the logic of walkRadio function --- src/event/radio.ts | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/event/radio.ts b/src/event/radio.ts index 9c31098a..8d3fa284 100644 --- a/src/event/radio.ts +++ b/src/event/radio.ts @@ -15,18 +15,26 @@ export function walkRadio( : `input[type="radio"][name=""], input[type="radio"]:not([name])`, ), ) - for (let i = group.findIndex(e => e === el) + direction; ; i += direction) { - if (!group[i]) { - i = direction > 0 ? 0 : group.length - 1 - } - if (group[i] === el) { - return + + let indexOfRadiogroup = group.findIndex(e => e === el) + do { + // move to the next element + indexOfRadiogroup += direction + if (!group[indexOfRadiogroup]) { + indexOfRadiogroup = direction > 0 ? 0 : group.length - 1 } - if (isDisabled(group[i])) { + const element = group[indexOfRadiogroup] + + if (isDisabled(element)) { continue } - focusElement(group[i]) - instance.dispatchUIEvent(group[i], 'click') - } + if (element === el) { + // If there is only one available radiobutton element, do nothing + } else { + focusElement(element) + instance.dispatchUIEvent(element, 'click') + } + break + } while (true) } From 5e9fd2b83a4c1f996237f2d32e90ed083c500468 Mon Sep 17 00:00:00 2001 From: Tomoya Kashifuku Date: Thu, 25 May 2023 22:28:47 +0900 Subject: [PATCH 4/4] fix direction --- src/event/behavior/keydown.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/event/behavior/keydown.ts b/src/event/behavior/keydown.ts index 6927deef..1305ca0c 100644 --- a/src/event/behavior/keydown.ts +++ b/src/event/behavior/keydown.ts @@ -28,7 +28,7 @@ const keydownBehavior: { ArrowDown: (event, target, instance) => { /* istanbul ignore else */ if (isElementType(target, 'input', {type: 'radio'} as const)) { - return () => walkRadio(instance, target, -1) + return () => walkRadio(instance, target, 1) } }, ArrowLeft: (event, target, instance) => { @@ -46,7 +46,7 @@ const keydownBehavior: { ArrowUp: (event, target, instance) => { /* istanbul ignore else */ if (isElementType(target, 'input', {type: 'radio'} as const)) { - return () => walkRadio(instance, target, 1) + return () => walkRadio(instance, target, -1) } }, Backspace: (event, target, instance) => {