This repository has been archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 494
/
types.ts
75 lines (63 loc) 路 1.92 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {
DetailedHTMLProps,
FormHTMLAttributes,
FormEvent,
ReactNode,
} from 'react'
interface BaseUnformField<T> {
name: string
ref?: any
setValue?: (ref: any, value: T) => void
clearValue?: (ref: any, newValue: T) => void
}
export interface PathUnformField<T> extends BaseUnformField<T> {
path: string
getValue?: undefined
}
export interface FunctionUnformField<T> extends BaseUnformField<T> {
path?: undefined
getValue: (ref: any) => T
}
export type UnformField<T = any> = PathUnformField<T> | FunctionUnformField<T>
export interface UnformErrors {
[key: string]: string | undefined
}
export interface UnformContext {
initialData: Record<string, any>
errors: UnformErrors
scopePath: string
registerField<T>(field: UnformField<T>): void
unregisterField: (name: string) => void
clearFieldError: (fieldName: string) => void
handleSubmit: (e?: FormEvent) => void
}
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>
type HTMLFormProps = DetailedHTMLProps<
FormHTMLAttributes<HTMLFormElement>,
HTMLFormElement
>
export interface FormHandles {
getFieldValue(fieldName: string): any
setFieldValue(fieldName: string, value: any): void | boolean
getFieldError(fieldName: string): string | undefined
setFieldError(fieldName: string, error: string): void
clearField(fieldName: string): void
getData(): Record<string, any>
getFieldRef(fieldName: string): any
setData(data: Record<string, any>): void
getErrors(): UnformErrors
setErrors(errors: Record<string, string>): void
reset(data?: Record<string, any>): void
submitForm(): void
}
export interface FormHelpers {
reset: (data?: Record<string, any>) => void
}
export interface SubmitHandler<T = any> {
(data: T, helpers: FormHelpers, event?: FormEvent): void
}
export interface FormProps extends Omit<HTMLFormProps, 'onSubmit'> {
initialData?: Record<string, any>
children: ReactNode
onSubmit: SubmitHandler
}