Skip to content

Commit 66a6c3e

Browse files
ChadLeforterikras
authored andcommitted
Added tsx generic typings for Field, Form, and FormSpy components (#522)
* Added tsx generic typings for Field, Form, and FormSpy components * Switched param back to any
1 parent 02bf898 commit 66a6c3e

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

typescript/ReactFinalForm.test.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,30 @@ function mutated() {
130130
</Form>
131131
);
132132
}
133+
134+
const typedOnSubmit = (values: { firstName: string }) => {
135+
// tslint:disable-next-line no-console
136+
console.log(values);
137+
};
138+
139+
// with typed form data and field
140+
function withTypedFormData() {
141+
return (
142+
<Form<{ firstName: string }> onSubmit={typedOnSubmit}>
143+
{({ handleSubmit }) => (
144+
<form onSubmit={handleSubmit}>
145+
<div>
146+
<label>First Name</label>
147+
<Field<string>
148+
name="firstName"
149+
component="input"
150+
type="text"
151+
placeholder="First Name"
152+
initialValue=""
153+
/>
154+
</div>
155+
</form>
156+
)}
157+
</Form>
158+
);
159+
}

typescript/index.d.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,26 @@ export interface FormProps<FormValues = AnyObject>
7070
initialValuesEqual?: (a?: AnyObject, b?: AnyObject) => boolean;
7171
}
7272

73-
export interface UseFieldConfig {
73+
export interface UseFieldConfig<FieldValue> {
7474
afterSubmit?: () => void;
7575
allowNull?: boolean;
7676
beforeSubmit?: () => void | boolean;
77-
defaultValue?: any;
78-
format?: (value: any, name: string) => any;
77+
defaultValue?: FieldValue;
78+
format?: (value: FieldValue, name: string) => any;
7979
formatOnBlur?: boolean;
80-
initialValue?: any;
80+
initialValue?: FieldValue;
8181
isEqual?: (a: any, b: any) => boolean;
8282
multiple?: boolean;
83-
parse?: (value: any, name: string) => any;
83+
parse?: (value: any, name: string) => FieldValue;
8484
subscription?: FieldSubscription;
8585
type?: string;
8686
validate?: FieldValidator;
8787
validateFields?: string[];
88-
value?: any;
88+
value?: FieldValue;
8989
}
9090

91-
export interface FieldProps<T extends HTMLElement>
92-
extends UseFieldConfig,
91+
export interface FieldProps<FieldValue, T extends HTMLElement>
92+
extends UseFieldConfig<FieldValue>,
9393
RenderableProps<FieldRenderProps<T>> {
9494
name: string;
9595
[otherProp: string]: any;
@@ -104,12 +104,18 @@ export interface FormSpyProps<FormValues = AnyObject>
104104
extends UseFormStateParams<FormValues>,
105105
RenderableProps<FormSpyRenderProps<FormValues>> {}
106106

107-
export const Field: React.FC<FieldProps<any>>;
108-
export const Form: React.FC<FormProps<AnyObject>>;
109-
export const FormSpy: React.FC<FormSpyProps<AnyObject>>;
110-
export function useField<T extends HTMLElement>(
107+
export const Field: <FieldValue = any, T extends HTMLElement = HTMLElement>(
108+
props: FieldProps<FieldValue, T>
109+
) => React.ReactElement;
110+
export const Form: <FormValues = AnyObject>(
111+
props: FormProps<FormValues>
112+
) => React.ReactElement;
113+
export const FormSpy: <FormValues = AnyObject>(
114+
props: FormSpyProps<FormValues>
115+
) => React.ReactElement;
116+
export function useField<FieldValue = any, T extends HTMLElement = HTMLElement>(
111117
name: string,
112-
config?: UseFieldConfig
118+
config?: UseFieldConfig<FieldValue>
113119
): FieldRenderProps<T>;
114120
export function useForm<FormValues = AnyObject>(
115121
componentName?: string

0 commit comments

Comments
 (0)