Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Radio inputs - fix arrow button functionality #1049

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/event/behavior/keydown.ts
Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand Down
22 changes: 8 additions & 14 deletions src/event/radio.ts
Expand Up @@ -14,19 +14,13 @@ export function walkRadio(
? `input[type="radio"][name="${window.CSS.escape(el.name)}"]`
: `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
}
if (isDisabled(group[i])) {
continue
}

focusElement(group[i])
instance.dispatchUIEvent(group[i], 'click')
).filter(elt => !isDisabled(elt))
// 1 or 0 radio buttons found
if (group.length <= 1) {
return;
}
// Multiple buttons found
const nextRadioElement = group[(group.indexOf(el) + direction + group.length) % group.length];
focusElement(nextRadioElement)
instance.dispatchUIEvent(nextRadioElement, 'click')
vgpena marked this conversation as resolved.
Show resolved Hide resolved
}
19 changes: 15 additions & 4 deletions tests/event/behavior/keydown.ts
Expand Up @@ -318,6 +318,7 @@ cases(
<input type="radio" name="group" value="c" disabled/>
<input type="radio" name="group" value="d"/>
<input type="radio" name="foo"/>
<input type="radio" name="group" value="e" />
<input type="text" name="group"/>
`,
{focus},
Expand Down Expand Up @@ -359,15 +360,25 @@ cases(
key: 'ArrowUp',
expectedTarget: '//input[@value="a"]',
},
'forward around the corner': {
focus: '//input[@value="d"]',
'ArrowRight around the corner': {
focus: '//input[@value="e"]',
key: 'ArrowRight',
expectedTarget: '//input[@value="a"]',
},
'backward around the corner': {
'ArrowLeft around the corner': {
focus: '//input[@value="a"]',
key: 'ArrowLeft',
expectedTarget: '//input[@value="e"]',
},
'ArrowUp around the corner': {
focus: '//input[@value="a"]',
key: 'ArrowUp',
expectedTarget: '//input[@value="d"]',
expectedTarget: '//input[@value="e"]',
},
'ArrowDown around the corner': {
focus: '//input[@value="e"]',
key: 'ArrowDown',
expectedTarget: '//input[@value="a"]',
},
ph-fritsche marked this conversation as resolved.
Show resolved Hide resolved
'do nothing on single radio': {
focus: '//input[@name="solo"]',
Expand Down