| 
 | 1 | +import React, { useState } from 'react';  | 
 | 2 | +interface TextInputGenProps {  | 
 | 3 | +  subjectName?: string;  | 
 | 4 | +  onAccept: (value: string) => void;  | 
 | 5 | +  // onCancel: () => void;  | 
 | 6 | +  autocomplete: string[];  | 
 | 7 | +}  | 
 | 8 | + | 
 | 9 | +const TextInputGen: React.FC<TextInputGenProps> = ({  | 
 | 10 | +  subjectName = '',  | 
 | 11 | +  onAccept,  | 
 | 12 | +  // onCancel,  | 
 | 13 | +  autocomplete,  | 
 | 14 | +}) => {  | 
 | 15 | +  const [inputValue, setInputValue] = useState(`${subjectName}`);  | 
 | 16 | +  const [suggestions, setSuggestions] = useState<string[]>([]);  | 
 | 17 | +  const [selectedIndex, setSelectedIndex] = useState<number | null>(null);  | 
 | 18 | + | 
 | 19 | +  // const typedDictionary: string[] = autocomplete;  | 
 | 20 | + | 
 | 21 | +  // Show all descriptors on focus  | 
 | 22 | +  const handleFocus = () => {  | 
 | 23 | +    setSuggestions(autocomplete);  | 
 | 24 | +    setSelectedIndex(null);  | 
 | 25 | +  };  | 
 | 26 | + | 
 | 27 | +  const handleInputChange = (value: string) => {  | 
 | 28 | +    setInputValue(value);  | 
 | 29 | +    setSelectedIndex(null); // Reset selected index  | 
 | 30 | + | 
 | 31 | +    // Filter dictionary to find matches  | 
 | 32 | +    if (value.trim() === '') {  | 
 | 33 | +      setSuggestions(autocomplete);  | 
 | 34 | +    } else {  | 
 | 35 | +      const lowerCaseValue = value.toLowerCase();  | 
 | 36 | +      const filtered = autocomplete.filter((word) =>  | 
 | 37 | +        word.toLowerCase().startsWith(lowerCaseValue)  | 
 | 38 | +      );  | 
 | 39 | +      setSuggestions(filtered);  | 
 | 40 | +    }  | 
 | 41 | +  };  | 
 | 42 | + | 
 | 43 | +  // const handleAccept = () => {  | 
 | 44 | +  //   onAccept(inputValue);  | 
 | 45 | +  //   setSuggestions([]); // Clear suggestions on accept  | 
 | 46 | +  // };  | 
 | 47 | + | 
 | 48 | +  const handleSuggestionClick = (suggestion: string) => {  | 
 | 49 | +    setInputValue(`${subjectName}'s ${suggestion}`); // Update input with the selected suggestion  | 
 | 50 | +    setSuggestions([]); // Clear suggestions  | 
 | 51 | +    onAccept(`${subjectName}'s ${suggestion}`);  | 
 | 52 | +  };  | 
 | 53 | + | 
 | 54 | +  const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {  | 
 | 55 | +    if (suggestions.length === 0) return;  | 
 | 56 | + | 
 | 57 | +    if (e.key === 'ArrowDown') {  | 
 | 58 | +      setSelectedIndex((prev) =>  | 
 | 59 | +        prev === null || prev === suggestions.length - 1 ? 0 : prev + 1  | 
 | 60 | +      );  | 
 | 61 | +    } else if (e.key === 'ArrowUp') {  | 
 | 62 | +      setSelectedIndex((prev) =>  | 
 | 63 | +        prev === null || prev === 0 ? suggestions.length - 1 : prev - 1  | 
 | 64 | +      );  | 
 | 65 | +    } else if (e.key === 'Enter') {  | 
 | 66 | +      if (selectedIndex !== null) {  | 
 | 67 | +        handleSuggestionClick(suggestions[selectedIndex]);  | 
 | 68 | +      } else {  | 
 | 69 | +        onAccept(inputValue); // Accept typed text if no suggestion is highlighted  | 
 | 70 | +        setSuggestions([]);  | 
 | 71 | +      }  | 
 | 72 | +      e.preventDefault();  | 
 | 73 | +    }  | 
 | 74 | +  };  | 
 | 75 | + | 
 | 76 | +  return (  | 
 | 77 | +    <div className='flex flex-col space-y-4 relative'>  | 
 | 78 | +      <input  | 
 | 79 | +        type='text'  | 
 | 80 | +        className='w-full p-2 border bg-gray-200 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 text-black'  | 
 | 81 | +        value={inputValue}  | 
 | 82 | +        onChange={(e) => handleInputChange(e.target.value)}  | 
 | 83 | +        onFocus={handleFocus}  | 
 | 84 | +        onKeyDown={handleKeyDown} // Added onKeyDown  | 
 | 85 | +      />  | 
 | 86 | +      <span className='absolute right-2 text-gray-500 pointer-events-none'>  | 
 | 87 | +        ▼{' '}  | 
 | 88 | +      </span>  | 
 | 89 | +      {/* Suggestions Dropdown */}  | 
 | 90 | +      {suggestions.length > 0 && (  | 
 | 91 | +        <ul className='left-0 w-full bg-white border border-gray-300 rounded-lg shadow-lg max-h-60 overflow-y-auto z-10 text-black text-left'>  | 
 | 92 | +          {suggestions.map((suggestion, index) => (  | 
 | 93 | +            <li  | 
 | 94 | +              key={index}  | 
 | 95 | +              className={`px-4 py-2 hover:bg-blue-100 cursor-pointer ${  | 
 | 96 | +                selectedIndex === index ? 'bg-blue-500 text-white' : ''  | 
 | 97 | +              }`}  | 
 | 98 | +              onClick={() => handleSuggestionClick(suggestion)}  | 
 | 99 | +            >  | 
 | 100 | +              {`${subjectName}'s ${suggestion}`}  | 
 | 101 | +            </li>  | 
 | 102 | +          ))}  | 
 | 103 | +        </ul>  | 
 | 104 | +      )}  | 
 | 105 | + | 
 | 106 | +      {/* <div className='flex justify-start space-x-2'>  | 
 | 107 | +        <button  | 
 | 108 | +          onClick={handleAccept}  | 
 | 109 | +          className='bg-blue-500 text-white px-4 py-2 rounded-lg hover:bg-blue-600'  | 
 | 110 | +        >  | 
 | 111 | +          Accept  | 
 | 112 | +        </button>  | 
 | 113 | +        <button  | 
 | 114 | +          onClick={onCancel}  | 
 | 115 | +          className='bg-gray-500 text-white px-4 py-2 rounded-lg hover:bg-gray-600'  | 
 | 116 | +        >  | 
 | 117 | +          Cancel  | 
 | 118 | +        </button>  | 
 | 119 | +      </div> */}  | 
 | 120 | +    </div>  | 
 | 121 | +  );  | 
 | 122 | +};  | 
 | 123 | + | 
 | 124 | +export default TextInputGen;  | 
0 commit comments