-
Notifications
You must be signed in to change notification settings - Fork 3
/
reffff.txt
51 lines (44 loc) · 1.55 KB
/
reffff.txt
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
import React, { useRef, useState, useEffect } from "react";
const AutoComplete = () => {
// ... (other state and functions)
const suggestionsRef = useRef(null);
useEffect(() => {
// Ensure that the selected suggestion is always in the view
if (suggestionsRef.current) {
const suggestionElement = suggestionsRef.current.children[selectedSuggestionIndex];
if (suggestionElement) {
// Calculate the scroll position to make the selected suggestion visible
const suggestionTop = suggestionElement.offsetTop;
const suggestionHeight = suggestionElement.clientHeight;
const containerHeight = suggestionsRef.current.clientHeight;
const scrollTop = suggestionsRef.current.scrollTop;
if (suggestionTop < scrollTop) {
suggestionsRef.current.scrollTop = suggestionTop;
} else if (suggestionTop + suggestionHeight > scrollTop + containerHeight) {
suggestionsRef.current.scrollTop = suggestionTop + suggestionHeight - containerHeight;
}
}
}
}, [selectedSuggestionIndex]);
// ... (rest of the component)
return (
<div>
<input
type="text"
value={query}
onChange={handleInputChange}
onKeyDown={handleKeyDown}
placeholder="Search for something..."
ref={inputRef}
/>
<div
className="suggestions"
style={{ maxHeight: "200px", overflowY: "auto" }}
ref={suggestionsRef}
>
{ /* suggestions mapping code */}
</div>
</div>
);
};
export default AutoComplete;