8000 :sparkles: use lodash.setWith/get/unset · vue-use-form/vue-use-form@1481b59 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1481b59

Browse files
committed
✨ use lodash.setWith/get/unset
1 parent ebf8449 commit 1481b59

File tree

7 files changed

+286
-725
lines changed

7 files changed

+286
-725
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"jest": "^27.5.1",
4444
"jsdom": "^20.0.0",
4545
"nodemon": "^2.0.15",
46+
"prettier": "^2.7.1",
4647
"rimraf": "^3.0.2",
4748
"rollup": "^2.70.2",
4849
"ts-jest": "^27.1.4",

packages/core/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@
3535
"vue": ">=3.0.0"
3636
},
3737
"dependencies": {
38+
"lodash.get": "^4.4.2",
3839
"lodash.setwith": "^4.3.2",
39-
"lodash.topath": "^4.5.2"
40+
"lodash.unset": "^4.5.2"
4041
},
4142
"devDependencies": {
43+
"@types/lodash.get": "^4.4.7",
4244
"@types/lodash.setwith": "^4.3.7",
43-
"@types/lodash.topath": "^4.5.7",
45+
"@types/lodash.unset": "^4.5.7",
4446
"tsup": "^5.12.9"
4547
}
4648
}

packages/core/src/logic/creatFormControl.ts

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
import { nextTick, reactive, ref, toRefs, unref } from 'vue'
2-
import setWith from 'lodash.setwith'
3-
import toPath from 'lodash.topath'
2+
import set from 'lodash.setwith'
3+
import get from 'lodash.get'
4+
import unset from 'lodash.unset'
45
import { VALIDATION_MODE } from '../shared/constant'
56
import {
6-
get,
77
isArray,
88
isEmptyObject,
99
isFunction,
1010
isNullOrUndefined,
1111
isNumber,
12+
isObject,
1213
isString,
1314
isUndefined,
14-
set,
15-
unset,
1615
} from '../utils'
1716
import { InvalidDate } from '../utils/constant'
1817

1918
import { deepEqual } from '../utils/deepEqual'
2019
import { isRadioOrCheckboxInput } from '../utils/fieldElement'
2120
import { getFormEl } from '../utils/getFormEl'
22-
import { getPath } from '../utils/getPath'
2321
import { getValidationMode } from '../utils/getValidationMode'
2422
import { isFieldElement } from '../utils/isFieldElement'
2523

@@ -81,12 +79,14 @@ export function creatFormControl<
8179

8280
const _setFormState = (props: { [K in TFormStateKey]?: TFormState[K] }) => {
8381
Object.entries(props).forEach(([key, val]) => {
84-
_formState[key] = val
82+
if (isObject(_formState[key])) {
83+
_formState[key] = val
84+
}
8585
})
8686
}
8787

8888
const _setFormStateError = (fieldName: FieldsKey, error: FieldError) => {
89-
setWith(_formState.errors, fieldName, error)
89+
set(_formState.errors, fieldName, error)
9090
}
9191

9292
const _getFormStateError = (fieldName?: FieldsKey) =>
@@ -97,28 +97,21 @@ export function creatFormControl<
9797
return
9898
}
9999

100-
const paths = toPath(fieldName)
101-
102-
if (paths.length !== 1) {
103-
const error = getPath(fieldName as string, _formState.errors, -1)
104-
unset(error, paths.at(-1))
105-
} else {
106-
unset(_formState.errors, fieldName)
107-
}
100+
unset(_formState.errors, fieldName)
108101
}
109102

110103
const _getField = (name: FieldsKey) => {
111-
return getPath(name as string, _fields) as Field | undefined
104+
return get(_fields, name)
112105
}
113106

114107
const _setFields = (name: FieldsKey, fieldOptions: Partial<Field>) => {
115108
// init field
116109
const field = _getField(name)
117110
if (isNullOrUndefined(field)) {
118-
setWith(_fields, name, {})
111+
set(_fields, name, {})
119112
}
120113

121-
setWith(_fields, name, { ...field, ...fieldOptions })
114+
set(_fields, name, { ...field, ...fieldOptions })
122115
}
123116

