8000 :sparkles: optimize isDirty · vue-use-form/vue-use-form@256a4f2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 256a4f2

Browse files
committed
✨ optimize isDirty
1 parent 1481b59 commit 256a4f2

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

packages/core/src/logic/creatFormControl.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,15 @@ import {
99
isFunction,
1010
isNullOrUndefined,
1111
isNumber,
12-
isObject,
1312
isString,
1413
isUndefined,
1514
} from '../utils'
1615
import { InvalidDate } from '../utils/constant'
17-
1816
import { deepEqual } from '../utils/deepEqual'
1917
import { isRadioOrCheckboxInput } from '../utils/fieldElement'
2018
import { getFormEl } from '../utils/getFormEl'
2119
import { getValidationMode } from '../utils/getValidationMode'
2220
import { isFieldElement } from '../utils/isFieldElement'
23-
2421
import { warn } from '../utils/warn'
2522
import { handleValidateError, validateField } from './validate'
2623
import type { RegisterOptions } from '../types/validator'
@@ -78,11 +75,9 @@ export function creatFormControl<
7875
_options.criteriaMode === VALIDATION_MODE.all
7976

8077
const _setFormState = (props: { [K in TFormStateKey]?: TFormState[K] }) => {
81-
Object.entries(props).forEach(([key, val]) => {
82-
if (isObject(_formState[key])) {
83-
_formState[key] = val
84-
}
85-
})
78+
for (const [key, val] of Object.entries(props)) {
79+
_formState[key] = val
80+
}
8681
}
8782

8883
const _setFormStateError = (fieldName: FieldsKey, error: FieldError) => {
@@ -92,6 +87,16 @@ export function creatFormControl<
9287
const _getFormStateError = (fieldName?: FieldsKey) =>
9388
fieldName ? get(_formState.errors, fieldName) : _formState.errors
9489

90+
const isEmptyErrors = () => {
91+
for (const key of _originalFieldsKey.keys()) {
92+
if (_getFormStateError(key as FieldsKey)) {
93+
return false
94+
}
95+
}
96+
97+
return true
98+
}
99+
95100
const _removeFormStateError = (fieldName: FieldsKey) => {
96101
if (isEmptyObject(_formState.errors)) {
97102
return
@@ -151,6 +156,16 @@ export function creatFormControl<
151156
}
152157
}
153158

159+
const isDirty = () => {
160+
for (const key of _originalFieldsKey.keys()) {
161+
if (_isDirtyField(key as FieldsKey)) {
162+
return true
163+
}
164+
}
165+
166+
return false
167+
}
168+
154169
const _getDirtyFields = (handleIsDirty = true) => {
155170
const dirtyFields = {} as TFormState['dirtyFields']
156171

@@ -165,7 +180,7 @@ export function creatFormControl<
165180

166181
if (handleIsDirty) {
167182
_setFormState({
168-
isDirty: !isEmptyObject(dirtyFields),
183+
isDirty: isDirty(),
169184
})
170185
}
171186

@@ -410,7 +425,7 @@ export function creatFormControl<
410425
await _onChange()
411426
_handleAllDirtyFieldsOperate()
412427

413-
if (!isEmptyObject(_formState.errors) && isFunction(onError)) {
428+
if (!isEmptyErrors() && isFunction(onError)) {
414429
await onError(_formState.errors, e)
415430
_setFormState({
416431
isSubmitting: false,
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import type { FieldElement } from '../types/filed'
22

3-
export const isRadioInput = (el?: FieldElement): el is HTMLInputElement => el?.type === 'radio'
3+
export const isRadioInput = (el?: FieldElement): el is HTMLInputElement =>
4+
el?.type === 'radio'
45

5-
export const isCheckBoxInput = (el?: FieldElement): el is HTMLInputElement => el?.type === 'checkbox'
6+
export const isCheckBoxInput = (el?: FieldElement): el is HTMLInputElement =>
7+
el?.type === 'checkbox'
68

7-
export const isRadioOrCheckboxInput = (el?: FieldElement): el is HTMLInputElement =>
8-
isRadioInput(el) || isCheckBoxInput(el)
9+
export const isRadioOrCheckboxInput = (
10+
el?: FieldElement
11+
): el is HTMLInputElement => isRadioInput(el) || isCheckBoxInput(el)

0 commit comments

Comments
 (0)
0