import React from "react"; import { Formik, FormikProps } from "formik"; import { Dialog, Heading, Pane, Switch, TextInput } from "evergreen-ui"; export enum InputType { SWITCH = 1, TEXT_INPUT } interface FormProps { initialValues: T; onSubmit: (values: T) => void; title: string; formsFields: { key: string; type: InputType; label: string; isDisabled?: (values: T) => boolean; props?: any; }[]; open: boolean; toggle: () => void; } const Form = ({ initialValues, onSubmit, title, formsFields, open, toggle }: FormProps & { children?: React.ReactNode }) => { return ( ) => { return ( { props.submitForm(); close(); }} onCancel={close => { props.resetForm(); close(); }} > <> {formsFields.map( ({ type, key, label, isDisabled, props: _props }, i) => { let FormField; if (type === InputType.TEXT_INPUT) FormField = TextInput; else if (type === InputType.SWITCH) FormField = Switch; const _isDisabled = isDisabled ? isDisabled(props.values) : undefined; return ( {label} {FormField && ( )} ); } )} ); }} onSubmit={values => { onSubmit(values); }} /> ); }; export default Form;