index.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use client'
  2. import type { SVGProps } from 'react'
  3. import React, { useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import cn from 'classnames'
  6. type InputProps = {
  7. placeholder?: string
  8. value?: string
  9. defaultValue?: string
  10. onChange?: (v: string) => void
  11. className?: string
  12. wrapperClassName?: string
  13. type?: string
  14. showPrefix?: React.ReactNode
  15. prefixIcon?: React.ReactNode
  16. }
  17. const GlassIcon = ({ className }: SVGProps<SVGElement>) => (
  18. <svg width="14" height="14" viewBox="0 0 14 14" fill="none" xmlns="http://www.w3.org/2000/svg" className={className ?? ''}>
  19. <path d="M12.25 12.25L10.2084 10.2083M11.6667 6.70833C11.6667 9.44675 9.44675 11.6667 6.70833 11.6667C3.96992 11.6667 1.75 9.44675 1.75 6.70833C1.75 3.96992 3.96992 1.75 6.70833 1.75C9.44675 1.75 11.6667 3.96992 11.6667 6.70833Z" stroke="#344054" strokeWidth="1.25" strokeLinecap="round" strokeLinejoin="round" />
  20. </svg>
  21. )
  22. const Input = ({ value, defaultValue, onChange, className = '', wrapperClassName = '', placeholder, type, showPrefix, prefixIcon }: InputProps) => {
  23. const [localValue, setLocalValue] = useState(value ?? defaultValue)
  24. const { t } = useTranslation()
  25. return (
  26. <div className={`relative inline-flex w-full ${wrapperClassName}`}>
  27. {showPrefix && <span className='whitespace-nowrap absolute left-2 self-center'>{prefixIcon ?? <GlassIcon className='h-3.5 w-3.5 stroke-current text-gray-700 stroke-2' />}</span>}
  28. <input
  29. type={type ?? 'text'}
  30. className={cn('inline-flex h-7 w-full py-1 px-2 rounded-lg text-xs leading-normal bg-gray-100 caret-primary-600 hover:bg-gray-100 focus:ring-1 focus:ring-inset focus:ring-gray-200 focus-visible:outline-none focus:bg-white placeholder:text-gray-400', showPrefix ? '!pl-7' : '', className)}
  31. placeholder={placeholder ?? (showPrefix ? t('common.operation.search') ?? '' : 'please input')}
  32. value={localValue}
  33. onChange={(e) => {
  34. setLocalValue(e.target.value)
  35. onChange && onChange(e.target.value)
  36. }}
  37. />
  38. </div>
  39. )
  40. }
  41. export default Input