'use client' import React, { type ChangeEvent, type JSX, useRef, useState, useId } from 'react' import { Calendar, Clock } from 'lucide-react' import { FieldWrapper } from './shared' import DateTimePickerPopup from './shared/dateTimePickerPopup' import ColorPickerPopup from './shared/colorPickerPopup' import useClickOutside from '../../hooks/useClickOutside' export type InputProps = Omit, 'name'> & { name: string label?: string error?: string className?: string icon?: JSX.Element info?: string description?: string textSize?: 'sm' | 'md' reserveError?: boolean } export default function Input(props: InputProps) { const { name, label, error, className, icon, info, description, textSize = 'sm', reserveError, ...inputProps } = props const { type = 'text', value } = inputProps const localRef = useRef(null) const [isOpen, setIsOpen] = useState(false) const id = useId() const anchorName = `--input-${id.replace(/:/g, '')}` const containerRef = useRef(null) useClickOutside(containerRef, () => setIsOpen(false)) const isDateType = ['date', 'datetime-local', 'time'].includes(type as string) const isColorType = type === 'color' const isClickableType = isDateType || isColorType function handleIconClick() { if (isClickableType && !inputProps.disabled) { setIsOpen(!isOpen) } else if (localRef.current && !inputProps.disabled) { localRef.current.focus() } } function pad(n: number) { return n.toString().padStart(2, '0') } function handleDateChange(date: Date) { const onChange = inputProps.onChange if (!onChange) return const yyyy = date.getFullYear() const MM = pad(date.getMonth() + 1) const dd = pad(date.getDate()) const hh = pad(date.getHours()) const mm = pad(date.getMinutes()) let newValue = '' if (type === 'date') newValue = `${yyyy}-${MM}-${dd}` else if (type === 'time') newValue = `${hh}:${mm}` else if (type === 'datetime-local') newValue = `${yyyy}-${MM}-${dd}T${hh}:${mm}` const event = { target: { name, value: newValue, type, }, } as unknown as ChangeEvent onChange(event) } function handleColorChange(color: string) { const onChange = inputProps.onChange if (!onChange) return const event = { target: { name, value: color, type, }, } as unknown as ChangeEvent onChange(event) } let displayIcon = icon if (!displayIcon && isDateType) { if (type === 'time') { displayIcon = } else { displayIcon = } } else if (!displayIcon && isColorType) { displayIcon = (
) } function getDateValue() { if (!value) return null if (type === 'time') { const date = new Date(`2000-01-01T${value}`) return isNaN(date.getTime()) ? null : date } const date = new Date(value as string) return isNaN(date.getTime()) ? null : date } function getDateDisplayValue() { if (!value || !isDateType) return value as string const date = getDateValue() if (!date) return value as string const yyyy = date.getFullYear() const MM = pad(date.getMonth() + 1) const dd = pad(date.getDate()) const hh = pad(date.getHours()) const mm = pad(date.getMinutes()) if (type === 'date') return `${dd}.${MM}.${yyyy}` if (type === 'time') return `${hh}:${mm}` if (type === 'datetime-local') return `${dd}.${MM}.${yyyy} ${hh}:${mm}` return value as string } return (
{displayIcon && (
{displayIcon}
)} isClickableType && !inputProps.disabled && setIsOpen(true)} aria-describedby={error ? `${name}-error` : undefined} style={{ anchorName } as React.CSSProperties & { anchorName?: string }} className={` w-full rounded-md bg-login-500/50 border border-login-500 text-login-text placeholder-login-200 text-sm focus:outline-none focus:border-login/60 focus:ring-1 focus:ring-login/30 disabled:opacity-40 disabled:cursor-not-allowed h-10 py-2 ${displayIcon ? 'pl-10 pr-3' : 'px-3'} transition-all duration-150 input-reset ${error ? 'border-red-500/70 focus:border-red-500/70 focus:ring-red-500/30' : ''} ${isClickableType && !inputProps.disabled ? 'cursor-pointer' : ''} `} /> {isClickableType && ( )} {isOpen && isDateType && !inputProps.disabled && ( setIsOpen(false)} anchorName={anchorName} /> )} {isOpen && isColorType && !inputProps.disabled && ( setIsOpen(false)} anchorName={anchorName} /> )}
) }