'use client' import type { ReactNode } from 'react' import { Controller, type Control } from 'react-hook-form' import ConsumerFieldShell from '@/components/consumer/input/ConsumerFieldShell' import type { ConsumerInputRadius, ConsumerInputSize } from '@/components/consumer/input/consumerInputTypes' import { CONSUMER_CHECKBOX_CLASS_NAMES, CONSUMER_FIELD_DESCRIPTION_CLASS, CONSUMER_FIELD_LABEL_CLASS, CONSUMER_RADIO_CLASS_NAMES, buildConsumerOtpSegmentClass, withConsumerFieldError, } from '@/components/consumer/input/consumerInputStyles' import { Checkbox, Radio, RadioGroup, Switch } from '@/components/heroui/SelectionControls' import { InputOtp } from '@/components/heroui/InputOtp' import { getOptionValue, type SelectOptionItem } from '@/components/formElements/input/inputUtils' import { coerceToString } from '@/helpers' import { cn } from '@/lib/cn' interface Props { autoFocus?: boolean className?: string control: Control description?: ReactNode disabled?: boolean kind: 'radio' | 'checkbox' | 'otp' | 'switch' label?: string name: string options: SelectOptionItem[] orientation: 'horizontal' | 'vertical' otpClassNames?: { segment?: string; segmentWrapper?: string } otpLength: number placeholder?: string radius: ConsumerInputRadius readonly?: boolean required?: boolean selectKey: string size: ConsumerInputSize } export default function ConsumerChoiceField({ autoFocus, className, control, description, disabled, kind, label, name, options, orientation, otpClassNames, otpLength, placeholder, radius, readonly, required, selectKey, size, }: Props) { const otpSegmentClass = otpClassNames?.segment ?? buildConsumerOtpSegmentClass({ radius, size }) return ( { const invalid = !!error const otpSegment = invalid ? withConsumerFieldError(otpSegmentClass) : otpSegmentClass if (kind === 'otp') { return ( ) } if (kind === 'radio') { return ( {options.map((item) => ( {coerceToString(getOptionValue(item, 'name'))} ))} ) } if (kind === 'checkbox') { return (
{label} {description ?

{description}

: null} {invalid && error?.message ?

{error.message}

: null}
) } return (
{label} {description ?

{description}

: null}
) }} /> ) }