index.tsx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. 'use client'
  2. import type { SVGProps } from 'react'
  3. import React, { useState } from 'react'
  4. import { useTranslation } from 'react-i18next'
  5. import s from './style.module.css'
  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={s.prefix}>{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={`${s.input} ${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