Skip to content

Commit d51f8e0

Browse files
authored
feat: upgrade focus-trap-js to support focus on radio elements (#447)
1 parent cf4b6b3 commit d51f8e0

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@
5555
"size-limit": [
5656
{
5757
"path": "dist/react-responsive-modal.cjs.production.min.js",
58-
"limit": "3.1 KB"
58+
"limit": "3.3 KB"
5959
},
6060
{
6161
"path": "dist/react-responsive-modal.esm.js",
62-
"limit": "3.1 KB"
62+
"limit": "3.3 KB"
6363
}
6464
],
6565
"dependencies": {

src/lib/focusTrapJs.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// https://github.com/alexandrzavalii/focus-trap-js/blob/master/src/index.js v1.0.9
1+
// https://github.com/alexandrzavalii/focus-trap-js/blob/master/src/index.js v1.1.0
22

33
export const candidateSelectors = [
44
'input',
@@ -20,12 +20,39 @@ function isHidden(node: any) {
2020
);
2121
}
2222

23+
function getCheckedRadio(nodes: any, form: any) {
24+
for (var i = 0; i < nodes.length; i++) {
25+
if (nodes[i].checked && nodes[i].form === form) {
26+
return nodes[i];
27+
}
28+
}
29+
}
30+
31+
function isNotRadioOrTabbableRadio(node: any) {
32+
if (node.tagName !== 'INPUT' || node.type !== 'radio' || !node.name) {
33+
return true;
34+
}
35+
var radioScope = node.form || node.ownerDocument;
36+
var radioSet = radioScope.querySelectorAll(
37+
'input[type="radio"][name="' + node.name + '"]'
38+
);
39+
var checked = getCheckedRadio(radioSet, node.form);
40+
return checked === node || (checked === undefined && radioSet[0] === node);
41+
}
42+
2343
export function getAllTabbingElements(parentElem: any) {
44+
var currentActiveElement = document.activeElement;
2445
var tabbableNodes = parentElem.querySelectorAll(candidateSelectors.join(','));
2546
var onlyTabbable = [];
2647
for (var i = 0; i < tabbableNodes.length; i++) {
2748
var node = tabbableNodes[i];
28-
if (!node.disabled && getTabindex(node) > -1 && !isHidden(node)) {
49+
if (
50+
currentActiveElement === node ||
51+
(!node.disabled &&
52+
getTabindex(node) > -1 &&
53+
!isHidden(node) &&
54+
isNotRadioOrTabbableRadio(node))
55+
) {
2956
onlyTabbable.push(node);
3057
}
3158
}

0 commit comments

Comments
 (0)