import * as React from "react"; import type { MouseEventHandler } from "react"; import { createContext, useCallback, useContext, useMemo } from "react"; import type { CreateParams, RaRecord, TransformData, UpdateParams, } from "ra-core"; import { setSubmissionErrors, useRecordFromLocation, useSaveContext, useTranslate, ValidationError, warning, } from "ra-core"; import { Loader2, Save } from "lucide-react"; import * as LabelPrimitive from "@radix-ui/react-label"; import { Slot } from "@radix-ui/react-slot"; import { FormProvider, useFormContext, useFormState } from "react-hook-form"; import type { UseMutationOptions } from "@tanstack/react-query"; import { cn } from "@/lib/utils"; import { Label } from "@/components/ui/label"; import { Button } from "@/components/ui/button"; const Form = FormProvider; type FormItemContextValue = { id: string; name: string; }; const FormItemContext = createContext( {} as FormItemContextValue, ); const useFormField = () => { const { getFieldState, formState } = useFormContext(); const { id, name } = useContext(FormItemContext); const fieldState = getFieldState(name, formState); return useMemo( () => ({ formItemId: id, formDescriptionId: `${id}-description`, formMessageId: `${id}-message`, ...fieldState, }), [id, fieldState], ); }; function FormField({ className, id, name, ...props }: FormItemProps) { const contextValue: FormItemContextValue = useMemo( () => ({ id, name, }), [id, name], ); return (
); } type FormItemProps = Omit, "id"> & { id: string; name: string; }; function FormLabel({ className, ...props }: React.ComponentProps) { const { error, formItemId } = useFormField(); return (