Skip to content

Commit b33f79f

Browse files
committed
✨ use setWith
1 parent a893400 commit b33f79f

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

packages/core/package.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,17 @@
2929
],
3030
"scripts": {
3131
"build": "unbuild",
32-
"stub": "unbuild --stub"
32+
"dev": "tsup --watch src"
3333
},
3434
"peerDependencies": {
3535
"vue": ">=3.0.0"
36+
},
37+
"devDependencies": {
38+
"@types/lodash": "^4.14.186",
39+
"@types/lodash.setwith": "^4.3.7",
40+
"tsup": "^5.12.9"
41+
},
42+
"dependencies": {
43+
"lodash.setwith": "^4.3.2"
3644
}
3745
}

packages/core/src/logic/creatFormControl.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { nextTick, reactive, ref, unref } from 'vue'
2+
import setWith from 'lodash.setwith'
23
import { VALIDATION_MODE } from '../shared/constant'
34
import type { FieldError, FieldErrors } from '../types/errors'
45
import type { Field, FieldElement, FieldValues, Fields } from '../types/filed'
@@ -50,11 +51,7 @@ export function creatFormControl<TFieldValues extends FieldValues = FieldValues>
5051

5152
const _formState = reactive<TFormState>({
5253
get isDirty() {
53-
if (isEmptyObject(_formState.dirtyFields)) {
54-
return false
55-
}
56-
57-
return true
54+
return !isEmptyObject(_formState.dirtyFields)
5855
},
5956
isValidating: false,
6057
dirtyFields: {} as FieldNamesMarkedBoolean<TFieldValues>,
@@ -81,7 +78,7 @@ export function creatFormControl<TFieldValues extends FieldValues = FieldValues>
8178
}
8279

8380
const _setFormStateError = (fieldName: FieldsKey, error: FieldError) => {
84-
set(_formState.errors, fieldName, error)
81+
setWith(_formState.errors, fieldName, error)
8582
}
8683

8784
const _getFormStateError = (fieldName?: FieldsKey) => fieldName ? get(_formState.errors, fieldName) : _formState.errors
@@ -418,9 +415,9 @@ export function creatFormControl<TFieldValues extends FieldValues = FieldValues>
418415
} = options
419416

420417
const defaultVal = options?.value
421-
|| get(_defaultValues, fieldName as string)
422-
|| get(_fieldArrayDefaultValues, (fieldName as string).split('.').find(item => isNumber(parseInt(item))))
423-
|| ''
418+
|| get(_defaultValues, fieldName as string)
419+
|| get(_fieldArrayDefaultValues, (fieldName as string).split('.').find(item => isNumber(parseInt(item))))
420+
|| ''
424421

425422
if (!field) {
426423
_setFields(fieldName, {
@@ -460,12 +457,12 @@ export function creatFormControl<TFieldValues extends FieldValues = FieldValues>
460457
}
461458
}
462459

463-
const handleValueChange = async (input: InputEvent | any) => {
460+
const handleValueChange = (input: InputEvent | any) => {
464461
field.inputValue.value = (input?.target as any)?.value || input || ''
465462

466463
_handleAllDirtyFieldsOperate(fieldName)
467464
if (validationModeBeforeSubmit.isOnChange) {
468-
await _onChange(fieldName)
465+
_onChange(fieldName)
469466
}
470467
}
471468

0 commit comments

Comments
 (0)