Tabindex в пользовательском стиле выбора - PullRequest
0 голосов
/ 11 ноября 2019

Я работаю над расширением Google Chrome. Пока что я не планирую делать это для других браузеров (пока), поэтому совместимость браузера не является проблемой. У меня много стилей CSS, идущих на расширение, и я решил следовать великолепному руководству W3 Schools на пользовательских полях выбора - https://www.w3schools.com/howto/howto_custom_select.asp. Пока все хорошо! Тем не менее, это для расширения калькулятора, и мне действительно нужно, чтобы пользователи могли переключаться между разными полями ввода с помощью клавиатуры.

Я не уверен, как заставить это работать?

Это просто вопрос, который я пропустил в отношении tabindex, ИЛИ мне нужно добавить код Javascript с прослушивателями событий и константами для ключей?

Пока что я настроилдизайн для элемента и добавил "tabindex" в раскрывающемся списке. Поле выбора «выбирается» с помощью вкладки, но вы ничего не можете выбрать с помощью клавиатуры. Посмотрите здесь на мой кодовый блок: https://codepen.io/lars-ejaas/pen/KKKBmNp

<DIV id=settings>
    <H1>Settings</H1>
    <p>Decimal separator</p><div class="custom-select" style="width:88px; margin-left: 10px; display: inline-flex">       
        <select id="seperator" tabindex="-1">
            <option value="SELECT">POINT</option>
            <option value="COMMA">COMMA</option>
            <option value="POINT">POINT</option>
        </select>
    </div>
    <br>Enable mousescroll in input fields <input type="checkbox" id="scroll"> 
</DIV>

<STYLE>
/* Remove blue ring around focused checkbox */

input:focus {
  border-color: #333333 !important;  
  outline-color: #333333 !important;
}

/* Remove blue ring around focused url's */
a:focus {
  outline-color: #333333 !important;
}
.custom-select {
  position: relative;
  font-family: Oswald;

}

.custom-select select {
  display: none; /*hide original SELECT element:*/
}

.select-selected {
  background-image: -webkit-repeating-linear-gradient(left, hsla(0,0%,100%,0) 0%, hsla(0,0%,100%,0) 6%, hsla(0,0%,100%, 0.1) 7.5%), -webkit-repeating-linear-gradient(left, hsla(0,0%, 0%,0) 0%, hsla(0,0%, 0%,0) 4%, hsla(0,0%, 0%,0.03) 4.5%), -webkit-repeating-linear-gradient(left, hsla(0,0%,100%,0) 0%, hsla(0,0%,100%,0) 1.2%, hsla(0,0%,100%,0.15) 2.2%), linear-gradient(180deg, hsl(0,0%,78%) 0%, hsl(0,0%,82%) 78%, hsl(0,0%,78%) 53%, hsl(0,0%,70%)100%);
  box-shadow: rgba(38, 38, 38, 0.1) 0px 0px 0px 3px inset, rgba(38, 38, 38, 0.6) 0px -1px 5px 4px inset, rgba(0, 0, 0, 0.25) 0px -1px 0px 7px inset, rgba(255, 255, 255, 0.7) 0px 2px 1px 7px inset;
  height: 26px; 
  width: 46px;
  border-radius: 20px;
  padding: 4px 19px 4px 19px;
  border: 1px;
  color: #333;

}

.select-selected:focus {
  outline: none;
  border-width: 3px;
  height: 26px; 
  border-style: solid;
  border-color: rgb(102, 102, 102);
  border-image: initial;
  padding: 1px 16px;

} 

/*style the arrow inside the select element:*/
.select-selected:after {
  position: absolute;
  content: "";
  top: 14px;
  right: 11px;
  width: 0;
  height: 0;
  border: 6px solid transparent;
  border-color: #333 transparent transparent transparent;
}

/*point the arrow upwards when the select box is open (active):*/
.select-selected.select-arrow-active:after {
  border-color: transparent transparent #333 transparent;
  top: 7px;
  right: 11px;
}

