Skip to content

Commit 58fb5f9

Browse files
committed
Added tsx generic typings for Field, Form, and FormSpy components
1 parent 3dc413a commit 58fb5f9

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
@@ -64,26 +64,26 @@ export interface FormProps<FormValues = object>
6464
initialValuesEqual?: (a?: object, b?: object) => boolean;
6565
}
6666

67-
export interface UseFieldConfig {
67+
export interface UseFieldConfig<FieldValue> {
6868
afterSubmit?: () => void;
6969
allowNull?: boolean;
7070
beforeSubmit?: () => void | boolean;
71-
defaultValue?: any;
72-
format?: (value: any, name: string) => any;
71+
defaultValue?: FieldValue;
72+
format?: (value: FieldValue, name: string) => any;
7373
formatOnBlur?: boolean;
74-
initialValue?: any;
74+
initialValue?: FieldValue;
7575
isEqual?: (a: any, b: any) => boolean;
7676
multiple?: boolean;
77-
parse?: (value: any, name: string) => any;
77+
parse?: (value: FieldValue, name: string) => FieldValue;
7878
subscription?: FieldSubscription;
7979
type?: string;
8080
validate?: FieldValidator;
8181
validateFields?: string[];
82-
value?: any;
82+
value?: FieldValue;
8383
}
8484

85-
export interface FieldProps<T extends HTMLElement>
86-
extends UseFieldConfig,
85+
export interface FieldProps<FieldValue, T extends HTMLElement>
86+
extends UseFieldConfig<FieldValue>,
8787
RenderableProps<FieldRenderProps<T>> {
8888
name: string;
8989
[otherProp: string]: any;
@@ -98,12 +98,18 @@ export interface FormSpyProps<FormValues>
9898
extends UseFormStateParams<FormValues>,
9999
RenderableProps<FormSpyRenderProps<FormValues>> {}
100100

101-
export const Field: React.FC<FieldProps<any>>;
102-
export const Form: React.FC<FormProps<object>>;
103-
export const FormSpy: React.FC<FormSpyProps<object>>;
104-
export function useField<T extends HTMLElement>(
101+
export const Field: <FieldValue = any, T extends HTMLElement = HTMLElement>(
102+
props: FieldProps<FieldValue, T>
103+
) => React.ReactElement;
104+
export const Form: <FormValues = object>(
105+
props: FormProps<FormValues>
106+
) => React.ReactElement;
107+
export const FormSpy: <FormValues = object>(
108+
props: FormSpyProps<FormValues>
109+
) => React.ReactElement;
110+
export function useField<FieldValue = any, T extends HTMLElement = HTMLElement>(
105111
name: string,
106-
config?: UseFieldConfig
112+
config?: UseFieldConfig<FieldValue>
107113
): FieldRenderProps<T>;
108114
export function useForm<FormValues = object>(
109115
componentName?: string

0 commit comments

Comments
 (0)