Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix override state props
  • Loading branch information
Luncher committed May 23, 2022
commit 031f8a1cbed5e82f6edfda8b07c5776672799cbd
7 changes: 3 additions & 4 deletions src/adapter/v2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { override, action, computed, makeObservable } from 'mobx'
import { action, computed, makeObservable } from 'mobx'
import * as v2 from 'formstate-x-v2'
import { BaseState } from '../state'
import * as v3 from '..'
Expand All @@ -25,10 +25,9 @@ class Upgrader<T extends v2.ComposibleValidatable<unknown, V>, V> extends BaseSt

@computed get value() { return this.stateV2.value }
@computed get touched() { return this.stateV2.dirty }
@override override get ownError() {
@computed get rawError() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

多了个空格?以及第 30 行也多了个

return getV3OwnError(this.stateV2)
}
@computed get rawError() { return this.ownError }
}
@computed get error() { return this.stateV2.error }
@computed get activated() { return this.stateV2._activated }
@computed get validateStatus() {
Expand Down
17 changes: 9 additions & 8 deletions src/debouncedState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { action, computed, makeObservable, observable, override, reaction } from
import { FieldState } from './fieldState'
import { ValidatableState } from './state'
import { IState, ValidateStatus, ValueOf } from './types'
import { debounce, isPassed, normalizeError } from './utils'
import { debounce, isPassed } from './utils'

const defaultDelay = 200 // ms

/** Infomation synced from original state */
type OriginalInfo<V> = Pick<IState<V>, 'activated' | 'touched' | 'error' | 'ownError' | 'hasError'>
/** Information synced from original state */
type OriginalInfo<V> = Pick<IState<V>, 'activated' | 'touched' | 'error' | 'ownError' | 'hasError' | 'rawError'>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ownErrorhasError 都可以干掉了


/**
* The state for debounce purpose.
Expand All @@ -24,7 +24,7 @@ export class DebouncedState<S extends IState<V>, V = ValueOf<S>> extends Validat
/** Debounced version of original value */
@observable.ref value!: V

/** Orignal information, same version with current `value` */
/** Original information, same version with current `value` */
@observable.ref private synced!: OriginalInfo<V>

/** Original information for current `value` */
Expand All @@ -43,18 +43,19 @@ export class DebouncedState<S extends IState<V>, V = ValueOf<S>> extends Validat
touched: this.$.touched,
error: this.$.error,
ownError: this.$.ownError,
hasError: this.$.hasError
hasError: this.$.hasError,
rawError: this.$.rawError
}
}

@computed get touched() {
return this.original.touched
}

@override override get ownError() {
@override override get rawError() {
if (this.disabled) return undefined
if (this.rawError) return normalizeError(this.rawError)
return this.original.ownError
if (this.validationResult) return this.validationResult
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!isPassed(this.validationResult)) 会比 if (this.validationResult) 更严谨吗?

return this.original.rawError
}

@override override get error() {
Expand Down
4 changes: 2 additions & 2 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ export abstract class ValidatableState<V> extends BaseState implements IState<V>
@observable activated = false

/**
* The original return value of validation.
* The original validation result.
*/
@observable private validationResult: ValidationResult
@observable protected validationResult: ValidationResult

@computed get rawError() {
return this.disabled ? undefined : this.validationResult
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function normalizeError(result: ValidationResult): ValidationError {
}

export function isErrorObject(err: any): err is ValidationErrorObject {
if (err != null && typeof err === 'object' && 'message' in err) {
if (err != null && typeof err === 'object' && typeof err.message === 'string') {
return true
}
return false
Expand Down