/*style the items (options), including the selected item:*/
.select-items div {
  color: #333;
  padding: 6px 16px;
  cursor: pointer;
  /*user-select: none;*/

}

/*style items (options):*/
.select-items {
  position: absolute;
  background-color: white;
  box-shadow: rgba(0, 0, 0, 0.8) 2px 2px 8px 0px inset;
  top: 100%;
  z-index: 99;
  border-radius: 20px;
  border-style: outset;
  border-width: 3px;
  outline: none;
}

/*hide the items when the select box is closed:*/
.select-hide {
  display: none;
}

.select-items div:hover, .same-as-selected {
  background-color: rgba(0, 0, 0, 0.1);
  border-radius: 10px;
}

.select-items:focus, .select-items:active {
  outline: none;
}

</STYLE>


var x, i, j, selElmnt, a, b, c;
/*look for any elements with the class "custom-select":*/
x = document.getElementsByClassName("custom-select");
for (i = 0; i < x.length; i++) {
  selElmnt = x[i].getElementsByTagName("select")[0];
  /*for each element, create a new DIV that will act as the selected item:*/
  a = document.createElement("DIV");
  a.setAttribute("class", "select-selected");
  a.setAttribute("tabindex", "0");
  a.innerHTML = selElmnt.options[selElmnt.selectedIndex].innerHTML;
  x[i].appendChild(a);
  /*for each element, create a new DIV that will contain the option list:*/
  b = document.createElement("DIV");
  b.setAttribute("class", "select-items select-hide");
  for (j = 1; j < selElmnt.length; j++) {
    /*for each option in the original select element,
    create a new DIV that will act as an option item:*/
    c = document.createElement("DIV");
    c.setAttribute("tabindex", "0");
    c.innerHTML = selElmnt.options[j].innerHTML;
    c.addEventListener("click", function(e) {
        /*when an item is clicked, update the original select box,
        and the selected item:*/
        var y, i, k, s, h;
        s = this.parentNode.parentNode.getElementsByTagName("select")[0];
        h = this.parentNode.previousSibling;
        for (i = 0; i < s.length; i++) {
          if (s.options[i].innerHTML == this.innerHTML) {
            s.selectedIndex = i;
            h.innerHTML = this.innerHTML;
            y = this.parentNode.getElementsByClassName("same-as-selected");
            for (k = 0; k < y.length; k++) {
              y[k].removeAttribute("class");
            }
            this.setAttribute("class", "same-as-selected");
            break;
          }
        }
        h.click();
    });
    b.appendChild(c);
  }
  x[i].appendChild(b);
  a.addEventListener("click", function(e) {
      /*when the select box is clicked, close any other select boxes,
      and open/close the current select box:*/
      e.stopPropagation();
      closeAllSelect(this);
      this.nextSibling.classList.toggle("select-hide");
      this.classList.toggle("select-arrow-active");
    });
}
function closeAllSelect(elmnt) {
  /*a function that will close all select boxes in the document,
  except the current select box:*/
  var x, y, i, arrNo = [];
  x = document.getElementsByClassName("select-items");
  y = document.getElementsByClassName("select-selected");
  for (i = 0; i < y.length; i++) {
    if (elmnt == y[i]) {
      arrNo.push(i)
    } else {
      y[i].classList.remove("select-arrow-active");
    }
  }
  for (i = 0; i < x.length; i++) {
    if (arrNo.indexOf(i)) 
      x[i].classList.add("select-hide");
    }
  }
}

/*if the user clicks anywhere outside the select box,
then close all select boxes:*/
document.addEventListener("click", closeAllSelect);

Мне нужно, чтобы у пользователя была возможность вкладки в поле выбора - откройте раскрывающийся список с помощью клавиши пробела, выберите нужный вариант с помощью клавиш со стрелками ивыберите его с помощью «enter» - как в обычном окне выбора. Прямо сейчас вы можете только «вкладывать» в поле выбора.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...