Hi, do you have any tips for using Form components inside DynamicTable rows? I’ve found that:
- The default values aren’t visible on these Textfield components
- Their values aren’t retrievable with getValues and handleSubmit - and changes aren’t recognised in the formState object.
Here’s some code that demonstrates the issue.
import React from 'react';
import ForgeReconciler, { useForm, DynamicTable, Form, Label, Textfield, Button } from '@forge/react';
import { head, presidents } from './data';
const App = () => {
const presidentEntries = Object.fromEntries(
presidents.flatMap((president: { id: number; name: string; party: string; term: string; }) => [
[`president.${president.id}.name`, president.name],
[`president.${president.id}.party`, president.party],
[`president.${president.id}.term`, president.term],
])
);
presidentEntries['president.writein.name'] = 'Steamboat Mickey'
const { register, handleSubmit, getFieldId, getValues, formState } = useForm({defaultValues: presidentEntries});
const formRows = presidents.map( (president, index) => ({
key: `row-${index}`,
cells: [
{
key: `row-${index}-name`,
content: <Textfield {...register(`president.${president.id}.name`), {appearance: "subtle"}} />,
},
{
key: `row-${index}-party`,
content: <Textfield {...register(`president.${president.id}.party`), {appearance: "subtle"}} />,
},
{
key: `row-${index}-term`,
content: <Textfield {...register(`president.${president.id}.term`), {appearance: "subtle"}} />,
},
],
}));
return (
<Form onSubmit={handleSubmit((data) => {console.log(data); console.log(getValues('president')); console.log(formState)})}>
<DynamicTable
caption="List of US Presidents"
head={head}
rows={formRows}
/>
<Label labelFor={getFieldId('president.writein.name')}>Your write-in president</Label>
<Textfield {...register('president.writein.name')} />
<Button appearance="primary" type="submit">Submit</Button>
</Form>
);
};
ForgeReconciler.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
On submission, the formState is reporting changes to the ‘president.writein.name’ field but not to any changes to fields in the DynamicTable.
{
"errors": {},
"isSubmitted": true,
"isSubmitSuccessful": true,
"isSubmitting": false,
"submitCount": 2,
"isValid": true,
"dirtyFields": {
"president": {
"writein": {
"name": true
}
}
},
"touchedFields": {
"president": {
"writein": {
"name": true
}
}
}
}
Is there something that I can do to make DynamicTable play more nicely with Form?