T
TanStack10mo ago
multiple-amethyst

React Aria Components Integration for TanStack Form

TanStack Forms currently does not expose a field ref, which makes it challenging to integrate with React Aria components. React Aria relies on assigning a ref to the input component for accessibility and focus management, but TanStack Forms lacks documentation or examples for this integration. Please provide an integration guide or example in the documentation for using TanStack Forms with React Aria components, similar to how React Hook Form handles this scenario. Here's an example of how React Hook Form integrates with React Aria:
import { Controller, useForm } from 'react-hook-form';
import { Button, FieldError, Form, Input, Label, TextField } from 'react-aria-components';

function App() {
let { handleSubmit, control } = useForm({
defaultValues: {
name: ''
}
});
let onSubmit = (data) => {
// Call your API here...
};

return (
<Form onSubmit={handleSubmit(onSubmit)}>
<Controller
control={control}
name="name"
rules={{ required: 'Name is required.' }}
render={({
field: { name, value, onChange, onBlur, ref },
fieldState: { invalid, error }
}) => (
<TextField
name={name}
value={value}
onChange={onChange}
onBlur={onBlur}
isRequired
validationBehavior="aria"
isInvalid={invalid}
>
<Label>Name</Label>
<Input ref={ref} />
<FieldError>{error?.message}</FieldError>
</TextField>
)}
/>
<Button type="submit">Submit</Button>
</Form>
);
}
import { Controller, useForm } from 'react-hook-form';
import { Button, FieldError, Form, Input, Label, TextField } from 'react-aria-components';

function App() {
let { handleSubmit, control } = useForm({
defaultValues: {
name: ''
}
});
let onSubmit = (data) => {
// Call your API here...
};

return (
<Form onSubmit={handleSubmit(onSubmit)}>
<Controller
control={control}
name="name"
rules={{ required: 'Name is required.' }}
render={({
field: { name, value, onChange, onBlur, ref },
fieldState: { invalid, error }
}) => (
<TextField
name={name}
value={value}
onChange={onChange}
onBlur={onBlur}
isRequired
validationBehavior="aria"
isInvalid={invalid}
>
<Label>Name</Label>
<Input ref={ref} />
<FieldError>{error?.message}</FieldError>
</TextField>
)}
/>
<Button type="submit">Submit</Button>
</Form>
);
}
Thank you in Advance.
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?