Skip to content

Commit 7a1113f

Browse files
committed
fix: 🐛 fix overwrite field option issue
1 parent 7a06428 commit 7a1113f

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/__tests__/use-form-getValue.test.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react";
2-
import render, { act } from 'hooks-test-util'
2+
import render, {act} from 'hooks-test-util'
33
import useForm from '../index'
44

55
describe("use-form getValue test", () => {
@@ -54,15 +54,13 @@ describe("use-form getValue test", () => {
5454

5555
console.warn = jest.fn()
5656

57-
let password
58-
const text = 'yayayayaya'
5957
const field = container.hook[1]
6058

6159
field("apassword", {
6260
rules: [{
6361
type: "string",
6462
validator: (rule, value, callback)=>{
65-
password = container.hook[0].getValue().password
63+
const password = container.hook[0].getValue().password
6664
if(value != password){
6765
callback("两次输入的密码不一致!")
6866
}
@@ -72,11 +70,9 @@ describe("use-form getValue test", () => {
7270
})
7371

7472
act(() => {
75-
container.hook[1]('password').onChange(text)
73+
container.hook[1]('apassword').onChange('yayayayaya')
7674
})
7775

78-
expect(password).toEqual(text)
79-
8076
expect(container.hook[0].errors).toEqual( {
8177
"apassword": [{"field": "apassword", "message": "两次输入的密码不一致!"}]
8278
})

src/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import {mapValues} from './utils/map-values'
55
import noop from './utils/noop'
66
import {ValidateError, ValidationRule} from "./typing"
77
import {actions, reducer} from "./reducer"
8-
import {FiledType} from "./filed-type"
8+
import { FiledType } from "./filed-type"
99

1010
export interface FieldOption {
1111
rules?: ValidationRule[],
12-
type?: 'text' | 'checkbox' | 'boolean' | 'radio'
12+
type?: FiledType
1313
}
1414

1515
export type UseForm = <T>(initialData: Partial<T>) => [
@@ -42,7 +42,7 @@ const getFieldData = (value) => {
4242
}
4343
}
4444

45-
export function getResetValue(type) {
45+
export function getResetValue(type?: FiledType) {
4646
switch (type) {
4747
case FiledType.text:
4848
return ''
@@ -89,7 +89,6 @@ const useForm: UseForm = <T>(intial: Partial<T>) => {
8989
if(option) {
9090
fieldOptions[name] = option
9191
}
92-
return null
9392
}
9493

9594
const getValidateDescriptor = useCallback((type) => {
@@ -160,12 +159,12 @@ const useForm: UseForm = <T>(intial: Partial<T>) => {
160159
}
161160
}
162161

163-
const field = <K extends keyof T>(name: K, option: FieldOption = {}) => {
162+
const field = <K extends keyof T>(name: K, option?: FieldOption) => {
164163
setOption(name, option)
165164
return {
166165
get value() {
167166
const value = get(state, `fields.${name}.value`)
168-
return value === undefined ? getResetValue(option.type) : value
167+
return value === undefined ? getResetValue(option && option.type) : value
169168
},
170169
set value(value) {
171170
this.onChange(value)

0 commit comments

Comments
 (0)