Skip to content

Commit 6e565eb

Browse files
author
bietkul
committed
Added submitted property
1 parent 58430bb commit 6e565eb

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

index.d.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ declare module "react-reactive-form" {
378378
* Length of the control array.
379379
*/
380380
length: number;
381+
/**
382+
* A form array is submitted if the `handleSubmit` event has been triggered on it.
383+
*/
384+
submitted: boolean;
381385
/**
382386
* Get the `AbstractControl` at the given `index` in the array.
383387
*/
@@ -484,10 +488,10 @@ declare module "react-reactive-form" {
484488
* Submit action, can be used to tell the form that it has been submitted.
485489
* Useful when `updateOn` property is `submit`.
486490
* ```
487-
* <form onSubmit={this.form.onSubmit}/>
491+
* <form onSubmit={this.form.handleSubmit}/>
488492
* ```
489493
*/
490-
onSubmit():void
494+
handleSubmit(): void
491495
}
492496
/**
493497
* Tracks the value and validity state of a group of `FormControl`
@@ -563,6 +567,10 @@ declare module "react-reactive-form" {
563567
validatorOrOpts?: ValidatorFn|ValidatorFn[]|AbstractControlOptions|null,
564568
asyncValidator?: AsyncValidatorFn|AsyncValidatorFn[]|null)
565569
controls: {[key: string]: AbstractControl}
570+
/**
571+
* A form group is submitted if the `handleSubmit` event has been triggered on it.
572+
*/
573+
submitted: boolean;
566574
/**
567575
* Registers a control with the group's list of controls.
568576
*
@@ -671,10 +679,10 @@ declare module "react-reactive-form" {
671679
* Submit action, can be used to tell the form that it has been submitted.
672680
* Useful when `updateOn` property is `submit`.
673681
* ```
674-
* <form onSubmit={this.form.onSubmit}/>
682+
* <form onSubmit={this.form.handleSubmit}/>
675683
* ```
676684
*/
677-
onSubmit():void
685+
handleSubmit():void
678686
}
679687
/**
680688
* Tracks the value and validation status of an individual form control.

src/Field.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ export default class Field extends React.Component {
3030
// Remove Listener
3131
this.removeListener();
3232
}
33+
shouldComponentUpdate() {
34+
return false;
35+
}
3336
getComponent() {
3437
const { render, control } = this.props;
3538
if (control) {

src/model.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -646,13 +646,13 @@ export class FormControl extends AbstractControl {
646646
if(!this.dirty) { this.markAsDirty(); }
647647
if(!this.touched) { this.markAsTouched(); }
648648
this.setValue(this._pendingValue, { validate: true });
649-
} else if (!this.touched) {
650-
if(this.updateOn === "submit" && !this._pendingTouched) {
651-
this._pendingTouched = true
652-
} else {
653-
this.markAsTouched();
649+
} else if(this.updateOn === "submit") {
650+
this._pendingTouched = true;
651+
this._pendingDirty = true;
652+
} else {
653+
if(!this.dirty) { this.markAsDirty(); }
654+
if(!this.touched) { this.markAsTouched(); }
654655
this.stateChanges.next();
655-
}
656656
}
657657
};
658658
/**
@@ -740,14 +740,16 @@ export class FormGroup extends AbstractControl {
740740
super(coerceToValidator(validatorOrOpts),
741741
coerceToAsyncValidator(asyncValidator, validatorOrOpts));
742742
this.controls = controls;
743+
this.submitted = false;
743744
this.validatorOrOpts = validatorOrOpts;
744-
this.updateDOM = new Subject();
745745
this._initObservables();
746746
this._setUpdateStrategy(validatorOrOpts);
747747
this._setUpControls();
748748
this.updateValueAndValidity({ onlySelf: true, emitEvent: false });
749-
this.onSubmit = () => {
750-
this._syncPendingControls();
749+
this.handleSubmit = (e) => {
750+
e.preventDefault();
751+
this.submitted = true;
752+
if(!this._syncPendingControls()) { this.updateValueAndValidity({ validate: true }) }
751753
}
752754
}
753755
/**
@@ -990,14 +992,16 @@ export class FormArray extends AbstractControl {
990992
coerceToValidator(validatorOrOpts),
991993
coerceToAsyncValidator(asyncValidator, validatorOrOpts));
992994
this.controls = controls;
995+
this.submitted = false;
993996
this.validatorOrOpts = validatorOrOpts;
994-
this.updateDOM = new Subject();
995997
this._initObservables();
996998
this._setUpdateStrategy(validatorOrOpts);
997999
this._setUpControls();
9981000
this.updateValueAndValidity({onlySelf: true, emitEvent: false});
999-
this.onSubmit = () => {
1000-
this._syncPendingControls();
1001+
this.handleSubmit = (e) => {
1002+
e.preventDefault();
1003+
this.submitted = true;
1004+
if(!this._syncPendingControls()) { this.updateValueAndValidity({ validate: true }) }
10011005
}
10021006
}
10031007
/**

0 commit comments

Comments
 (0)