Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1481b59

Browse files
committed
✨ use lodash.setWith/get/unset
1 parentebf8449 commit1481b59

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-
importsetWithfrom'lodash.setwith'
3-
importtoPathfrom'lodash.topath'
2+
importsetfrom'lodash.setwith'
3+
importgetfrom'lodash.get'
4+
importunsetfrom'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:{[KinTFormStateKey]?: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-
constpaths=toPath(fieldName)
101-
102-
if(paths.length!==1){
103-
consterror=getPath(fieldNameasstring,_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-
returngetPath(nameasstring,_fields)asField|undefined
104+
returnget(_fields,name)
112105
}
113106

114107
const_setFields=(name:FieldsKey,fieldOptions:Partial<Field>)=>{
115108
// init field
116109
constfield=_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-
constdefaultVal=get(_defaultValues,fieldNameasstring)
182+
constdefaultVal=get(_defaultValues,fieldName)
190183
constval=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
constsetValue:UseFormSetValue<TFieldValues,FieldsKey>=async(
497483
name,
498484
value,
@@ -569,7 +555,7 @@ export function creatFormControl<
569555

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

699685
constisExistInErrors=(fieldName:keyofTFieldValues)=>
700-
!isEmptyObject(getPath(fieldNameasstring,_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+
importsetfrom'lodash.setwith'
3+
import{isArray}from'../utils'
34
importtype{
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-
importtype{ToRefs}from'@vue/reactivity'
1+
importtype{ToRefs}from'vue'
22
importtype{FieldValues,Fields}from'./filed'
33
importtype{Resolver}from'./resolver'
44
importtype{

‎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
exportconstisFunction=(val:unknown):val isFunction=>
22
typeofval==='function'
33

4-
exportconstisNumber=(val:unknown):val isnumber=>typeofval==='number'&&!isNaN(val)
4+
exportconstisNumber=(val:unknown):val isnumber=>
5+
typeofval==='number'&&!Number.isNaN(val)
56

67
exportconstisString=(val:unknown):val isstring=>typeofval==='string'
78

8-
exportconstisBoolean=(val:unknown):val isBoolean=>typeofval==='boolean'
9+
exportconstisBoolean=(val:unknown):val isBoolean=>
10+
typeofval==='boolean'
911

1012
exportconstisObject=(val:unknown)=>
1113
val!==null&&typeofval==='object'
1214

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

1518
exportconstisEmptyObject=(val:unknown)=>
1619
isObject(val)&&Object.keys(valasobject).length===0
1720

18-
exportconstisUndefined=(val:unknown):val isundefined=>typeofval==='undefined'
21+
exportconstisUndefined=(val:unknown):val isundefined=>
22+
typeofval==='undefined'
1923

2024
exportconstisNull=(val:unknown):val isnull=>val===null
2125

2226
exportconstisNullOrUndefined=(val:unknown)=>
2327
isNull(val)||isUndefined(val)
2428

25-
exportconstisHTMLElement=(val:unknown):val isHTMLElement=>valinstanceofHTMLElement
29+
exportconstisHTMLElement=(val:unknown):val isHTMLElement=>
30+
valinstanceofHTMLElement
2631

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

2935
exportconstisRegex=(val:unknown):val isRegExp=>valinstanceofRegExp
3036

@@ -36,4 +42,3 @@ export const isPrimitive = (val: unknown) =>
3642
exportconstisDateObject=(val:unknown):val isDate=>valinstanceofDate
3743

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp