Skip to content

Commit 9e1d3d6

Browse files
committed
fix: fixed reset bug
1 parent 09e32c6 commit 9e1d3d6

File tree

11 files changed

+64
-53
lines changed

11 files changed

+64
-53
lines changed

docs/use-form/getValues.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ getValues(['test1', 'test2'])
2323
```ts
2424
register('root.test1')
2525
register('root.test2')
26-
getValues('root') // { test1: '', test2: ''}
26+
getValues('root') // { test1: '', test2: ''}
2727
```
2828

eslint.config.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
const antfu = require('@antfu/eslint-config').default
1+
// eslint.config.js
2+
import antfu from '@antfu/eslint-config'
23

3-
module.exports = antfu()
4+
export default antfu({
5+
rules: {
6+
'no-console': 'off',
7+
},
8+
ignores: [
9+
'dist',
10+
'public',
11+
'.github',
12+
'.vscode',
13+
'docs',
14+
'playground',
15+
'*.md',
16+
],
17+
})

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "@vue-hooks-form/monorepo",
3+
"type": "module",
34
"version": "0.0.15",
45
"private": "true",
56
"description": "Vue Composition API for validating form.",

packages/core/src/logic/creatFormControl.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import { nextTick, reactive, ref, unref } from 'vue'
2+
import set from 'lodash.setwith'
3+
import get from 'lodash.get'
4+
import unset from 'lodash.unset'
25
import { VALIDATION_MODE } from '../shared/constant'
36
import type { FieldError, FieldErrors } from '../types/errors'
47
import type { Field, FieldElement, FieldValues, Fields } from '../types/filed'
@@ -23,7 +26,7 @@ import type {
2326
UseFormUnregister,
2427
} from '../types/form'
2528
import type { DefaultValues, UnpackNestedValue } from '../types/utils'
26-
import { get, isArray, isEmptyObject, isFunction, isNullOrUndefined, isNumber, isString, isUndefined, set, unset } from '../utils'
29+
import { isArray, isEmptyObject, isFunction, isNullOrUndefined, isNumber, isString, isUndefined } from '../utils'
2730

