import {Forms, useForm, showToast} from "attio/client"
// Define your schema outside of the component
const formSchema = {
title: Forms.string(),
url: Forms.string().url(),
age: Forms.number().min(18),
approved: Forms.boolean(),
subscribed: Forms.boolean(),
}
export function SimpleFormDialog({ onDone } : { onDone: () => void }){
const {
Form,
TextInput,
NumberInput,
Checkbox,
Toggle,
SubmitButton,
} = useForm(formSchema,
{
// These default values are required because these strings are required
title: "",
url: "",
// No defaults are obligatory for required numbers or booleans
// They default to 0 and false respectively
},
);
return (
<Form
onSubmit={async (values) => {
// Usually you'd call a server function here
await showToast({
title: "Form submitted",
variant: "success",
text: JSON.stringify(values, null, 2),
});
onDone()
}}
>
{/* These `name` props are strongly typed to your form schema */}
<TextInput label="Title" name="title" />
<TextInput label="URL" name="url" />
<NumberInput label="Age" name="age" />
<Checkbox label="Approved" name="approved" />
<Toggle label="Subscribed" name="subscribed" />
{/* A `<SubmitButton/>` is required as a direct child of `<Form/>`. */}
{/* No matter where you place it, it will be */}
{/* rendered in the footer of the dialog. */}
<SubmitButton label="Submit" />
</Form>
);
}