import React from "react"; import { RadioGroup } from "@headlessui/react"; import { get, isNil } from "lodash"; import { useTranslations } from "next-intl"; import { Controller, useFormContext } from "react-hook-form"; import { twMerge } from "tailwind-merge"; import { Field, FieldProps } from "./Field"; export type BaseOption = { value: T; label: string | JSX.Element; disabled?: boolean; data?: D; }; export type RadioGroupFieldProps = FieldProps & { required?: boolean; options: BaseOption[]; }; export const RadioGroupField = ({ name, className, labelClassName, childrenWrapperClassName, label, hideErrorMessage, required, options, }: RadioGroupFieldProps) => { const t = useTranslations("App"); const form = useFormContext(); const { formState: { errors }, } = form; const hasError = !!get(errors, name)?.message; 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 text-sm 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} ))} ); }} /> ); };