Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

3.5 KB
Raw Blame
"use client";

import { forwardRef } from "react";
import { cn } from "@/lib/utils";
import {
  Cancel01Icon,
  Loading03Icon,
  Search01Icon,
} from "@hugeicons/core-free-icons";
import { Icon } from "@/components/atoms/Icon/Icon";

interface Props {
  value: string;
  onChange: (next: string) => void;
  placeholder?: string;
  "aria-label"?: string;
  disabled?: boolean;
  loading?: boolean;
  maxLength?: number;
  name?: string;
  autoComplete?: string;
  spellCheck?: boolean;
  size?: "xsmall" | "small" | "medium";
  className?: string;
}

const sizeStyles = {
  xsmall: "h-7 rounded-md pl-8 pr-7 text-xs leading-5",
  small: "h-[36px] pl-10 pr-9 text-sm leading-[22px]",
  medium: "h-[46px] pl-12 pr-10 text-sm leading-[22px]",
} as const;

const iconOffset = {
  xsmall: { left: "left-2.5", right: "right-1" },
  small: { left: "left-3", right: "right-2" },
  medium: { left: "left-4", right: "right-3" },
} as const;

export const SearchInput = forwardRef<HTMLInputElement, Props>(
  function SearchInput(
    {
      value,
      onChange,
      placeholder = "Search",
      "aria-label": ariaLabel,
      disabled,
      loading,
      maxLength,
      name,
      autoComplete,
      spellCheck,
      size = "medium",
      className,
    },
    ref,
  ) {
    const hasValue = value.length > 0;
    return (
      <div className={cn("relative w-full", className)}>
        <Icon
          icon={Search01Icon}
          size={size === "medium" ? 20 : size === "small" ? 16 : 14}
          className={cn(
            "pointer-events-none absolute top-1/2 -translate-y-1/2 text-muted-foreground",
            iconOffset[size].left,
          )}
        />
        <input
          ref={ref}
          type="search"
          value={value}
          onChange={(e) => onChange(e.target.value)}
          placeholder={placeholder}
          aria-label={ariaLabel ?? placeholder}
          disabled={disabled}
          maxLength={maxLength}
          name={name}
          autoComplete={autoComplete}
          spellCheck={spellCheck}
          className={cn(
            "w-full rounded-xl border border-input bg-background text-foreground placeholder:text-muted-foreground focus:border-ring focus:outline-none focus:ring-1 focus:ring-ring disabled:cursor-not-allowed disabled:opacity-60",
            sizeStyles[size],
            "[&::-webkit-search-cancel-button]:appearance-none",
          )}
        />
        {loading ? (
          <span
            role="status"
            aria-label="Searching"
            className={cn(
              "absolute top-1/2 flex size-6 -translate-y-1/2 items-center justify-center text-muted-foreground",
              iconOffset[size].right,
            )}
          >
            <Icon
              icon={Loading03Icon}
              size={size === "medium" ? 16 : 14}
              className="animate-spin"
            />
          </span>
        ) : hasValue && !disabled ? (
          <button
            type="button"
            onClick={() => onChange("")}
            aria-label="Clear search"
            className={cn(
              "absolute top-1/2 flex size-6 -translate-y-1/2 items-center justify-center rounded-full text-muted-foreground transition hover:bg-muted hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
              iconOffset[size].right,
            )}
          >
            <Icon icon={Cancel01Icon} size={size === "medium" ? 14 : 12} />
          </button>
        ) : null}
      </div>
    );
  },
);