All files / src/ahuora-design-system/ui auto-select-input.tsx

100% Statements 8/8
100% Branches 5/5
100% Functions 4/4
100% Lines 8/8

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 69 70 71 72 73 74                                            3102x   3102x 44x     3102x 44x 44x     3102x                   44x                                                            
import { Pencil } from "lucide-react";
import DebouncedInput from "./debounced-input";
import { useState } from "react";
import { ToolTipCover } from "./tooltip";
import { cn } from "@/lib/utils";
 
export type AutoSelectInputProps = {
  value: string;
  onUpdateValue: (value: string) => void;
  textSize?: string;
  width?: string;
  textColor?: string;
};
 
export function AutoSelectInput({
  value,
  width,
  onUpdateValue,
  textSize = "",
  textColor,
  ...props
}: AutoSelectInputProps) {
  const [isEditing, setIsEditing] = useState(false);
 
  const handleStartEditing = () => {
    setIsEditing(true);
  };
 
  const handleFinishEditing = (update: string) => {
    setIsEditing(false);
    onUpdateValue(update);
  };
 
  return (
    <>
      {isEditing ? (
        <DebouncedInput
          autoSelect
          className={cn("text-muted-foreground font-medium w-full", textSize)}
          value={value}
          endIcon={Pencil}
          onUpdate={handleFinishEditing}
          autoFocus
          onBlur={() => setIsEditing(false)}
          {...props}
        />
      ) : (
        <ToolTipCover content="Click to edit name" asChild>
          <button
            onClick={handleStartEditing}
            className={cn(
              "hover:text-foreground-muted flex items-center gap-2 group w-full truncate",
              width,
            )}
            {...props}
          >
            <span
              className={cn(
                "truncate strong",
                textSize,
                textColor || "text-foreground",
              )}
            >
              {value}
            </span>{" "}
            {/* Apply truncate here */}
            <Pencil className="h-4 w-4 opacity-0 group-hover:opacity-100 transition-opacity" />
          </button>
        </ToolTipCover>
      )}
    </>
  );
}