Skip to content

Commit 85b7741

Browse files
committed
Pass through FormEvent to handleSubmit
1 parent f00ae8a commit 85b7741

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

example/src/App.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ export function Form() {
303303
<button type="button" onClick={() => form.setErrors("This is a parent error")}>
304304
Set parent error
305305
</button>
306+
<button type="button" onClick={() => form.parent.setValue("author", null, true)}>
307+
Test
308+
</button>
306309
</div>
307310
</VisualRender>
308311
)}

src/form.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export class FormState<T extends object, State = DefaultState, Error extends str
213213
* @param isDefault Is this the default value?
214214
* @param notifyChild Should this form notify the child form about this change?
215215
* @param notifyParent Should this form notify the parent form about this change?
216-
* @param setValuesWasUsed Fire all `anyListeners` after field is set? You should not touch this. (will be false for bulk sets, they will call fireAnyListeners() after every field is set)
216+
* @param fireAny Fire all `anyListeners` after field is set?
217217
*/
218218
public setValue<Key extends keyof T>(
219219
key: Key,
@@ -291,6 +291,7 @@ export class FormState<T extends object, State = DefaultState, Error extends str
291291
}
292292

293293
this.fireAnyListeners();
294+
294295
if (notifyParent && this instanceof ChildFormState) {
295296
if (typeof values === "object" && values !== null) {
296297
this.parent.setValueInternal(
@@ -354,6 +355,7 @@ export class FormState<T extends object, State = DefaultState, Error extends str
354355
* @param error The error.
355356
* @param notifyChild Should this form notify the child form about this change?
356357
* @param notifyParent Should this form notify the parent form about this change?
358+
* @param fireAny Fire all `anyListeners` after field is set?
357359
*/
358360
public setError<Key extends keyof T>(
359361
key: Key,
@@ -465,7 +467,7 @@ export class FormState<T extends object, State = DefaultState, Error extends str
465467
* Creates a submit handler to pass to your `<form onSubmit={...}>`. The function executes the passed handler only if the form validates correctly.
466468
* @param handler The handler to execute when this form contains no errors.
467469
*/
468-
public handleSubmit(handler: (form: FormState<T, State, Error>) => void | Promise<void>) {
470+
public handleSubmit(handler: (form: FormState<T, State, Error>, ev: React.FormEvent<HTMLFormElement>) => void | Promise<void>) {
469471
async function handle(this: FormState<T, State, Error>, ev: React.FormEvent<HTMLFormElement>) {
470472
ev.preventDefault();
471473

@@ -483,7 +485,7 @@ export class FormState<T extends object, State = DefaultState, Error extends str
483485

484486
if (!(await this.validate())) return;
485487
this.setState({ ...this.state, isSubmitting: true });
486-
await handler(this);
488+
await handler(this, ev);
487489
this.setState({ ...this.state, isSubmitting: false });
488490
}
489491
return handle.bind(this);
@@ -575,17 +577,4 @@ export class ChildFormState<T extends FieldsOfType<any, object>, K extends KeysO
575577
this.parent = parent;
576578
this.name = name;
577579
}
578-
579-
// public setValueInternal<F extends keyof NonNullable<T[K]>>(
580-
// key: F,
581-
// value: T[K][F] | undefined,
582-
// dirty: boolean,
583-
// validate?: boolean,
584-
// isDefault: boolean = false,
585-
// notifyChild: boolean = true,
586-
// notifyParent: boolean = true,
587-
// fireAny: boolean = true
588-
// ) {
589-
// super.setValueInternal(key, value, dirty, validate, isDefault, notifyChild, notifyParent, fireAny);
590-
// }
591580
}

0 commit comments

Comments
 (0)