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

feat: upgrade focus-trap-js to support focus on radio elements #447

Merged
merged 2 commits into from Nov 8, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 package.json
Expand Up @@ -55,11 +55,11 @@
"size-limit": [
{
"path": "dist/react-responsive-modal.cjs.production.min.js",
"limit": "3.1 KB"
"limit": "3.3 KB"
},
{
"path": "dist/react-responsive-modal.esm.js",
"limit": "3.1 KB"
"limit": "3.3 KB"
}
],
"dependencies": {
Expand Down
31 changes: 29 additions & 2 deletions src/lib/focusTrapJs.ts
@@ -1,4 +1,4 @@
// https://github.com/alexandrzavalii/focus-trap-js/blob/master/src/index.js v1.0.9
// https://github.com/alexandrzavalii/focus-trap-js/blob/master/src/index.js v1.1.0

export const candidateSelectors = [
'input',
Expand All @@ -20,12 +20,39 @@ function isHidden(node: any) {
);
}

function getCheckedRadio(nodes: any, form: any) {
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].checked && nodes[i].form === form) {
return nodes[i];
}
}
}

function isNotRadioOrTabbableRadio(node: any) {
if (node.tagName !== 'INPUT' || node.type !== 'radio' || !node.name) {
return true;
}
var radioScope = node.form || node.ownerDocument;
var radioSet = radioScope.querySelectorAll(
'input[type="radio"][name="' + node.name + '"]'
);
var checked = getCheckedRadio(radioSet, node.form);
return checked === node || (checked === undefined && radioSet[0] === node);
}

export function getAllTabbingElements(parentElem: any) {
var currentActiveElement = document.activeElement;
var tabbableNodes = parentElem.querySelectorAll(candidateSelectors.join(','));
var onlyTabbable = [];
for (var i = 0; i < tabbableNodes.length; i++) {
var node = tabbableNodes[i];
if (!node.disabled && getTabindex(node) > -1 && !isHidden(node)) {
if (
currentActiveElement === node ||
(!node.disabled &&
getTabindex(node) > -1 &&
!isHidden(node) &&
isNotRadioOrTabbableRadio(node))
) {
onlyTabbable.push(node);
}
}
Expand Down