Skip to content

Commit ea51247

Browse files
authored
Merge pull request #2 from nossas/feature/certificates
Feature/certificates
2 parents 88d7ce8 + 9c7f1d8 commit ea51247

File tree

5 files changed

+377
-128
lines changed

5 files changed

+377
-128
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import React, { useState, useRef, useEffect } from "react";
2+
import styled from "styled-components";
3+
4+
5+
const DomainAutocompleteStyled = styled.div`
6+
position: relative;
7+
flex: 1;
8+
9+
input {
10+
width: 100%;
11+
padding: 10px 20px;
12+
}
13+
14+
ul {
15+
position: absolute;
16+
width: 100%;
17+
border-top: 1px solid #c7c7c7;
18+
list-style-type: none;
19+
margin: 0;
20+
padding: 0;
21+
background-color: white;
22+
23+
max-height: 200px;
24+
overflow: auto;
25+
26+
li {
27+
padding: 10px 20px;
28+
29+
&:hover, &.active {
30+
background-color: #c7c7c7;
31+
}
32+
}
33+
}
34+
35+
.help-text {
36+
font-size: 14px;
37+
}
38+
`
39+
40+
41+
const DomainAutocomplete = ({ name, initialValue, domains, helpText }) => {
42+
const [inputValue, setInputValue] = useState(initialValue || "");
43+
const [suggestions, setSuggestions] = useState([]);
44+
const [selectedIndex, setSelectedIndex] = useState(-1);
45+
const containerRef = useRef(null); // Ref para a div do autocomplete
46+
47+
const handleChange = (e) => {
48+
const value = e.target.value;
49+
setInputValue(value);
50+
setSelectedIndex(-1);
51+
52+
if (value.includes(".")) {
53+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
54+
const [subdomain, root] = value.split(".").slice(-2);
55+
setSuggestions(domains.filter(domain => domain.startsWith(root)));
56+
} else {
57+
setSuggestions(domains.filter(domain => domain.startsWith(value)));
58+
}
59+
};
60+
61+
const handleKeyDown = (e) => {
62+
if (e.key === "ArrowDown") {
63+
setSelectedIndex((prevIndex) => (prevIndex < suggestions.length - 1 ? prevIndex + 1 : prevIndex));
64+
} else if (e.key === "ArrowUp") {
65+
setSelectedIndex((prevIndex) => (prevIndex > 0 ? prevIndex - 1 : 0));
66+
} else if (e.key === "Escape") {
67+
setSuggestions([])
68+
} else if (e.key === "Enter" && selectedIndex >= 0) {
69+
e.preventDefault();
70+
completeSelection(suggestions[selectedIndex]);
71+
}
72+
};
73+
74+
const completeSelection = (suggestion) => {
75+
const parts = inputValue.split(".");
76+
const prefix = parts.slice(0, 1).join(".");
77+
let newValue = prefix ? `${prefix}.${suggestion}` : suggestion;
78+
79+
if (suggestion.startsWith(inputValue)) {
80+
newValue = suggestion;
81+
}
82+
83+
setInputValue(newValue);
84+
setSuggestions([]);
85+
setSelectedIndex(-1);
86+
};
87+
88+
const handleClickOutside = (evt) => {
89+
if (containerRef.current && !containerRef.current.contains(evt.target)) {
90+
setSuggestions([]);
91+
}
92+
};
93+
94+
useEffect(() => {
95+
document.addEventListener('mousedown', handleClickOutside);
96+
return () => {
97+
document.removeEventListener('mousedown', handleClickOutside);
98+
}
99+
}, []);
100+
101+
return (
102+
<DomainAutocompleteStyled ref={containerRef}>
103+
<input
104+
type="text"
105+
name={name}
106+
value={inputValue}
107+
onChange={handleChange}
108+
onKeyDown={handleKeyDown}
109+
placeholder="Digite um domínio personalizado"
110+
/>
111+
{suggestions.length > 0 && (
112+
<ul>
113+
{suggestions.map((suggestion, index) => (
114+
<li
115+
key={suggestion}
116+
onClick={() => completeSelection(suggestion)}
117+
className={`${index === selectedIndex ? "active" : ""}`}
118+
>
119+
{suggestion}
120+
</li>
121+
))}
122+
</ul>
123+
)}
124+
{helpText && <span className="help-text">{helpText}</span>}
125+
</DomainAutocompleteStyled>
126+
);
127+
};
128+
129+
export default DomainAutocomplete;

0 commit comments

Comments
 (0)