'use client'
import type { KeyboardEvent, ReactNode } from 'react'
import { useState } from 'react'
import { Controller, type Control } from 'react-hook-form'
import { Input, Textarea } from '@/components/heroui/Input'
import { NumberInput } from '@/components/heroui/NumberInput'
import { buildOutsideLabel, formatNumberInputDisplayValue } from '@/components/formElements/input/inputUtils'
import Button from '@/components/formElements/Button'
import EyeCrossedIcon from '@/components/icons/EyeCrossedIcon'
import EyeIcon from '@/components/icons/EyeIcon'
import { convertPersianToEnglish } from '@/helpers'
import { formatTomanInWords } from '@/helpers/numberToPersianWords'
import { texts } from '@/texts'
function isTomanAmountLabel(label?: string): boolean {
return typeof label === 'string' && label.includes(texts.common.toman)
}
function buildAmountDescription(value: unknown, label: string | undefined, existing?: ReactNode): ReactNode {
if (!isTomanAmountLabel(label)) return existing
const amountInWords = formatTomanInWords(value)
if (!amountInWords && !existing) return undefined
if (!amountInWords) return existing
if (!existing) return amountInWords
return (
<>
{amountInWords}
{existing}
>
)
}
interface Props {
ariaLabel?: string
autoFocus?: boolean
className?: string
control: Control
description?: ReactNode
direction: 'rtl' | 'ltr'
disabled?: boolean
englishDigitsOnly: boolean
finalInputWrapper: string
formatOptions?: Intl.NumberFormatOptions
iconEnd?: ReactNode
iconStart?: ReactNode
inputType?: 'text' | 'email' | 'number' | 'password' | 'tel'
isClearable: boolean
kind: 'input' | 'numberInput' | 'textarea'
label?: string
labelIcon?: ReactNode
maxValue?: number
minValue?: number
name: string
placeholder?: string
radius: 'none' | 'sm' | 'md' | 'lg' | 'full'
readonly?: boolean
required?: boolean
size?: 'sm' | 'md' | 'lg'
textAreaMinRows: number
variant: 'flat' | 'bordered' | 'faded' | 'underlined'
onBlur?: () => void
onClear?: () => void
onKeyDown?: (event: KeyboardEvent) => void
}
export default function TextFieldControl(props: Props) {
const [passwordVisible, setPasswordVisible] = useState(false)
const { control, kind, name } = props
return (
{
const description = buildAmountDescription(field.value, props.label, props.description)
const common = {
autoFocus: props.autoFocus,
className: props.className,
description,
errorMessage: error?.message,
isDisabled: props.disabled,
isInvalid: !!error,
isReadOnly: props.readonly,
isRequired: false,
labelPlacement: 'outside' as const,
placeholder: props.placeholder,
radius: props.radius,
size: props.size,
validationBehavior: 'aria' as const,
variant: props.variant,
}
if (kind === 'textarea') {
const canClear = props.isClearable && !props.readonly
return (