import React from "react"; import { RadioGroup } from "@headlessui/react"; import { isNil } from "lodash"; import { useTranslations } from "next-intl"; import { Controller, FieldPath, FieldValues, useFormContext, } from "react-hook-form"; import { twMerge } from "tailwind-merge"; import { Prettify } from "@/types"; import { Field, GenericFieldProps } from "./Field"; export type BaseOption = { value: T; label: string | JSX.Element; disabled?: boolean; data?: D; }; export type RadioGroupFieldProps< TFieldValues extends FieldValues = FieldValues, TFieldName extends FieldPath = FieldPath, T = string, D = unknown, > = Prettify< GenericFieldProps & { required?: boolean; options: BaseOption[]; } >; export const RadioGroupField = < TFieldValues extends FieldValues = FieldValues, TFieldName extends FieldPath = FieldPath, T extends React.Key = string, D = unknown, >({ name, control, className, labelClassName, childrenWrapperClassName, label, hideErrorMessage, required, options, }: RadioGroupFieldProps) => { const t = useTranslations("App"); const form = useFormContext(); const { formState: { errors }, } = form; const valueToOption = (value: T): BaseOption => options.find((o) => o.value === value) ?? { value, label: "" }; return ( { const optionValue = isNil(value) ? null : valueToOption(value); return ( {options.map((option) => ( twMerge( "flex flex-1 cursor-pointer items-center justify-center rounded-lg border px-5 py-3 text-center font-medium focus:outline-none", "border-gray-300 bg-gray-50 text-gray-900", "dark:border-gray-600 dark:bg-gray-700 dark:text-white", checked && "border-primary text-primary dark:border-primary dark:text-primary", ) } disabled={option.disabled} > {option.label} ))} ); }} /> ); };