Replies: 1 comment 6 replies
-
I figured I'd leave my solution here. I was able to successfully integrate RHF and tanstack-table using First declare your types: interface TableProps {
name: string;
}
type FormProps = {
table: TableProps[];
}; Then add some data. Note that the first object is just empty to provide a default form to add new items: const dummyData: DraftTableProps[] = [
{
name: '',
},
{
name: 'John Doe',
},
]; Then create the table component: const Table = () => {
// Notice how I am rendering an input within the cell and registering it via the row index.
// Feel free to put controllers here or whatever.
const columns: ColumnDef<DraftTableProps>[] = [
{
accessorKey: 'name',
header: "Name",
cell: ({ row }) => {
return (
<Input
{...register(`table.${row.index}.name`, {
required: 'Required'
})}
placeholder="Draft Name"
/> )
},
},
// An example of a nested field
{
accessorKey: 'location',
header: "Location",
cell: ({ row }) => {
return (
<Popover>
<PopoverTrigger asChild>
<Button variant="outline" small>
Location
</Button>
</PopoverTrigger>
<PopoverContent>
<Input {...register(`table.${row.index}.location.building`)} placeholder="Building" />
<Input {...register(`table.${row.index}.location.floor`)} placeholder="Floor" />
<Input {...register(`table.${row.index}.location.room`)} placeholder="Room" />
</PopoverContent>
</Popover>
);
},
// Here is where you can add a button to submit an individual row.
{
id: 'actions',
cell: ({ row }) => {
return (
<>
<Button
variant="outline"
type="submit"
small
onClick={handleSubmit((data) => onSubmitSingle(data.table[row.index]))}
>
Submit Individual
</Button>
<Button variant="outline" small onClick={() => remove(row.index)}>
Remove
</Button>
</>
);
}
},
];
// Instantiate the form hook.
const { register, handleSubmit, control, clearErrors } = useForm<DraftFormProps>({
defaultValues: {
table: dummyData
}
});
const { fields, remove } = useFieldArray({
name: 'table',
control
});
// Instantiate the table instance and pass in the field array directly into the table.
const table = useReactTable({
data: fields,
columns,
getCoreRowModel: getCoreRowModel(),
rowCount: dummyData.length || 0,
pageCount: 1,
getRowId: (row) => row.name,
});
// Submit handlers
const onSubmitSingle = (data: DraftTableProps) => {
console.log(data);
};
const onSubmit: SubmitHandler<DraftFormProps> = (data) => {
console.log(data);
};
// Render the form and the table within.
return (
<form onSubmit={handleSubmit(onSubmit)}>
<Table></Table>
</form>
)
} Hope this helps anyone else looking for this. Edit: I may have skipped some steps but you made need to instantiate multiple useForm hooks depending on how you set up your rows. |
Beta Was this translation helpful? Give feedback.
-
Hello, I'm working on a project where I need to handle form submissions at the row level of a table, utilizing React Hook Form and Tanstack Table. My table has columns for "Phone Number," "Name," "Date," "Region," and "Actions." The "Actions" column contains sticky buttons for "Edit," "Save," and "Cancel."
In view mode, all columns display their values, with the "Actions" column featuring only an "Edit" button. Upon entering the edit mode by clicking "Edit," the "Actions" column switches to show "Save" and "Cancel" buttons. Meanwhile, other columns convert their content to input components or other custom components suitable for editing.
I've managed to implement sticky buttons and dynamically switch between display and edit modes. However, I'm uncertain about integrating the React Hook Form (RHF) state effectively. My custom components are designed to work with RHF when a controller is passed to them. Initially, I thought about setting up the RHF state before rendering and passing the entire useForm object to the Tanstack Table meta. Nonetheless, this approach seems potentially complex, especially when it comes to managing connections to sticky buttons and handling errors.
Does anyone have recommendations or best practices on how to achieve this functionality in a straightforward and understandable manner, particularly with emphasis on error handling and state management for individual rows using React Hook Form?
Beta Was this translation helpful? Give feedback.
All reactions