Replaced the selectbox with native HTML <select>

This commit is contained in:
rcmainak 2018-03-13 18:43:07 +05:30 committed by Danny Coates
parent 0ed5c7f1e7
commit 77e3b5a3e6
No known key found for this signature in database
GPG Key ID: 4C442633C62E00CB
2 changed files with 35 additions and 71 deletions

View File

@ -1,59 +1,32 @@
const html = require('choo/html'); const html = require('choo/html');
const number = require('../../utils').number;
module.exports = function(selected, options, translate, changed) { module.exports = function(selected, options, translate, changed) {
const id = `select-${Math.random()}`; const id = `select-${Math.random()}`;
let x = selected; let x = selected;
return html` return html`
<div class="selectbox"> <span class="select">
<div onclick=${toggle}> <select id="${id}" onchange=${choose}>
<span class="link">${translate(selected)}</span>
<svg width="32" height="32">
<polygon points="8 18 17 28 26 18" fill="#0094fb"/>
</svg>
</div>
<ul id="${id}" class="selectbox__options">
${options.map( ${options.map(
i => html` i =>
<li html`<option value="${i}" ${
class="selectbox__option" i === selected ? 'selected' : ''
onclick=${choose} }>${translate(i)}</option>`
data-value="${i}">${number(i)}</li>`
)} )}
</ul> </select>
</div>`;
<svg id="arrow" width="32" height="32">
function close() { <polygon points="8 18 17 28 26 18" fill="#0094fb"/>
const ul = document.getElementById(id); </svg>
const body = document.querySelector('body'); </span>`;
ul.classList.remove('selectbox__options--active');
body.removeEventListener('click', close);
}
function toggle(event) {
event.stopPropagation();
const ul = document.getElementById(id);
if (ul.classList.contains('selectbox__options--active')) {
close();
} else {
ul.classList.add('selectbox__options--active');
const body = document.querySelector('body');
body.addEventListener('click', close);
}
}
function choose(event) { function choose(event) {
event.stopPropagation();
const target = event.target; const target = event.target;
const value = +target.dataset.value; const value = +target.value;
target.parentNode.previousSibling.firstElementChild.textContent = translate(
value
);
if (x !== value) { if (x !== value) {
x = value; x = value;
changed(value); changed(value);
} }
close();
} }
}; };

View File

@ -1,36 +1,27 @@
.selectbox { .select {
display: inline-block;
position: relative;
cursor: pointer;
}
.selectbox__options {
display: none;
}
.selectbox__options--active {
display: block;
position: absolute;
top: 0;
left: 0;
padding: 0;
margin: 40px 0;
background-color: var(--pageBGColor); background-color: var(--pageBGColor);
border: 1px solid rgba(12, 12, 13, 0.3); overflow: hidden;
border-radius: 4px;
box-shadow: 1px 2px 4px rgba(12, 12, 13, 0.3);
} }
.selectbox__option { select {
color: var(--lightTextColor); appearance: none;
font-size: 12pt; outline: 0;
list-style: none; box-shadow: none;
user-select: none; border: 0;
white-space: nowrap; background: #fff;
padding: 0 60px; background-image: none;
border-bottom: 1px solid rgba(12, 12, 13, 0.3); font-size: 1em;
margin: 0;
color: #0094fb;
cursor: pointer;
border-color: none;
} }
.selectbox__option:hover { select:active {
background-color: #f4f4f4; background-color: var(--pageBGColor);
border: 0;
}
#arrow {
position: relative;
} }