@testing-library/user-event
Version:
Fire events the same way the user does
25 lines (22 loc) • 886 B
JavaScript
import { dispatchUIEvent } from '../../event/index.js';
import { focus } from '../focus/focus.js';
import { getWindow } from '../misc/getWindow.js';
import { isDisabled } from '../misc/isDisabled.js';
function walkRadio(config, el, direction) {
const window = getWindow(el);
const group = Array.from(el.ownerDocument.querySelectorAll(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');
}
}
export { walkRadio };