import React from "react"; import { Formik, FormikProps } from "formik"; import { Dialog, Heading, Pane, Switch, TextInput, Select } from "evergreen-ui"; export enum InputType { SWITCH = 1, TEXT_INPUT, SELECT } interface FormProps { initialValues: T; onSubmit: (values: T) => void; title: string; formsFields: { key: string; type: InputType; label: string; isDisabled?: (values: T) => boolean; props?: any; options?: { label: string; value: string | number }[]; }[]; 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, options }, i ) => { let FormField, select; if (type === InputType.TEXT_INPUT) FormField = TextInput; else if (type === InputType.SWITCH) FormField = Switch; else if (type === InputType.SELECT) select = ( ); const _isDisabled = isDisabled ? isDisabled(props.values) : undefined; return ( {label} {FormField && ( )} {select} ); } )} ); }} onSubmit={values => { onSubmit(values); }} /> ); }; export default Form;