Skip to content

Commit 615cacd

Browse files
authored
Merge pull request #723 from jsx-eslint/fix-634
[fix:635] Ignore control elements that are hidden
2 parents 592c2cc + 2b7f6b7 commit 615cacd

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

__tests__/src/rules/control-has-associated-label-test.js

+12
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,15 @@ ruleTester.run(`${ruleName}:strict`, rule, {
311311
.map(ruleOptionsMapperFactory(strictOptions))
312312
.map(parserOptionsMapper),
313313
});
314+
315+
ruleTester.run(`${ruleName}:no-config`, rule, {
316+
valid: [
317+
{ code: '<input type="hidden" />' },
318+
{ code: '<input type="text" aria-hidden="true" />' },
319+
]
320+
.map(parserOptionsMapper),
321+
invalid: [
322+
{ code: '<input type="text" />', errors: [expectedError] },
323+
]
324+
.map(parserOptionsMapper),
325+
});

src/rules/control-has-associated-label.js

+6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import includes from 'array-includes';
1515
import { generateObjSchema, arraySchema } from '../util/schemas';
1616
import type { ESLintContext } from '../../flow/eslint';
1717
import isDOMElement from '../util/isDOMElement';
18+
import isHiddenFromScreenReader from '../util/isHiddenFromScreenReader';
1819
import isInteractiveElement from '../util/isInteractiveElement';
1920
import isInteractiveRole from '../util/isInteractiveRole';
2021
import mayHaveAccessibleLabel from '../util/mayHaveAccessibleLabel';
@@ -67,10 +68,15 @@ module.exports = {
6768
}
6869
const props = node.openingElement.attributes;
6970
const nodeIsDOMElement = isDOMElement(tag);
71+
const nodeIsHiddenFromScreenReader = isHiddenFromScreenReader(tag, props);
7072
const nodeIsInteractiveElement = isInteractiveElement(tag, props);
7173
const nodeIsInteractiveRole = isInteractiveRole(tag, props);
7274
const nodeIsControlComponent = controlComponents.indexOf(tag) > -1;
7375

76+
if (nodeIsHiddenFromScreenReader) {
77+
return;
78+
}
79+
7480
let hasAccessibleLabel = true;
7581
if (
7682
nodeIsInteractiveElement

0 commit comments

Comments
 (0)