-
-
Notifications
You must be signed in to change notification settings - Fork 627
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
label-has-associated-control fails for identical string values of id and htmlFor #511
Comments
It's not really feasible for the linter rule to try to follow every variable in scope and try to figure out if it's the same or not. What happens if you put the ID in a variable, and use the same variable in both places? |
Thought that might fix it, but it still fails: const DotfilesCheckBox = (props: Props) => {
const { id, showDotfiles, toggleDotfiles } = props;
const inputId = `DotfilesCheckbox${id}`;
return (
<div
className="checkbox"
style={{
marginLeft: '1.0em',
}}
>
<input
checked={showDotfiles}
id={inputId}
type="checkbox"
onChange={toggleDotfiles}
/>
<label htmlFor={inputId}>Show Dotfiles</label>
</div>
);
}; |
I have the same issue with the code below: const Foo = () => {
const domId = 'foo';
return (
<div>
<label htmlFor={domId}>Foo</label>
<input type="text" id={domId} />
</div>
);
}; |
Yep, same issue. Contrary to @ljharb's suggestion, I actually don't think it's that unfeasible for the linter to be able to handle this, since it's a somewhat common use-case. I will start on a PR. |
@himynameisdave note i said “every” :-) a PR that can catch more cases would be very appreciated! |
And I have an even simpler case where linting fails:
Errors:
|
Nest the input in the label. |
Nope; doesn't solve the problem. |
ah, maybe because it's a button. buttons don't typically have labels because they contain their own CTA text, but maybe they should be considered? cc @jessebeach |
I have a very similar issue. With version 6.2.3 of the plugin the following code reports an error;
Please don't respond that I need to wrap the input in the label. I don't want this. In fact, my actual code is more complex, but even this simple example doesn't work |
@jeroenpeeters is your rule configured to allow the more lax arrangement of not properly wrapping the input in the label? By default i believe it requires both wrapping and linking. |
@ljharb That would be strange, because the documentation (https://github.com/evcohen/eslint-plugin-jsx-a11y/blob/master/docs/rules/label-has-associated-control.md) clearly states that;
The rule is not explicitly configured, so it must be defaults then. |
Are you extending any shared configs, like airbnb, that might be setting it? |
Having the same issue with the following code. I didn't think it was an HTML requirement to wrap inputs with labels -- more like an option? So maybe it should be an option in this linter rule as well
|
@nth-chile it's a requirement if you want to support 100% of assistive devices. It is an option for this linter rule, but the default is to do both. The second-best option is to nest, since the majority of devices support that. |
The following code fails on
jsx-a11y/label-has-associated-control
. Changingid
andhtmlFor
to string literals results in a pass.I could see this failing by design for mutable identifiers, but since these are defined as
const
, it seems safe to conclude that they will evaluate to identical strings if they are in the same block scope. I also realize there are limitations to what can be done statically.The text was updated successfully, but these errors were encountered: