aboutsummaryrefslogtreecommitdiff
path: root/client/src/components/CustomSelect/CustomSelect.jsx
blob: 4a906bdc05355d48e0766b67153447ef650260ed (plain)
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
import React, { useEffect, useState, useRef } from "react";
import { SUPPORTED_LANGUAGES } from "../../utils/constants";

import styles from "./CustomSelect.module.css";

const CustomSelect = ({ onSelect, textAreaRef, isModalOpen }) => {
  const [isOpen, setIsOpen] = useState(false);
  const [options, setOptions] = useState(SUPPORTED_LANGUAGES);
  const [selectedOption, setSelectedOption] = useState(
    options.length > 0 ? options[0] : null,
  );
  const [focusedOptionIndex, setFocusedOptionIndex] = useState(0);
  const selectRef = useRef(null);
  const searchRef = useRef(null);
  const optionsRef = useRef([]);

  const toggleDropdown = () => {
    setIsOpen(!isOpen);
  };

  const handleOptionClick = (option, index) => {
    setSelectedOption(option);
    onSelect(option.value);
    setFocusedOptionIndex(index);
    setIsOpen(false);
    setOptions(SUPPORTED_LANGUAGES);
    if (textAreaRef.current) {
      textAreaRef.current.focus();
    }
  };

  const handleClickOutside = (event) => {
    if (selectRef.current && !selectRef.current.contains(event.target)) {
      setIsOpen(false);
      setOptions(SUPPORTED_LANGUAGES);

      if (textAreaRef.current) {
        textAreaRef.current.focus();
      }
    }
  };

  const handleChange = (e) => {
    const searchVal = e.target.value;
    const filteredOptions =
      searchVal.length > 0
        ? SUPPORTED_LANGUAGES.filter((option) =>
            option.label.toLowerCase().includes(searchVal.toLowerCase()),
          )
        : SUPPORTED_LANGUAGES;

    setOptions(filteredOptions);
    setFocusedOptionIndex(0);
  };

  const handleKeyDown = (event) => {
    if (isOpen) {
      switch (event.key) {
        case "ArrowDown":
          setFocusedOptionIndex((prevIndex) => {
            const newIndex =
              prevIndex < options.length - 1 ? prevIndex + 1 : prevIndex;
            optionsRef.current[newIndex].scrollIntoView({ block: "nearest" });
            return newIndex;
          });
          break;
        case "ArrowUp":
          setFocusedOptionIndex((prevIndex) => {
            const newIndex = prevIndex > 0 ? prevIndex - 1 : prevIndex;
            optionsRef.current[newIndex].scrollIntoView({ block: "nearest" });
            return newIndex;
          });
          break;
        case "Enter":
          event.preventDefault();
          handleOptionClick(options[focusedOptionIndex], focusedOptionIndex);
          break;
        case "Escape":
          setIsOpen(false);
          setOptions(SUPPORTED_LANGUAGES);
          if (textAreaRef.current) {
            textAreaRef.current.focus();
          }
          break;
        default:
          break;
      }
    } else if (!isModalOpen && event.ctrlKey && event.key === "l") {
      event.preventDefault();
      setIsOpen(true);
    }
  };

  useEffect(() => {
    document.addEventListener("mousedown", handleClickOutside);
    document.addEventListener("keydown", handleKeyDown);
    return () => {
      document.removeEventListener("mousedown", handleClickOutside);
      document.removeEventListener("keydown", handleKeyDown);
    };
  }, [isOpen, options, focusedOptionIndex]);

  useEffect(() => {
    if (isOpen && searchRef.current) {
      searchRef.current.focus();
    }
  }, [isOpen]);

  return (
    <div className={styles.select} ref={selectRef}>
      <div className={styles.selected__option} onClick={toggleDropdown}>
        {selectedOption ? (
          <>
            <span>&#x3c;&#x2f;&#x3e;</span>
            <span>{selectedOption.label}</span>
            <span>&#9660;</span>
          </>
        ) : (
          "Select an option"
        )}
      </div>
      {isOpen && (
        <div className={styles.options__container}>
          <input
            onChange={handleChange}
            className={styles.options__search}
            ref={searchRef}
            placeholder="Search..."
          />
          <div className={styles.options}>
            {options.map((option, index) => (
              <div
                key={option.value}
                className={`${styles.option} ${index === focusedOptionIndex ? styles.focused : ""}`}
                onClick={() => handleOptionClick(option, index)}
                tabIndex={0}
                ref={(el) => (optionsRef.current[index] = el)}
              >
                {option.label}
              </div>
            ))}
          </div>
        </div>
      )}
    </div>
  );
};

export default CustomSelect;