index.tsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import type { FC } from 'react'
  2. import { useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { RiSearchLine } from '@remixicon/react'
  5. import cn from '@/utils/classnames'
  6. import { XCircle } from '@/app/components/base/icons/src/vender/solid/general'
  7. type SearchInputProps = {
  8. placeholder?: string
  9. className?: string
  10. value: string
  11. onChange: (v: string) => void
  12. white?: boolean
  13. }
  14. const SearchInput: FC<SearchInputProps> = ({
  15. placeholder,
  16. className,
  17. value,
  18. onChange,
  19. white,
  20. }) => {
  21. const { t } = useTranslation()
  22. const [focus, setFocus] = useState<boolean>(false)
  23. return (
  24. <div className={cn(
  25. 'group flex items-center px-2 h-8 rounded-lg bg-gray-200 hover:bg-gray-300 border border-transparent overflow-hidden',
  26. focus && '!bg-white hover:bg-white shadow-xs !border-gray-300',
  27. !focus && value && 'hover:!bg-gray-200 hover:!shadow-xs hover:!border-black/5',
  28. white && '!bg-white hover:!bg-white shadow-xs !border-gray-300 hover:!border-gray-300',
  29. className,
  30. )}>
  31. <div className="pointer-events-none shrink-0 flex items-center mr-1.5 justify-center w-4 h-4">
  32. <RiSearchLine className="h-3.5 w-3.5 text-gray-500" aria-hidden="true" />
  33. </div>
  34. <input
  35. type="text"
  36. name="query"
  37. className={cn(
  38. 'grow block h-[18px] bg-gray-200 border-0 text-gray-700 text-[13px] placeholder:text-gray-500 appearance-none outline-none group-hover:bg-gray-300 caret-blue-600',
  39. focus && '!bg-white hover:bg-white group-hover:bg-white placeholder:!text-gray-400',
  40. !focus && value && 'hover:!bg-gray-200 group-hover:!bg-gray-200',
  41. white && '!bg-white hover:!bg-white group-hover:!bg-white placeholder:!text-gray-400',
  42. )}
  43. placeholder={placeholder || t('common.operation.search')!}
  44. value={value}
  45. onChange={(e) => {
  46. onChange(e.target.value)
  47. }}
  48. onFocus={() => setFocus(true)}
  49. onBlur={() => setFocus(false)}
  50. autoComplete="off"
  51. />
  52. {value && (
  53. <div
  54. className='shrink-0 flex items-center justify-center w-4 h-4 cursor-pointer group/clear'
  55. onClick={() => onChange('')}
  56. >
  57. <XCircle className='w-3.5 h-3.5 text-gray-400 group-hover/clear:text-gray-600' />
  58. </div>
  59. )}
  60. </div>
  61. )
  62. }
  63. export default SearchInput