index.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import type { FC } from 'react'
  2. import { useRef, 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. const isComposing = useRef<boolean>(false)
  24. return (
  25. <div className={cn(
  26. 'group flex items-center px-2 h-8 rounded-lg bg-gray-200 hover:bg-gray-300 border border-transparent overflow-hidden',
  27. focus && '!bg-white hover:bg-white shadow-xs !border-gray-300',
  28. !focus && value && 'hover:!bg-gray-200 hover:!shadow-xs hover:!border-black/5',
  29. white && '!bg-white hover:!bg-white shadow-xs !border-gray-300 hover:!border-gray-300',
  30. className,
  31. )}>
  32. <div className="pointer-events-none shrink-0 flex items-center mr-1.5 justify-center w-4 h-4">
  33. <RiSearchLine className="h-3.5 w-3.5 text-gray-500" aria-hidden="true" />
  34. </div>
  35. <input
  36. type="text"
  37. name="query"
  38. className={cn(
  39. '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',
  40. focus && '!bg-white hover:bg-white group-hover:bg-white placeholder:!text-gray-400',
  41. !focus && value && 'hover:!bg-gray-200 group-hover:!bg-gray-200',
  42. white && '!bg-white hover:!bg-white group-hover:!bg-white placeholder:!text-gray-400',
  43. )}
  44. placeholder={placeholder || t('common.operation.search')!}
  45. value={value}
  46. onChange={(e) => {
  47. if (!isComposing.current)
  48. onChange(e.target.value)
  49. }}
  50. onCompositionStart={() => {
  51. isComposing.current = true
  52. }}
  53. onCompositionEnd={() => {
  54. isComposing.current = false
  55. }}
  56. onFocus={() => setFocus(true)}
  57. onBlur={() => setFocus(false)}
  58. autoComplete="off"
  59. />
  60. {value && (
  61. <div
  62. className='shrink-0 flex items-center justify-center w-4 h-4 cursor-pointer group/clear'
  63. onClick={() => onChange('')}
  64. >
  65. <XCircle className='w-3.5 h-3.5 text-gray-400 group-hover/clear:text-gray-600' />
  66. </div>
  67. )}
  68. </div>
  69. )
  70. }
  71. export default SearchInput