Skip to content

Commit

Permalink
fix(core): stop onChange triggering onMount and add tests to assert this
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry Whorlow committed Nov 22, 2024
1 parent da975e5 commit a0534b5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
11 changes: 6 additions & 5 deletions packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,11 +551,6 @@ export class FieldApi<

this.prevState = state
this.state = state

this.options.listeners?.onChange?.({
value: state.value,
fieldApi: this,
})
},
},
)
Expand Down Expand Up @@ -701,6 +696,12 @@ export class FieldApi<
*/
setValue = (updater: Updater<TData>, options?: UpdateMetaOptions) => {
this.form.setFieldValue(this.name, updater as never, options)

this.options.listeners?.onChange?.({
value: updater as never,
fieldApi: this,
})

this.validate('change')
}

Expand Down
23 changes: 23 additions & 0 deletions packages/form-core/tests/FieldApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,29 @@ describe('field api', () => {
expect(triggered).toStrictEqual('other')
})

it('should not run the listener onChange on mount', () => {
const form = new FormApi({
defaultValues: {
name: 'test',
},
})

let triggered!: string
const field = new FieldApi({
form,
name: 'name',
listeners: {
onChange: ({ value }) => {
triggered = value
},
},
})

field.mount()

expect(triggered).toStrictEqual(undefined)
})

it('should run listener onBlur', () => {
const form = new FormApi({
defaultValues: {
Expand Down

0 comments on commit a0534b5

Please sign in to comment.