8000 :sparkles: delayError & shouldUnregister · vue-use-form/vue-use-form@e904a98 · GitHub
[go: up one dir, main page]

Skip to content

Commit e904a98

Browse files
committed
✨ delayError & shouldUnregister
1 parent 2b1cc9c commit e904a98

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

packages/core/src/logic/creatFormControl.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { nextTick, reactive, ref, unref } from 'vue'
1+
import { nextTick, onMounted, reactive, ref, unref } from 'vue'
22
import { VALIDATION_MODE } from '../shared/constant'
33
import type { FieldError, FieldErrors } from '../types/errors'
44
import type { Field, FieldElement, FieldValues, Fields } from '../types/filed'
@@ -66,6 +66,14 @@ export function creatFormControl<TFieldValues extends FieldValues = FieldValues>
6666
const shouldDisplayAllAssociatedErrors
6767
= _options.criteriaMode === VALIDATION_MODE.all
6868

69+
if (_options.shouldUnregister) {
70+
onMounted(() => {
71+
Object.keys(_fields).forEach((key) => {
72+
unset(_fields, key)
73+
})
74+
})
75+
}
76+
6977
const _setFormState = (props: { [K in TFormStateKey]?: TFormState[TFormStateKey] }) => {
7078
Object.entries(props).forEach(([key, val]) => {
7179
set(_formState, key as TFormStateKey, val)
@@ -188,6 +196,14 @@ export function creatFormControl<TFieldValues extends FieldValues = FieldValues>
188196
res = await validateField(field, unref(_options.shouldFocusError!), shouldDisplayAllAssociatedErrors)
189197
}
190198
setValidating(false)
199+
// delayError
200+
if (_options.delayError && _options.delayError > 0) {
201+
await new Promise((resolve) => {
202+
setTimeout(() => {
203+
resolve(true)
204+
}, _options.delayError)
205+
})
206+
}
191207
if (Object.keys(res || {}).length) {
192208
_setFormStateError(fieldName, res)
193209
} else {

packages/core/src/types/form.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export interface UseFormProps<TFieldValues extends object> {
1717
resolver: Resolver<TFieldValues>
1818
shouldFocusError: boolean
1919
shouldUnregister: boolean
20-
shouldUseNativeValidation: boolean
2120
criteriaMode: CriteriaMode
2221
delayError: number
2322
}

packages/core/src/useForm.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ export function useForm<TFieldValues extends FieldValues = FieldValues>(
1313
criteriaMode: 'firstError',
1414
shouldFocusError: true,
1515
shouldUnregister: false,
16-
shouldUseNativeValidation: false,
17-
delayError: undefined,
16+
delayError: 0,
1817
...props,
1918
}
2019
return {

0 commit comments

Comments
 (0)
0