2831
import {
2932
createErrorHandler as createErrorHandlerUtil,
@@ -36,7 +39,6 @@ import { getFormEl } from '../utils/getFormEl'
3639
import { getValidationMode } from '../utils/getValidationMode'
3740
import { isFieldElement } from '../utils/isFieldElement'
3841
import type { RegisterOptions } from '../types/validator'
39-
import { warn } from '../utils/warn'
4042
import { handleValidateError, validateField } from './validate'
4143

4244
export function creatFormControl<TFieldValues extends FieldValues = FieldValues>(
@@ -350,7 +352,7 @@ export function creatFormControl<TFieldValues extends FieldValues = FieldValues>
350352

351353
const setValue: UseFormSetValue<TFieldValues, FieldsKey> = async (name, value, config = {}) => {
352354
if (isNullOrUndefined(_fields[name])) {
353-
warn(`setValue cannot set not exist field #${name as string}`)
355+
console.warn(`setValue cannot set not exist field #${name as string}`)
354356
return
355357
}
356358

@@ -417,7 +419,7 @@ export function creatFormControl<TFieldValues extends FieldValues = FieldValues>
417419

418420
const defaultVal = options?.value
419421
|| get(_defaultValues, fieldName as string)
420-
|| get(_fieldArrayDefaultValues, (fieldName as string).split('.').find(item => isNumber(Number.parseInt(item))))
422+
|| get(_fieldArrayDefaultValues, (fieldName as string).split('.').find(item => isNumber(Number.parseInt(item)))!)
421423
|| ''
422424

423425
if (!field) {
@@ -426,7 +428,7 @@ export function creatFormControl<TFieldValues extends FieldValues = FieldValues>
426428
rule: options,
427429
isDirty: false,
428430
isUnregistered: false,
429-
el: ref(null),
431+
el: ref<any>(null),
430432
})
431433

432434
field = get(_fields, fieldName)
@@ -474,7 +476,7 @@ export function creatFormControl<TFieldValues extends FieldValues = FieldValues>
474476
...vModelBinding === 'modelValue'
475477
&& {
476478
value: field.inputValue.value,
477-
onInput: (e) => {
479+
onInput: (e: any) => {
478480
if (_fields[fieldName].isUnregistered)
479481
return
480482

@@ -499,8 +501,8 @@ export function creatFormControl<TFieldValues extends FieldValues = FieldValues>
499501
}
500502

501503
const unregister: UseFormUnregister<TFieldValues> = (fieldName, options = {}) => {
502-
if (isNullOrUndefined(_fields[fieldName])) {
503-
warn(`cannot unregister not exist field #${fieldName as string}`)
504+
if (isNullOrUndefined(_fields[fieldName!])) {
505+
console.warn(`cannot unregister not exist field #${fieldName as string}`)
504506
return
505507
}
506508

packages/core/src/logic/validate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export async function validateField(
9292
const { value: maxValue, message: maxMsg } = getValueAndMessage(max)
9393
const { value: minValue, message: minMsg } = getValueAndMessage(min)
9494

95-
if (!isNaN(unrefInputVal)) {
95+
if (!Number.isNaN(unrefInputVal)) {
9696
if (minValue && unrefInputVal < minValue)
9797
exceedMin = true
9898
if (maxValue && unrefInputVal > maxValue)

packages/core/src/utils/getFormEl.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ export function getFormEl(elRef: MaybeRef<any> | InputEvent): FieldElement | und
2525
return val.querySelectorAll('input, textarea, select')[0] as FieldElement
2626
}
2727
}
28+
return undefined
2829
}

packages/core/src/utils/getValidatorError.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ export function getValidatorError(
2020
message: result,
2121
}
2222
}
23+
return undefined
2324
}

packages/core/src/utils/index.ts

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,64 @@ export function isFunction(val: unknown): val is Function {
22
return typeof val === 'function'
33
}
44

5-
export const isNumber = (val: unknown): val is number => typeof val === 'number' && !isNaN(val)
5+
export function isNumber(val: unknown): val is number {
6+
return typeof val === 'number' && !Number.isNaN(val)
7+
}
68

7-
export const isString = (val: unknown): val is string => typeof val === 'string'
9+
export function isString(val: unknown): val is string {
10+
return typeof val === 'string'
11+
}
812

9-
export const isBoolean = (val: unknown): val is boolean => typeof val === 'boolean'
13+
export function isBoolean(val: unknown): val is boolean {
14+
return typeof val === 'boolean'
15+
}
1016

1117
export function isObject(val: unknown) {
1218
return val !== null && typeof val === 'object'
1319
}
1420

15-
export const isArray = (val: unknown): val is Array<unknown> => Array.isArray(val)
21+
export function isArray(val: unknown): val is Array<unknown> {
22+
return Array.isArray(val)
23+
}
1624

1725
export function isEmptyObject(val: unknown) {
1826
return isObject(val) && Object.keys(val as object).length === 0
1927
}
2028

21-
export const isUndefined = (val: unknown): val is undefined => typeof val === 'undefined'
29+
export function isUndefined(val: unknown): val is undefined {
30+
return typeof val === 'undefined'
31+
}
2232

23-
export const isNull = (val: unknown): val is null => val === null
33+
export function isNull(val: unknown): val is null {
34+
return val === null
35+
}
2436

2537
export function isNullOrUndefined(val: unknown) {
2638
return isNull(val) || isUndefined(val)
2739
}
2840

29-
export const isHTMLElement = (val: unknown): val is HTMLElement => val instanceof HTMLElement
41+
export function isHTMLElement(val: unknown): val is HTMLElement {
42+
return val instanceof HTMLElement
43+
}
3044

31-
export const isEmpty = (val: unknown) => val === '' || val === null || val === undefined
45+
export function isEmpty(val: unknown) {
46+
return val === '' || val === null || val === undefined
47+
}
3248

33-
export const isRegex = (val: unknown): val is RegExp => val instanceof RegExp
49+
export function isRegex(val: unknown): val is RegExp {
50+
return val instanceof RegExp
51+
}
3452

35-
export const isObjectType = (val: unknown) => typeof val === 'object'
53+
export function isObjectType(val: unknown) {
54+
return typeof val === 'object'
55+
}
3656

3757
export function isPrimitive(val: unknown) {
3858
return isNullOrUndefined(val) || !isObjectType(val)
3959
}
4060

41-
export const isDateObject = (val: unknown): val is Date => val instanceof Date
61+
export function isDateObject(val: unknown): val is Date {
62+
return val instanceof Date
63+
}
4264

4365
export * from './createHandler'
44-
export * from './object'

packages/core/src/utils/object.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/core/src/utils/warn.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)