How to pass useTextField props to parent element instead of input element in React form using React Aria? #4522
-
I'm trying to use the here is how I would pass the props to the parent element: import React from "react";
const App = () => {
return (
<Form.Group
label="some-label"
errorMessage="message"
description="help"
className="mb-0"
>
<Form.Input
placeholder="placeholder"
onChange={(e) => {}}
required
/>
</Form.Group>
);
};
export default App; in this way, I could handle the error message, description, and everything common in one place which is the parent element, and then I can focus on the input, select, checkbox, etc... behavior in separate components instead of having to handle everything in common in each component and here is how I should pass the props if I used import React from "react";
const App = () => {
return (
<Form.Group
className="mb-0"
>
<Form.Input
label="some-label"
errorMessage="message"
description="help"
placeholder="placeholder"
onChange={(e) => {}}
required
/>
</Form.Group>
);
};
export default App; but I found out that I have to handle things like error messages in every single component I have Any help you can provide would be greatly appreciated. Thank you! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I don't quite understand how your ideal case works. What happens if you have multiple inputs in the group? How do you distinguish which message/label/etc gets applied to each input? The way each of those hooks (ie useTextfield) works, is it generates an id for the label, an id for the description, an ide for the error message. And then it applies all those ids to the field in their correct aria attributes. It also generates an id for the field, and applies it to the correct attributes for the label. So there is a lot of interconnectivity for it to be accessible. I think instead of handling it at the group, you should create a common wrapper for each input. We do this in React Spectrum components with the Field component. |
Beta Was this translation helpful? Give feedback.
I don't quite understand how your ideal case works. What happens if you have multiple inputs in the group? How do you distinguish which message/label/etc gets applied to each input? The way each of those hooks (ie useTextfield) works, is it generates an id for the label, an id for the description, an ide for the error message. And then it applies all those ids to the field in their correct aria attributes. It also generates an id for the field, and applies it to the correct attributes for the label. So there is a lot of interconnectivity for it to be accessible.
I think instead of handling it at the group, you should create a common wrapper for each input. We do this in React Spectrum com…