124117
const _getDefaultValue = (field: FieldsKey) => {
@@ -186,7 +179,7 @@ export function creatFormControl<
186179
return
187180
}
188181

189-
const defaultVal = get(_defaultValues, fieldName as string)
182+
const defaultVal = get(_defaultValues, fieldName)
190183
const val = field.inputValue.value
191184

192185
if (deepEqual(defaultVal, val)) {
@@ -486,13 +479,6 @@ export function creatFormControl<
486479
}
487480
}
488481

489-
const _setField = (name: FieldsKey, options: TFieldValues[FieldsKey]) => {
490-
setWith(_fields, name, {
491-
..._getField(name),
492-
..._options,
493-
})
494-
}
495-
496482
const setValue: UseFormSetValue<TFieldValues, FieldsKey> = async (
497483
name,
498484
value,
@@ -569,7 +555,7 @@ export function creatFormControl<
569555

570556
const defaultVal =
571557
options?.value ||
572-
get(_defaultValues, fieldName as string) ||
558+
get(_defaultValues, fieldName) ||
573559
get(
574560
_fieldArrayDefaultValues,
575561
(fieldName as string)
@@ -697,7 +683,7 @@ export function creatFormControl<
697683
}
698684

699685
const isExistInErrors = (fieldName: keyof TFieldValues) =>
700-
!isEmptyObject(getPath(fieldName as string, _formState.errors))
686+
!isEmptyObject(get(_formState.errors, fieldName))
701687

702688
return {
703689
control: {

packages/core/src/logic/createFieldArray.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { reactive } from 'vue'
2-
import { isArray, set } from '../utils'
2+
import set from 'lodash.setwith'
3+
import { isArray } from '../utils'
34
import type {
45
UseFieldArrayAppend,
56
UseFieldArrayField,

packages/core/src/types/form.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ToRefs } from '@vue/reactivity'
1+
import type { ToRefs } from 'vue'
22
import type { FieldValues, Fields } from './filed'
33
import type { Resolver } from './resolver'
44
import type {

packages/core/src/utils/index.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
11
export const isFunction = (val: unknown): val is Function =>
22
typeof val === 'function'
33

4-
export const isNumber = (val: unknown): val is number => typeof val === 'number' && !isNaN(val)
4+
export const isNumber = (val: unknown): val is number =>
5+
typeof val === 'number' && !Number.isNaN(val)
56

67
export const isString = (val: unknown): val is string => typeof val === 'string'
78

8-
export const isBoolean = (val: unknown): val is Boolean => typeof val === 'boolean'
9+
export const isBoolean = (val: unknown): val is Boolean =>
10+
typeof val === 'boolean'
911

1012
export const isObject = (val: unknown) =>
1113
val !== null && typeof val === 'object'
1214

13-
export const isArray = (val: unknown): val is Array<unknown> => Array.isArray(val)
15+
export const isArray = (val: unknown): val is Array<unknown> =>
16+
Array.isArray(val)
1417

1518
export const isEmptyObject = (val: unknown) =>
1619
isObject(val) && Object.keys(val as object).length === 0
1720

18-
export const isUndefined = (val: unknown): val is undefined => typeof val === 'undefined'
21+
export const isUndefined = (val: unknown): val is undefined =>
22+
typeof val === 'undefined'
1923

2024
export const isNull = (val: unknown): val is null => val === null
2125

2226
export const isNullOrUndefined = (val: unknown) =>
2327
isNull(val) || isUndefined(val)
2428

25-
export const isHTMLElement = (val: unknown): val is HTMLElement => val instanceof HTMLElement
29+
export const isHTMLElement = (val: unknown): val is HTMLElement =>
30+
val instanceof HTMLElement
2631

27-
export const isEmpty = (val: unknown) => val === '' || val === null || val === undefined
32+
export const isEmpty = (val: unknown) =>
33+
val === '' || val === null || val === undefined
2834

2935
export const isRegex = (val: unknown): val is RegExp => val instanceof RegExp
3036

@@ -36,4 +42,3 @@ export const isPrimitive = (val: unknown) =>
3642
export const isDateObject = (val: unknown): val is Date => val instanceof Date
3743

3844
export * from './createHandler'
39-
export * from './object'

0 commit comments

Comments
 (0)
0