From b52d86b0e266af524df8a9f704ba3c7d81c64be5 Mon Sep 17 00:00:00 2001 From: Gerome Matilla Date: Sat, 6 Jun 2020 10:11:33 +0800 Subject: [PATCH] cleanup --- css/suggestions.css | 11 ++++++++--- js/auto-suggest.js | 46 +++++++++++++++++++++++++++++++-------------- 2 files changed, 40 insertions(+), 17 deletions(-) diff --git a/css/suggestions.css b/css/suggestions.css index b8c2001..f2085e5 100644 --- a/css/suggestions.css +++ b/css/suggestions.css @@ -8,6 +8,9 @@ display: table; margin: 0 auto; + opacity: 0; + transition: opacity var(--transition-speed); + /*Disable user touch/select on text elements*/ -webkit-touch-callout: none; -webkit-user-select: none; @@ -17,6 +20,10 @@ user-select: none; } +.suggestionsShow { + opacity: 1 !important; +} + /*The UL*/ #suggestions { @@ -39,15 +46,13 @@ /*The Li*/ #phrase { /*Align list horizontally*/ - display: inline-block; justify-content: center; - width: auto; height: auto; /*Always center horizontally*/ display: table-cell; - width: auto; + width: auto; text-align: center; } diff --git a/js/auto-suggest.js b/js/auto-suggest.js index 8498d52..972fe3a 100644 --- a/js/auto-suggest.js +++ b/js/auto-suggest.js @@ -1,22 +1,37 @@ var searchBox = document.getElementById('searchBox'); var suggestionsUL = document.getElementById('suggestions'); +var suggestionsContainer = document.getElementById('suggestionsContainer'); + +// Update searchbox on enter key +const phraseButtonCallback = button => { + + button.onkeyup = event => { + + if (event.key === 'Enter') { + + searchBox.value = button.innerText; + searchBox.focus(); + + } + + } + +} + +// Generate and parse suggestions const autocompleteCallback = phrase => { - var suggestion = phrase.map(i => i.phrase) .filter(s => !(s.toLowerCase() === String(searchBox.value).toLowerCase())) .slice(0, 4); - - // Empty ul on every callback + // Empty ul on every callback to refresh list suggestionsUL.innerHTML = ''; - // console.log(suggestion); - for (i=0; i<(suggestion.length); i++) { - - // console.log(suggestion[i]); + // Generate list elements + for (i = 0; i < (suggestion.length); i++) { var li = document.createElement('li'); li.id = 'phrase'; @@ -24,25 +39,28 @@ const autocompleteCallback = phrase => { var button = document.createElement('button'); button.type = 'button'; button.className = 'phraseButton'; - - button.innerHTML = suggestion[i]; + phraseButtonCallback(button); li.appendChild(button); suggestionsUL.appendChild(li); - - } - - + // Show suggestions + suggestionsContainer.classList.add('suggestionsShow'); } +// Update every keyup +searchBox.onkeyup = event => { -searchBox.onkeydown = () => { + if (event.key === 'Tab') return; if (searchBox.value < 1) { + + // Hide suggestions + suggestionsContainer.classList.remove('suggestionsShow'); + return; }