-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(keyboard): change radio group per arrow keys
- Loading branch information
1 parent
73e4347
commit e1fa521
Showing
4 changed files
with
138 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import {dispatchUIEvent} from '../../event' | ||
import {Config} from '../../setup' | ||
import {focus} from '../focus/focus' | ||
import {getWindow} from '../misc/getWindow' | ||
import {isDisabled} from '../misc/isDisabled' | ||
|
||
export function walkRadio( | ||
config: Config, | ||
el: HTMLInputElement & {type: 'radio'}, | ||
direction: -1 | 1, | ||
) { | ||
const window = getWindow(el) | ||
const group = Array.from( | ||
el.ownerDocument.querySelectorAll<HTMLInputElement & {type: 'radio'}>( | ||
el.name | ||
? `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 | ||
} | ||
|
||
focus(group[i]) | ||
dispatchUIEvent(config, group[i], 'click') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters