Skip to content

Commit c2a3a27

Browse files
committed
feat(core): add form listners for reset
1 parent 4e7c8a6 commit c2a3a27

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

.changeset/reset-listeners.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@tanstack/form-core": minor
3+
---
4+
5+
Add `onReset` listener support for forms and fields, add `reset` method to `FieldApi`, and expand `ListenerCause` type with `'reset'` and `'unmount'` values

packages/form-core/src/FieldApi.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import {
88
determineFieldLevelErrorSourceAndValue,
99
evaluate,
1010
getAsyncValidatorArray,
11+
getBy,
1112
getSyncValidatorArray,
1213
mergeOpts,
14+
setBy,
1315
} from './utils'
1416
import { defaultValidationLogic } from './ValidationLogic'
1517
import type { ReadonlyStore } from '@tanstack/store'
@@ -384,6 +386,7 @@ export interface FieldListeners<
384386
onMount?: FieldListenerFn<TParentData, TName, TData>
385387
onUnmount?: FieldListenerFn<TParentData, TName, TData>
386388
onSubmit?: FieldListenerFn<TParentData, TName, TData>
389+
onReset?: FieldListenerFn<TParentData, TName, TData>
387390
}
388391

389392
/**
@@ -2090,6 +2093,17 @@ export class FieldApi<
20902093
)
20912094
}
20922095

2096+
/**
2097+
* Resets the field value and meta to default state.
2098+
*/
2099+
reset = () => {
2100+
this.form.resetField(this.name)
2101+
this.options.listeners?.onReset?.({
2102+
value: this.state.value,
2103+
fieldApi: this,
2104+
})
2105+
}
2106+
20932107
private triggerOnBlurListener() {
20942108
const formDebounceMs = this.form.options.listeners?.onBlurDebounceMs
20952109
if (formDebounceMs && formDebounceMs > 0) {

packages/form-core/src/FormApi.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,23 @@ export interface FormListeners<
324324
>
325325
fieldApi: AnyFieldApi
326326
}) => void
327+
328+
onReset?: (props: {
329+
formApi: FormApi<
330+
TFormData,
331+
TOnMount,
332+
TOnChange,
333+
TOnChangeAsync,
334+
TOnBlur,
335+
TOnBlurAsync,
336+
TOnSubmit,
337+
TOnSubmitAsync,
338+
TOnDynamic,
339+
TOnDynamicAsync,
340+
TOnServer,
341+
TSubmitMeta
342+
>
343+
}) => void
327344
}
328345

329346
/**
@@ -1541,6 +1558,8 @@ export class FormApi<
15411558
fieldMetaBase,
15421559
}),
15431560
)
1561+
1562+
this.options.listeners?.onReset?.({ formApi: this })
15441563
}
15451564

15461565
/**

packages/form-core/src/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ export type ValidationCause =
2121
/**
2222
* @private
2323
*/
24-
export type ListenerCause = 'change' | 'blur' | 'submit' | 'mount'
24+
export type ListenerCause =
25+
| 'change'
26+
| 'blur'
27+
| 'submit'
28+
| 'mount'
29+
| 'reset'
30+
| 'unmount'
2531

2632
/**
2733
* @private

0 commit comments

Comments
 (0)