infoalloggi-monorepo/apps/infoalloggi/src/forms/FormFlags.tsx

119 lines
2.6 KiB
TypeScript
Raw Normal View History

import { z } from "zod/v4";
2025-08-04 17:45:44 +02:00
import {
2025-08-28 18:27:07 +02:00
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
2025-08-04 17:45:44 +02:00
} from "~/components/custom_ui/form";
import Input from "~/components/custom_ui/input";
import { Button } from "~/components/ui/button";
import { Switch } from "~/components/ui/switch";
2025-08-28 18:27:07 +02:00
import { useZodForm } from "~/lib/zodForm";
import type { Flags, FlagsId } from "~/schemas/public/Flags";
2025-08-04 17:45:44 +02:00
type FormProps = {
initialValues: Flags | null;
submitMutation: (values: Flags) => void;
2025-08-28 18:27:07 +02:00
handleRemove: (id: FlagsId) => void;
2025-08-04 17:45:44 +02:00
};
export const FormFlags = ({
2025-08-28 18:27:07 +02:00
initialValues,
submitMutation,
handleRemove,
2025-08-04 17:45:44 +02:00
}: FormProps) => {
2025-08-28 18:27:07 +02:00
const Schema = z.object({
id: z.string(),
value: z.boolean(),
2025-08-28 18:27:07 +02:00
});
type FormValues = z.infer<typeof Schema>;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
let defaultValues: FormValues;
// This can come from your database or API.
if (!initialValues) {
defaultValues = {
id: "",
value: false,
2025-08-28 18:27:07 +02:00
};
} else {
defaultValues = {
id: initialValues.id,
value: initialValues.value,
};
}
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
z.config(z.locales.it());
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const form = useZodForm(Schema, {
defaultValues: defaultValues,
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
function onSubmit(fields: FormValues) {
const values = {
id: fields.id as FlagsId,
value: fields.value,
};
submitMutation(values);
}
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
return (
<>
<Form {...form}>
<form
className="space-y-8 px-0.5"
2025-08-29 16:18:32 +02:00
onSubmit={form.handleSubmit(onSubmit)}
2025-08-28 18:27:07 +02:00
>
<FormField
control={form.control}
name="id"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="id">Codice</FormLabel>
<FormMessage />
</div>
<FormControl>
<Input placeholder="" {...field} id="id" />
</FormControl>
</FormItem>
)}
/>
<FormField
control={form.control}
name="value"
render={({ field }) => (
<FormItem>
<div className="flex flex-wrap items-center gap-x-2">
<FormLabel htmlFor="value">Attivo/Inattivo</FormLabel>
2025-08-28 18:27:07 +02:00
<FormMessage />
</div>
<FormControl>
<Switch
checked={field.value}
className="data-[state=checked]:bg-neutral-700"
2025-08-28 18:27:07 +02:00
id="value"
onCheckedChange={field.onChange}
2025-08-28 18:27:07 +02:00
/>
</FormControl>
</FormItem>
)}
/>
<div className="flex flex-row items-center justify-between">
<Button type="submit">Salva</Button>
{initialValues?.id && handleRemove && (
<Button
2025-08-29 16:18:32 +02:00
onClick={() => handleRemove(initialValues.id)}
2025-08-28 18:27:07 +02:00
type="button"
variant="destructive"
>
Cancella
</Button>
)}
</div>
</form>
</Form>
</>
);
2025-08-04 17:45:44 +02:00
};