-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdefineStatus.ts
More file actions
147 lines (132 loc) · 4.01 KB
/
Copy pathdefineStatus.ts
File metadata and controls
147 lines (132 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import type { Ref, UnwrapNestedRefs, WatchStopHandle } from 'vue'
import { computed, reactive, watchEffect } from 'vue'
import type { RuleItem, UseFormDefaultMessage, UseFormRule } from './type/form'
import type { StatusItem } from './type/formStatus'
import type { OnCleanup } from './type/util'
import { deepEqual } from './util/deepEqual'
import { invoke } from './util/invoke'
import { isFunction, isHasOwn, isObjectType, isPromise } from './util/is'
import { watchIgnorable } from './util/watchIgnorable'
export function initStatus<FormT extends {}>(
status: Record<PropertyKey, StatusItem>,
formObj: UnwrapNestedRefs<FormT>,
initialForm: Ref<FormT>,
formDefaultMessage: UseFormDefaultMessage,
formRule?: UseFormRule<FormT>,
) {
for (const key in formObj) {
if (!isHasOwn(formObj, key))
continue
// Functions or arrays of functions are allowed
const fieldRules = invoke(() => {
const formRuleItem = formRule?.[key] as RuleItem | RuleItem[] | undefined
return isFunction(formRuleItem) ? [formRuleItem] : formRuleItem
})
status[key] = reactive({
message: formDefaultMessage,
isError: false,
verifying: false,
isDirty: computed(() => !deepEqual((initialForm.value as any)[key], formObj[key])),
...statusControl(key, status, formObj, fieldRules, formDefaultMessage),
})
}
}
function statusControl<FormT extends {}>(
key: keyof UnwrapNestedRefs<FormT>,
status: Record<PropertyKey, StatusItem>,
formObj: UnwrapNestedRefs<FormT>,
fieldRules: RuleItem<any>[] | undefined,
formDefaultMessage: UseFormDefaultMessage,
) {
function setError(message: string, isError = true) {
status[key].message = message
status[key].isError = isError
}
/** parsing verification result */
function parseError(result: string | boolean) {
// result as string or falsity
// Exit validation on error
if (!result || typeof result === 'string') {
setError(result || formDefaultMessage)
return true
} // no errors
else {
setError(formDefaultMessage, false)
return false
}
}
/** Number of asynchronous validations in progress */
let verifyingCount = 0
function ruleEffect(onCleanup?: OnCleanup) {
let isEnded = false
if (onCleanup)
onCleanup(() => isEnded = true)
// Traverse the ruleset and check the rules
for (const rule of fieldRules || []) {
const result = rule(formObj[key], onCleanup)
// Determine whether it is synchronous verification
if (!isPromise(result)) {
if (parseError(result)) {
isEnded = true
break
}
}
else {
// If it's async validation, wait for its result in a new function
invoke(async () => {
verifyingCount += 1
status[key].verifying = !!verifyingCount
try {
const err = await result
// Validation will end when there is any one error result
// If the validation has ended, no further results will be processed
if (isEnded)
return
if (parseError(err))
isEnded = true
}
finally {
verifyingCount -= 1
status[key].verifying = !!verifyingCount
}
})
}
}
}
/** Used to stop watchEffect */
let stopEffect: WatchStopHandle | null = null
// Initialization rule check
const init = () => {
// Determine if it has been initialized
if (!fieldRules || stopEffect)
return
// monitor changes
stopEffect = watchEffect(ruleEffect)
}
// Begin validation when user input
const { ignoreUpdates } = watchIgnorable(
() => formObj[key],
init,
{
deep: isObjectType(formObj[key]),
},
)
function clearError() {
if (stopEffect) {
stopEffect()
stopEffect = null
}
setError(formDefaultMessage, false)
}
function verify() {
ruleEffect()
return !status[key].isError
}
return {
verify,
setError,
clearError,
init,
_ignoreUpdate: ignoreUpdates,
}
}