Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -70,26 +70,26 @@ export interface FormProps<FormValues = AnyObject>
initialValuesEqual?: (a?: AnyObject, b?: AnyObject) => 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: any, 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 @@ -104,12 +104,18 @@ export interface FormSpyProps<FormValues = AnyObject>
extends UseFormStateParams<FormValues>,
RenderableProps<FormSpyRenderProps<FormValues>> {}

export const Field: React.FC<FieldProps<any>>;
export const Form: React.FC<FormProps<AnyObject>>;
export const FormSpy: React.FC<FormSpyProps<AnyObject>>;
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 = AnyObject>(
props: FormProps<FormValues>
) => React.ReactElement;
export const FormSpy: <FormValues = AnyObject>(
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 = AnyObject>(
componentName?: string
Expand Down