Skip to content

Commit

Permalink
Added tsx generic typings for Field, Form, and FormSpy components
Browse files Browse the repository at this point in the history
  • Loading branch information
ChadLefort committed Jun 11, 2019
1 parent 3dc413a commit 58fb5f9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 13 deletions.
27 changes: 27 additions & 0 deletions typescript/ReactFinalForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,30 @@ function mutated() {
</Form>
);
}

const typedOnSubmit = (values: { firstName: string }) => {
// tslint:disable-next-line no-console
console.log(values);
};

// with typed form data and field
function withTypedFormData() {
return (
<Form<{ firstName: string }> onSubmit={typedOnSubmit}>
{({ handleSubmit }) => (
<form onSubmit={handleSubmit}>
<div>
<label>First Name</label>
<Field<string>
name="firstName"
component="input"
type="text"
placeholder="First Name"
initialValue=""
/>
</div>
</form>
)}
</Form>
);
}
32 changes: 19 additions & 13 deletions typescript/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,26 @@ export interface FormProps<FormValues = object>
initialValuesEqual?: (a?: object, b?: object) => boolean;
}

export interface UseFieldConfig {
export interface UseFieldConfig<FieldValue> {
afterSubmit?: () => void;
allowNull?: boolean;
beforeSubmit?: () => void | boolean;
defaultValue?: any;
format?: (value: any, name: string) => any;
defaultValue?: FieldValue;
format?: (value: FieldValue, name: string) => any;
formatOnBlur?: boolean;
initialValue?: any;
initialValue?: FieldValue;
isEqual?: (a: any, b: any) => boolean;
multiple?: boolean;
parse?: (value: any, name: string) => any;
parse?: (value: FieldValue, name: string) => FieldValue;
subscription?: FieldSubscription;
type?: string;
validate?: FieldValidator;
validateFields?: string[];
value?: any;
value?: FieldValue;
}

export interface FieldProps<T extends HTMLElement>
extends UseFieldConfig,
export interface FieldProps<FieldValue, T extends HTMLElement>
extends UseFieldConfig<FieldValue>,
RenderableProps<FieldRenderProps<T>> {
name: string;
[otherProp: string]: any;
Expand All @@ -98,12 +98,18 @@ export interface FormSpyProps<FormValues>
extends UseFormStateParams<FormValues>,
RenderableProps<FormSpyRenderProps<FormValues>> {}

export const Field: React.FC<FieldProps<any>>;
export const Form: React.FC<FormProps<object>>;
export const FormSpy: React.FC<FormSpyProps<object>>;
export function useField<T extends HTMLElement>(
export const Field: <FieldValue = any, T extends HTMLElement = HTMLElement>(
props: FieldProps<FieldValue, T>
) => React.ReactElement;
export const Form: <FormValues = object>(
props: FormProps<FormValues>
) => React.ReactElement;
export const FormSpy: <FormValues = object>(
props: FormSpyProps<FormValues>
) => React.ReactElement;
export function useField<FieldValue = any, T extends HTMLElement = HTMLElement>(
name: string,
config?: UseFieldConfig
config?: UseFieldConfig<FieldValue>
): FieldRenderProps<T>;
export function useForm<FormValues = object>(
componentName?: string
Expand Down

0 comments on commit 58fb5f9

Please # to comment.