1 | import {
|
2 | ChangeEvent,
|
3 | Component,
|
4 | ComponentType,
|
5 | DragEvent,
|
6 | FocusEvent,
|
7 | InputHTMLAttributes,
|
8 | SelectHTMLAttributes,
|
9 | TextareaHTMLAttributes,
|
10 | } from "react";
|
11 | import { Dispatch } from "redux";
|
12 |
|
13 | export type Normalizer = (value: any, previousValue?: any, allValues?: any, previousAllValues?: any) => any;
|
14 | export type Formatter = (value: any, name: string) => any;
|
15 | export type Parser = (value: any, name: string) => any;
|
16 | export type Validator = (value: any, allValues?: any, props?: any, name?: any) => any;
|
17 |
|
18 | export type EventHandler<Event> = (event: Event, name?: string) => void;
|
19 | export type EventWithDataHandler<Event> = (event?: Event, newValue?: any, previousValue?: any, name?: string) => void;
|
20 |
|
21 | export interface EventOrValueHandler<Event> extends EventHandler<Event> {
|
22 | (value: any): void;
|
23 | }
|
24 |
|
25 | export interface CommonFieldInputProps {
|
26 | name: string;
|
27 | onDragStart: EventHandler<DragEvent<any>>;
|
28 | onDrop: EventHandler<DragEvent<any>>;
|
29 | onFocus: EventHandler<FocusEvent<any>>;
|
30 | }
|
31 |
|
32 | export interface CommonFieldProps extends CommonFieldInputProps {
|
33 | onBlur: EventWithDataHandler<FocusEvent<any>>;
|
34 | onChange: EventWithDataHandler<ChangeEvent<any>>;
|
35 | }
|
36 |
|
37 | export interface BaseFieldProps<P = {}> extends Partial<CommonFieldProps> {
|
38 | name: string;
|
39 | component?: ComponentType<WrappedFieldProps & P> | "input" | "select" | "textarea" | undefined;
|
40 | format?: Formatter | null | undefined;
|
41 | normalize?: Normalizer | undefined;
|
42 | props?: P | undefined;
|
43 | parse?: Parser | undefined;
|
44 | validate?: Validator | Validator[] | undefined;
|
45 | warn?: Validator | Validator[] | undefined;
|
46 | forwardRef?: boolean | undefined;
|
47 | immutableProps?: string[] | undefined;
|
48 | }
|
49 |
|
50 | export interface GenericField<P> extends Component<BaseFieldProps<P> & P> {
|
51 | dirty: boolean;
|
52 | name: string;
|
53 | pristine: boolean;
|
54 | value: any;
|
55 | getRenderedComponent(): Component<WrappedFieldProps & P>;
|
56 | }
|
57 |
|
58 | export type GenericFieldHTMLAttributes =
|
59 | | InputHTMLAttributes<HTMLInputElement>
|
60 | | SelectHTMLAttributes<HTMLSelectElement>
|
61 | | TextareaHTMLAttributes<HTMLTextAreaElement>;
|
62 |
|
63 | export class Field<P extends GenericFieldHTMLAttributes | BaseFieldProps = GenericFieldHTMLAttributes | BaseFieldProps>
|
64 | extends Component<P>
|
65 | {
|
66 | dirty: boolean;
|
67 | name: string;
|
68 | pristine: boolean;
|
69 | value: any;
|
70 | getRenderedComponent(): Component<WrappedFieldProps & P>;
|
71 | }
|
72 |
|
73 | export interface WrappedFieldProps {
|
74 | input: WrappedFieldInputProps;
|
75 | meta: WrappedFieldMetaProps;
|
76 | }
|
77 |
|
78 | export interface WrappedFieldInputProps extends CommonFieldInputProps {
|
79 | checked?: boolean | undefined;
|
80 | value: any;
|
81 | onBlur: EventOrValueHandler<FocusEvent<any>>;
|
82 | onChange: EventOrValueHandler<ChangeEvent<any>>;
|
83 | }
|
84 |
|
85 | export interface WrappedFieldMetaProps {
|
86 | active?: boolean | undefined;
|
87 | autofilled: boolean;
|
88 | asyncValidating: boolean;
|
89 | dirty: boolean;
|
90 | dispatch: Dispatch<any>;
|
91 | error?: any;
|
92 | form: string;
|
93 | initial: any;
|
94 | invalid: boolean;
|
95 | pristine: boolean;
|
96 | submitting: boolean;
|
97 | submitFailed: boolean;
|
98 | touched: boolean;
|
99 | valid: boolean;
|
100 | visited: boolean;
|
101 | warning?: any;
|
102 | }
|
103 |
|
104 | export default Field;
|