All files / src/shared/components SearchInput.tsx

100% Statements 5/5
100% Branches 5/5
100% Functions 3/3
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68                                                              239x 239x                     147x                         2x 2x                    
import { useRef } from 'react'
import { X } from 'lucide-react'
import { Input } from '@/components/ui/input'
import { Label } from '@/components/ui/label'
import { cn } from '@/lib/utils'
import { SearchIcon } from '@/shared/icons/search-icon'
 
interface SearchInputProps {
  id: string
  label: string
  value: string
  onChange: (value: string) => void
  placeholder: string
  className?: string
  inputClassName?: string
  clearLabel?: string
}
 
// A visually-icon-only search affordance still needs a real associated
// <label> for screen reader users — kept off-screen
// with sr-only rather than dropped in favor of just aria-label/placeholder.
export function SearchInput({
  id,
  label,
  value,
  onChange,
  placeholder,
  className,
  inputClassName,
  clearLabel,
}: SearchInputProps) {
  const inputRef = useRef<HTMLInputElement>(null)
  return (
    <div className={cn('relative', className)}>
      <Label htmlFor={id} className="sr-only">
        {label}
      </Label>
      <SearchIcon className="text-ink-faint pointer-events-none absolute top-1/2 left-3 h-4 w-4 -translate-y-1/2" />
      <Input
        ref={inputRef}
        id={id}
        type="search"
        value={value}
        onChange={(event) => onChange(event.target.value)}
        placeholder={placeholder}
        className={cn(
          'pl-9',
          clearLabel && 'pr-12 [&::-webkit-search-cancel-button]:appearance-none',
          inputClassName,
        )}
      />
      {clearLabel && value && (
        <button
          type="button"
          aria-label={clearLabel}
          onClick={() => {
            onChange('')
            inputRef.current?.focus()
          }}
          className="absolute right-0 top-1/2 flex size-11 -translate-y-1/2 items-center justify-center rounded-xl text-ink-muted hover:text-ink"
        >
          <X aria-hidden className="size-4" />
        </button>
      )}
    </div>
  )
}