All files / components/Scaffold QuestionSearch.tsx

100% Statements 42/42
100% Branches 41/41
100% Functions 14/14
100% Lines 37/37

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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189                                207x 207x 207x     207x 75x   56x 56x         207x   71x       207x 69x 8x       4x     69x 69x     207x 97x     207x 4x 4x 4x     207x 10x 10x 10x     207x                             207x                 7x     2x 1x 2x                         1x 1x 1x 1x                                                                           35x   3x                                                                            
import { useState, useEffect, useRef } from "react";
import type { Question } from "main/types/conceptGraph";
 
interface QuestionSearchProps {
  questions: Question[];
  selectedQuestionId: string;
  onSelect: (id: string) => void;
  disabled: boolean;
}
 
export default function QuestionSearch({
  questions,
  selectedQuestionId,
  onSelect,
  disabled,
}: QuestionSearchProps) {
  const [inputValue, setInputValue] = useState("");
  const [isOpen, setIsOpen] = useState(false);
  const containerRef = useRef<HTMLDivElement>(null);
 
  // Reset when the questions list is cleared (assessment deselected or changed)
  useEffect(() => {
    if (questions.length === 0) {
      // eslint-disable-next-line react-hooks/set-state-in-effect
      setInputValue("");
      setIsOpen(false);
    }
  }, [questions]);
 
  // Reset if question is cleared externally
  useEffect(() => {
    // eslint-disable-next-line react-hooks/set-state-in-effect
    if (!selectedQuestionId) setInputValue("");
  }, [selectedQuestionId]);
 
  // Close dropdown on outside click
  useEffect(() => {
    const handler = (e: MouseEvent) => {
      if (
        containerRef.current &&
        !containerRef.current.contains(e.target as Node)
      ) {
        setIsOpen(false);
      }
    };
    document.addEventListener("mousedown", handler, true);
    return () => document.removeEventListener("mousedown", handler, true);
  }, []);
 
  const filtered = questions.filter((q) =>
    q.title.toLowerCase().includes(inputValue.toLowerCase()),
  );
 
  const handleSelect = (q: Question) => {
    setInputValue(q.title);
    setIsOpen(false);
    onSelect(q.id);
  };
 
  const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setInputValue(e.target.value);
    setIsOpen(true);
    if (!e.target.value) onSelect("");
  };
 
  const inputStyle: React.CSSProperties = {
    width: "100%",
    height: 28,
    padding: "0px 10px",
    fontFamily: "Helvetica, Arial, sans-serif",
    fontSize: 13,
    background: disabled ? "#ffffff" : "#ffffff",
    color: "#1E293B",
    border: "1px solid #000000",
    borderRadius: 6,
    outline: "none",
    cursor: disabled ? "not-allowed" : "text",
    boxSizing: "border-box",
  };
 
  return (
    <div ref={containerRef} style={{ position: "relative", minWidth: 240 }}>
      {/* Input + clear button wrapper */}
      <div style={{ position: "relative" }}>
        <input
          type="text"
          value={inputValue}
          onChange={handleChange}
          onFocus={() => {
            if (!disabled) setIsOpen(true);
          }}
          onKeyDown={(e) => {
            if (e.key === "Enter" && filtered.length > 0)
              handleSelect(filtered[0]);
            if (e.key === "Escape") setIsOpen(false);
          }}
          placeholder={
            disabled
              ? "Select assessment to pick question"
              : "Search questions…"
          }
          disabled={disabled}
          style={{ ...inputStyle, paddingRight: inputValue ? 28 : 10 }}
        />
        {inputValue && (
          <button
            onMouseDown={(e) => {
              e.preventDefault();
              setInputValue("");
              setIsOpen(false);
              onSelect("");
            }}
            style={{
              position: "absolute",
              right: 6,
              top: "50%",
              transform: "translateY(-50%)",
              background: "none",
              border: "none",
              cursor: "pointer",
              color: "#94A3B8",
              fontSize: 14,
              lineHeight: 1,
              padding: 2,
            }}
          >
            ✕
          </button>
        )}
      </div>
 
      {isOpen && !disabled && filtered.length > 0 && (
        <div
          style={{
            position: "absolute",
            top: "calc(100% + 4px)",
            left: 0,
            right: 0,
            background: "#fff",
            border: "1px solid #E2E8F0",
            borderRadius: 6,
            boxShadow: "0 4px 16px rgba(0,0,0,0.1)",
            zIndex: 100,
            maxHeight: 260,
            overflowY: "auto",
          }}
        >
          {filtered.map((q, i) => (
            <div
              key={q.id}
              onMouseDown={() => handleSelect(q)}
              className={
                "dropdown-option" +
                (q.id === selectedQuestionId ? " is-selected" : "")
              }
              style={{
                borderBottom:
                  i < filtered.length - 1 ? "1px solid #F1F5F9" : "none",
              }}
            >
              {q.title}
            </div>
          ))}
        </div>
      )}
 
      {isOpen && !disabled && filtered.length === 0 && inputValue && (
        <div
          style={{
            position: "absolute",
            top: "calc(100% + 4px)",
            left: 0,
            right: 0,
            background: "#fff",
            border: "1px solid #E2E8F0",
            borderRadius: 6,
            padding: "8px 12px",
            fontSize: 13,
            color: "#94A3B8",
            zIndex: 100,
          }}
        >
          No questions match "{inputValue}"
        </div>
      )}
    </div>
  );
}