'use client'
/**
* ConsumerInput — consumer-only form fields (direct heroui + RHF).
*
* **Design props**
* - `size`: `sm` | `md` | `lg`
* - `radius`: `full` (pill) | `control` (10px)
* - `tone`: `muted` (#E2E2E280 fill) | `bordered` (white + border). Select is always `muted`.
*
* Use in `app/(consumer)/**` and `components/consumer/**` instead of
* `components/formElements/Input`.
*
* Guidelines: `frontend/docs/consumer-ui-guidelines.md`
*/
import { useEffect } from 'react'
import { FormProvider, useForm, useFormContext, type UseFormReturn } from 'react-hook-form'
import ConsumerChoiceField from '@/components/consumer/input/ConsumerChoiceField'
import ConsumerSelectField from '@/components/consumer/input/ConsumerSelectField'
import ConsumerTextareaField from '@/components/consumer/input/ConsumerTextareaField'
import { ConsumerNumberField, ConsumerTextField } from '@/components/consumer/input/ConsumerTextField'
import { defaultValueForConsumerField, type ConsumerInputProps } from '@/components/consumer/input/consumerInputTypes'
import { texts, format } from '@/texts'
export type { ConsumerInputProps } from '@/components/consumer/input/consumerInputTypes'
function useOptionalFormContext(): UseFormReturn | null {
const context = useFormContext() as UseFormReturn | null
return context?.control ? context : null
}
function ConsumerInputFields({
radius = 'control',
size = 'md',
tone,
isClearable = false,
inputWrapper,
orientation = 'vertical',
direction = 'rtl',
englishDigitsOnly = false,
otpLength = 4,
otpClassNames,
selectKey = 'code',
selectValue = 'name',
textAreaMinRows = 5,
...props
}: ConsumerInputProps) {
const { control } = useFormContext()
const placeholder =
props.placeholder ??
(props.generalType === 'select'
? undefined
: typeof props.label === 'string'
? format(texts.common.enterField, { label: props.label })
: '')
switch (props.generalType) {
case 'numberInput':
return (
)
case 'input':
return (
)
case 'textarea':
return (
)
case 'select':
return (
)
case 'radio':
case 'checkbox':
case 'otp':
case 'switch':
return (
)
default:
return null
}
}
function ConsumerInputStandalone(props: ConsumerInputProps) {
const { name, generalType, value, onValueChange } = props
const methods = useForm({
defaultValues: { [name]: value ?? defaultValueForConsumerField(generalType) },
values: value !== undefined ? { [name]: value } : undefined,
})
useEffect(() => {
if (!onValueChange) return
const subscription = methods.watch((values, info) => {
if (info.name && info.name !== name) return
onValueChange(values[name])
})
return () => {
subscription.unsubscribe()
}
}, [methods, name, onValueChange])
return (
)
}
export default function ConsumerInput(props: ConsumerInputProps) {
const form = useOptionalFormContext()
const isControlled = props.value !== undefined || props.onValueChange !== undefined
if (!form || isControlled) {
return
}
return
}