Skip to content

Commit

Permalink
Optimize disable validate (#75)
Browse files Browse the repository at this point in the history
* optimize validate when state disabled

* update test case

* update validateResult

* update comment
  • Loading branch information
Luncher authored Mar 7, 2022
1 parent 92d9577 commit 1a1b6c4
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
11 changes: 11 additions & 0 deletions src/fieldState.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,17 @@ describe('FieldState validation', () => {
expect(state.hasError).toBe(false)
expect(state.error).toBeUndefined()

runInAction(() => options.disabled = false)

await delay()
expect(state.validating).toBe(false)
expect(state.validated).toBe(false)
expect(state.hasError).toBe(false)
expect(state.error).toBeUndefined()

runInAction(() => options.disabled = true)

await delay()
state.onChange('123')
await delay()
state.onChange('')
Expand Down
21 changes: 16 additions & 5 deletions src/fieldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ export default class FieldState<TValue> extends Disposable implements Composible
return !!this.error
}

/**
* The most recent validation result.
* If state is disabled and later enabled, the result of the last validation is obtained.
* If you need to clear the validation result, please use the reset function.
*/
@computed private get validateResult(): ValidateResult<TValue> {
return this.hasError
? { hasError: true, error: this.error } as const
: { hasError: false, value: this.value } as const
}

/**
* If the validation has been done.
* It does not means validation passed.
Expand Down Expand Up @@ -152,6 +163,10 @@ export default class FieldState<TValue> extends Disposable implements Composible
* Fire a validation behavior.
*/
async validate(): Promise<ValidateResult<TValue>> {
if (this.validationDisabled) {
return this.validateResult
}

const validation = this.validation

action('activate-and-sync-_value-when-validate', () => {
Expand All @@ -172,11 +187,7 @@ export default class FieldState<TValue> extends Disposable implements Composible
{ name: 'return-validate-when-not-validating' }
)

return (
this.hasError
? { hasError: true, error: this.error } as const
: { hasError: false, value: this.value } as const
)
return this.validateResult
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/formState.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,18 @@ describe('FormState (mode: object) validation', () => {
expect(state.ownError).toBeUndefined()
expect(state.error).toBeUndefined()

runInAction(() => options.disabled = false)

await delay()
expect(state.validating).toBe(false)
expect(state.validated).toBe(false)
expect(state.hasError).toBe(false)
expect(state.ownError).toBeUndefined()
expect(state.error).toBeUndefined()

runInAction(() => options.disabled = true)

await delay()
state.$.foo.onChange('')
await state.validate()
expect(state.hasOwnError).toBe(false)
Expand Down
21 changes: 16 additions & 5 deletions src/formState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ export default class FormState<TFields extends ValidatableFields, TValue = Value
return !!this.error
}

/**
* The most recent validation result.
* If state is disabled and later enabled, the result of the last validation is obtained.
* If you need to clear the validation result, please use the reset function.
*/
@computed private get validateResult(): ValidateResult<TValue> {
return this.hasError
? { hasError: true, error: this.error } as const
: { hasError: false, value: this.value } as const
}

/**
* If the validation has been done.
* It does not means validation passed.
Expand Down Expand Up @@ -219,6 +230,10 @@ export default class FormState<TFields extends ValidatableFields, TValue = Value
* Fire a validation behavior.
*/
async validate(): Promise<ValidateResult<TValue>> {
if (this.validationDisabled) {
return this.validateResult
}

action('activate-when-validate', () => {
this._activated = true
})()
Expand All @@ -234,11 +249,7 @@ export default class FormState<TFields extends ValidatableFields, TValue = Value
{ name: 'return-validate-when-not-validating' }
)

return (
this.hasError
? { hasError: true, error: this.error } as const
: { hasError: false, value: this.value } as const
)
return this.validateResult
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export interface Validatable<T, TValue = T> {
validated: boolean
validationDisabled: boolean
validate(): Promise<ValidateResult<TValue>>

// To see if there are requirements: enableAutoValidation, disableAutoValidation
// enableAutoValidation: () => void
// disableAutoValidation: () => void
Expand Down

0 comments on commit 1a1b6c4

Please sign in to comment.