File size: 2,579 Bytes
bf8b26e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
import React, { forwardRef } from 'react';
import { classNames } from '~/utils/classNames';
import { Input } from './Input';
import { motion, AnimatePresence } from 'framer-motion';

interface SearchInputProps extends React.InputHTMLAttributes<HTMLInputElement> {
  /** Function to call when the clear button is clicked */
  onClear?: () => void;

  /** Whether to show the clear button when there is input */
  showClearButton?: boolean;

  /** Additional class name for the search icon */
  iconClassName?: string;

  /** Additional class name for the container */
  containerClassName?: string;

  /** Whether the search is loading */
  loading?: boolean;
}

/**
 * SearchInput component
 *
 * A search input field with a search icon and optional clear button.
 */
export const SearchInput = forwardRef<HTMLInputElement, SearchInputProps>(
  (
    { className, onClear, showClearButton = true, iconClassName, containerClassName, loading = false, ...props },
    ref,
  ) => {
    const hasValue = Boolean(props.value);

    return (
      <div className={classNames('relative flex items-center w-full', containerClassName)}>
        {/* Search icon or loading spinner */}
        <div
          className={classNames(
            'absolute left-3 top-1/2 -translate-y-1/2 text-bolt-elements-textTertiary',
            iconClassName,
          )}
        >
          {loading ? (
            <span className="i-ph:spinner-gap animate-spin w-4 h-4" />
          ) : (
            <span className="i-ph:magnifying-glass w-4 h-4" />
          )}
        </div>

        {/* Input field */}
        <Input
          ref={ref}
          className={classNames('pl-10', hasValue && showClearButton ? 'pr-10' : '', className)}
          {...props}
        />

        {/* Clear button */}
        <AnimatePresence>
          {hasValue && showClearButton && (
            <motion.button
              initial={{ opacity: 0, scale: 0.8 }}
              animate={{ opacity: 1, scale: 1 }}
              exit={{ opacity: 0, scale: 0.8 }}
              transition={{ duration: 0.15 }}
              type="button"
              onClick={onClear}
              className="absolute right-3 top-1/2 -translate-y-1/2 text-bolt-elements-textTertiary hover:text-bolt-elements-textSecondary p-1 rounded-full hover:bg-bolt-elements-background-depth-2"
              aria-label="Clear search"
            >
              <span className="i-ph:x w-3.5 h-3.5" />
            </motion.button>
          )}
        </AnimatePresence>
      </div>
    );
  },
);

SearchInput.displayName = 'SearchInput';