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 tag should allow for htmlFor #1651
Comments
Use of |
The web label behavior doesn't happen on native either |
This asks a more general question then, should this library allow to do as much as the web does, or not? An alternative would then be to have a My question is does this library plan on limiting behaviour that is cross-platform only? Meaning anything that is web based will be dismissed? My opinion is that it shouldn't, as the platforms are inherently different, and the code should be able(as much as possible) to provide functionality for both, rather than limiting to a subset. This said, I really appreciate the work you and others did on this, and hoping it can fit my entire project! |
I'd like to consider re-opening this. I'm doing accessibility testing of an R.N. project. The HTML output from react-native-web does a really good job of producing accessible code and mapping R.N. to wai-aria. However, the fact that the html 'labels' can't be clicked to put focus into the associated form field means that I can't use the generated web version, as it breaks user expectation and prohibits WCAG validation. Given that React native web 'knows' which fields are associated with which 'labels' (as it takes the field's aria-label text from the contents of the labelling div) it would seem to be possible for it to (a) generate an id to uniquely identify the field, and use label for="generatedID" instead of div for the label (perhaps as a switch that can be turned on for those who need it, so it doesn't much up CSS or markup of existing projects?) |
Hi Bruce,
Can you share the RN code snippet that's being run on web?
This can be re-implemented in JavaScript, which will make the behavior cross-platform too.
React DOM might know that internally, but it doesn't expose any way for us to get that information.
This isn't straight-forward if there's server rendering and client hydration involved. React was meant to add a new hook to help with this, but it's caught in limbo and hasn't been shipped.
You can still output a I like the idea in principle of inferring the A different option is to drop down to the // Label.js (for native)
import {Text} from 'react-native';
export function Label({ for, ...props }) {
return <Text {...props} />
}
// Label.web.js (for web)
import {unstable_createElement} from 'react-native-web';
export function Label({ for, ...props }) {
return unstable_createElement('label', { htmlFor: for, ...props });
} This would allow you to use the same Another option is to set the DOM attribute in JS using a ref callback, but this only works for client-side rendering: const ref = React.useCallback((node) => {
if (Platform.OS === 'web' && node != null) {
node.setAttribute('for', id);
}
}, []);
return (
<>
<Text ref={ref}>label</Text>
<TextInput nativeID={id} />
</>
) |
Is your feature request related to a problem? Please describe.
accessibilityRole
allows to have better tags, such as<label>
, along with aria labels. One thing that is missing is thefor
attribute, which allows for the user to click on the label to focus the field.Describe a solution you'd like
Passing htmlFor to a
<Text accessibilityRole="label" htmlFor="email">Email</Text>
would yield the expected htmlAdditional context
While it doesn't concern UX directly, I use of capybara for my automated e2e testing, which heavily relies on this feature to interact with forms
I'm currently patching a follows:
I'll submit a PR ASAP for this, hopefully it makes sense to add it to the lib!
The text was updated successfully, but these errors were encountered: