Skip to content

fix(react-form): fallback array field value during index change to prevent uncontrolled error #1378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
@@ -993,11 +993,16 @@ export class FieldApi<
this.form = opts.form as never
this.name = opts.name as never
this.timeoutIds = {} as Record<ValidationCause, never>
// Only really relevant for arrays, the value could be gone once Derived's update fn is triggered
const potentialPreviousValue = this.form.getFieldValue(this.name)

this.store = new Derived({
deps: [this.form.store],
fn: () => {
const value = this.form.getFieldValue(this.name)
let value = this.form.getFieldValue(this.name)
if (value === undefined && potentialPreviousValue !== undefined) {
value = potentialPreviousValue
}
const meta = this.form.getFieldMeta(this.name) ?? {
...defaultFieldMeta,
...opts.defaultMeta,
71 changes: 71 additions & 0 deletions packages/react-form/tests/useField.test.tsx
Original file line number Diff line number Diff line change
@@ -1165,6 +1165,77 @@ describe('useField', () => {
expect(queryByText(fakePeople.molly.name)).not.toBeInTheDocument()
})

it('should not make field uncontrolled during array item removal', async () => {
// Spy on console.error before rendering
const consoleErrorSpy = vi.spyOn(console, 'error')

let id = 0

function Comp() {
const form = useForm({
defaultValues: {
people: [] as { id: number; name: string }[],
},
})

return (
<form>
<form.Field name="people" mode="array">
{(people) => (
<div>
<button
type="button"
onClick={() => {
people.pushValue({
id: id,
name: `${id++}`,
})
}}
data-testid="add"
>
Add
</button>
{people.state.value.map((person, i) => (
<form.Field name={`people[${i}].name`} key={person.id}>
{(field) => {
return (
<div>
<input
type="text"
value={field.state.value}
onChange={(e) => {
field.handleChange(e.target.value)
}}
/>
<button
type="button"
data-testid={`remove-${person.id}`}
onClick={() => {
people.removeValue(i)
}}
>
Remove
</button>
</div>
)
}}
</form.Field>
))}
</div>
)}
</form.Field>
</form>
)
}

const { getByTestId } = render(<Comp />)
await user.click(getByTestId('add'))
await user.click(getByTestId('add'))
await user.click(getByTestId('remove-0'))
// Making a controlled field uncontrolled will log an error
expect(consoleErrorSpy).not.toHaveBeenCalled()
})

it('should not rerender unrelated fields', async () => {
const renderCount = {
field1: 0,