index.tsx 2.8 KB

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