'use client'; import { ChangeEventHandler, ReactNode, createContext, useContext, } from 'react'; import { cn } from '@/lib/helper'; export interface RadioOption { label: string; value: string; } // DaisyUI Radio Colors export type RadioColor = | 'neutral' | 'primary' | 'secondary' | 'accent' | 'success' | 'warning' | 'info' | 'error'; // DaisyUI Radio Sizes export type RadioSize = 'xs' | 'sm' | 'md' | 'lg' | 'xl'; // Context untuk RadioGroup interface RadioGroupContextValue { name: string; value?: string; color?: RadioColor; size?: RadioSize; disabled?: boolean; onChange?: ChangeEventHandler; onBlur?: (e: React.FocusEvent) => void; } const RadioGroupContext = createContext( undefined ); const useRadioGroup = () => { const context = useContext(RadioGroupContext); if (!context) { throw new Error('RadioGroupItem must be used within RadioGroup'); } return context; }; // RadioGroup Component export interface RadioGroupProps { label?: string; bottomLabel?: string; name: string; value?: string; options?: RadioOption[]; color?: RadioColor; size?: RadioSize; className?: { wrapper?: string; label?: string; radioWrapper?: string; }; isError?: boolean; errorMessage?: string; required?: boolean; disabled?: boolean; onChange?: ChangeEventHandler; onBlur?: (e: React.FocusEvent) => void; children?: ReactNode; } export const RadioGroup = ({ label, bottomLabel, name, value, options, color = 'primary', size = 'md', className, isError, errorMessage, required = false, disabled = false, onChange, onBlur, children, }: RadioGroupProps) => { const contextValue: RadioGroupContextValue = { name, value, color, size, disabled, onChange, onBlur, }; return (
{/* Label atas */} {label && ( )} {/* Daftar opsi radio */}
{/* Jika options diberikan, render otomatis */} {options && options.map((option) => ( ))} {/* Atau gunakan children untuk custom rendering */} {children}
{/* Label bawah */} {!isError && bottomLabel && (

{bottomLabel}

)} {/* Pesan error */} {isError && errorMessage && (

{errorMessage}

)}
); }; // RadioGroupItem Component export interface RadioGroupItemProps { value: string; label?: string; className?: string; disabled?: boolean; color?: RadioColor; size?: RadioSize; } export const RadioGroupItem = ({ value, label, className, disabled: itemDisabled, color: itemColor, size: itemSize, }: RadioGroupItemProps) => { const { name, value: groupValue, color: groupColor, size: groupSize, disabled: groupDisabled, onChange, onBlur, } = useRadioGroup(); const isDisabled = itemDisabled ?? groupDisabled; const radioColor = itemColor ?? groupColor; const radioSize = itemSize ?? groupSize; return ( ); };