-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (48 loc) · 1.63 KB
/
index.js
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
const niceSelect = ({ withoutwide = undefined } = {}) => {
const select = document.querySelectorAll("select");
select.forEach((element) => {
let option = element.querySelectorAll("option");
function insertAfter(referenceNode, newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
element.style.display = "none";
var el = document.createElement("div");
insertAfter(element, el);
el.className = withoutwide ? "nice-select" : "nice-select wide";
let ul = document.createElement("ul"),
span = document.createElement("span");
ul.classList.add("list");
span.classList.add("current");
el.appendChild(ul);
el.appendChild(span);
el.addEventListener("click", () => {
el.classList.toggle("open");
});
document.addEventListener("click", (e) => {
if (!el.contains(e.target)) {
el.classList.remove("open");
}
});
option.forEach((opt) => {
var li = document.createElement("li");
ul.appendChild(li);
li.innerHTML = opt.innerText;
li.className = "option";
let defultValue = opt.getAttribute("data-dsplay");
let selected = opt.getAttribute("selected");
let disabled = opt.getAttribute("disabled");
if (defultValue || selected !== null || disabled !== null) {
li.className = "option focus";
span.innerText = opt.innerText;
} else {
span.innerText = option[0].innerText;
}
li.addEventListener("click", () => {
if (disabled == null) {
span.innerHTML = opt.innerText;
}
});
});
});
};
module.exports = niceSelect;