import { observable } from "mobx"; import React, { useCallback, useRef, useState } from "react"; import { useText } from "../../../lib"; import { Button, Category, Error, Modal } from "../../design"; import { Action, Props as ModalProps } from "../../design/atoms/display/Modal"; import { Form, FormTemplate, getInitialValues, MapFormToValues, Props as FormProps, } from "./Form"; /** * Modal Form props */ type Props = Exclude< ModalProps, "children" | "actions" | "registerOnClose" | "registerOnConfirm" > & Omit, "observed" | "onChange" | "onSubmit"> & { /** * Form submission callback */ callback: (values: MapFormToValues) => Promise; /** * Submit button properties */ submit?: Omit, "type">; /** * Custom actions after submit button */ actions?: Action[]; }; /** * Modal Form */ export function ModalForm(props: Props) { const values = observable(getInitialValues(props.schema, props.defaults)); const [error, setError] = useState(null!); const [processing, setProcessing] = useState(false); const Text = useText(); const onSubmit = useCallback(async () => { try { setProcessing(true); await props.callback(values); return true; } catch (err) { // ! FIXME: map error correctly setError("" + err); setProcessing(false); return false; } }, []); return ( true, children: "Cancel", palette: "plain", }, ]), ]}>
{error && ( } /> )} ); }