import cn from '@/utils/classnames' import { RiCloseCircleFill, RiErrorWarningLine, RiSearchLine } from '@remixicon/react' import { type VariantProps, cva } from 'class-variance-authority' import { noop } from 'lodash-es' import type { CSSProperties, ChangeEventHandler, FocusEventHandler } from 'react' import React from 'react' import { useTranslation } from 'react-i18next' import { CopyFeedbackNew } from '../copy-feedback' export const inputVariants = cva( '', { variants: { size: { regular: 'px-3 radius-md system-sm-regular', large: 'px-4 radius-lg system-md-regular', }, }, defaultVariants: { size: 'regular', }, }, ) export type InputProps = { showLeftIcon?: boolean showClearIcon?: boolean showCopyIcon?: boolean onClear?: () => void disabled?: boolean destructive?: boolean wrapperClassName?: string styleCss?: CSSProperties unit?: string } & Omit, 'size'> & VariantProps const removeLeadingZeros = (value: string) => value.replace(/^(-?)0+(?=\d)/, '$1') const Input = React.forwardRef(({ size, disabled, destructive, showLeftIcon, showClearIcon, showCopyIcon, onClear, wrapperClassName, className, styleCss, value, placeholder, onChange = noop, onBlur = noop, unit, ...props }, ref) => { const { t } = useTranslation() const handleNumberChange: ChangeEventHandler = (e) => { if (value === 0) { // remove leading zeros const formattedValue = removeLeadingZeros(e.target.value) if (e.target.value !== formattedValue) e.target.value = formattedValue } onChange(e) } const handleNumberBlur: FocusEventHandler = (e) => { // remove leading zeros const formattedValue = removeLeadingZeros(e.target.value) if (e.target.value !== formattedValue) { e.target.value = formattedValue onChange({ ...e, type: 'change', target: { ...e.target, value: formattedValue, }, }) } onBlur(e) } return (
{showLeftIcon && } {showClearIcon && value && !disabled && !destructive && (
)} {destructive && ( )} {showCopyIcon && (
)} { unit && (
{unit}
) }
) }) Input.displayName = 'Input' export default Input