How to make a label for? #215
-
I'm trying to make a label clickable (for web) to toggle a checkbox. Usually I'd use a label with a for attribute, but that doesn't seem possible. I know I can make it work by using javascript, but I'd rather not. Any suggestions? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
@RWOverdijk For the web, you need a For example: import * as React from 'react';
import { View } from 'react-native';
import { Checkbox } from '~/components/ui/checkbox';
import { Label } from '~/components/ui/label';
export default function CheckboxScreen() {
const [checked, setChecked] = React.useState(false);
return (
<View className='flex-1 justify-center items-center p-6 gap-12'>
<View className='flex-row gap-3 items-center'>
<Checkbox aria-labelledby='terms' checked={checked} onCheckedChange={setChecked} />
<Label
nativeID='terms'
>
Accept terms and conditions
</Label>
</View>
</View>
);
} Clicking the Label with toggle the checkbox on the web with this example. |
Beta Was this translation helpful? Give feedback.
-
@RWOverdijk The example I posted above works for me. If you check the first example here: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-labelledby that's how it works, with the Can you provide a minimal reproduction repo? |
Beta Was this translation helpful? Give feedback.
-
The Label primitive now has an |
Beta Was this translation helpful? Give feedback.
The Label primitive now has an
htmlFor
prop for the web only.