Skip to content

Commit

Permalink
Merge pull request #7 from nighca/master
Browse files Browse the repository at this point in the history
Debug FormState `_activated` logic
  • Loading branch information
nighca authored Oct 23, 2019
2 parents f5d2c7e + 6497d53 commit c1de99d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "formstate-x",
"version": "1.0.2",
"version": "1.0.3",
"description": "Extended alternative for formstate",
"repository": {
"type": "git",
Expand Down
20 changes: 14 additions & 6 deletions src/formState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,9 @@ export default class FormState<TFields extends ValidatableFields, TValue = Value

/**
* If activated (with auto validate).
* Form will only be activated when some field activated.
* Form will only be activated when `validate()` called or some field activated.
*/
@computed get _activated() {
return this.fields.some(
field => field._activated
)
}
@observable _activated = false

/**
* If value has been touched.
Expand Down Expand Up @@ -170,6 +166,7 @@ export default class FormState<TFields extends ValidatableFields, TValue = Value
* Reset to initial status.
*/
@action reset() {
this._activated = false
this._validateStatus = ValidateStatus.NotValidated
this._error = undefined
this.validation = undefined
Expand Down Expand Up @@ -205,6 +202,10 @@ export default class FormState<TFields extends ValidatableFields, TValue = Value
* Fire a validation behavior.
*/
async validate() {
runInAction('activate-when-validate', () => {
this._activated = true
})

this._validate()
this.fields.forEach(
field => field.validate()
Expand Down Expand Up @@ -277,6 +278,13 @@ export default class FormState<TFields extends ValidatableFields, TValue = Value
this.$ = observable(this.$, undefined, { deep: false })
}

// auto activate: any field activated -> form activated
this.addDisposer(reaction(
() => this.fields.some(field => field._activated),
someFieldActivated => someFieldActivated && !this._activated && (this._activated = true),
{ fireImmediately: true }
))

// auto validate: this.value -> this.validation
this.addDisposer(autorun(
() => !this.validationDisabled && this._activated && this._validate(),
Expand Down

0 comments on commit c1de99d

Please sign in to comment.