Skip to content

Commit

Permalink
Prevent browser autocomplete, do not hide on field click
Browse files Browse the repository at this point in the history
  • Loading branch information
zbynek committed Jul 29, 2024
1 parent e6cc455 commit 3c67676
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
17 changes: 7 additions & 10 deletions war/src/main/js/components/autocomplete/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ function init() {
};
}

function createAndShowDropdown(e, div, suggestions) {
function createAndShowDropdown(e, suggestions) {
const items = suggestions
.splice(0, 10)
.map((s) => convertSuggestionToItem(s, e));
if (!e.dropdown) {
Utils.generateDropdown(
div,
e,
(instance) => {
e.dropdown = instance;
instance.popper.style.minWidth = e.offsetWidth + "px";
Expand All @@ -61,7 +61,7 @@ function init() {
e.dropdown.show();
}

function updateSuggestions(e, div) {
function updateSuggestions(e) {
const text = e.value.trim();
const delimiter = e.getAttribute("autoCompleteDelimChar");
const word = delimiter ? text.split(delimiter).reverse()[0].trim() : text;
Expand All @@ -75,9 +75,7 @@ function init() {
e.getAttribute("autoCompleteUrl") + "?value=" + encodeURIComponent(word);
fetch(url)
.then((rsp) => (rsp.ok ? rsp.json() : {}))
.then((response) =>
createAndShowDropdown(e, div, response.suggestions || []),
);
.then((response) => createAndShowDropdown(e, response.suggestions || []));
}

function debounce(callback) {
Expand All @@ -98,10 +96,9 @@ function init() {
"input-auto-complete",
0,
function (e) {
e.setAttribute("autocomplete", "off");
e.dataset["hideOnClick"] = "false";
// form field with auto-completion support
// insert the auto-completion container
var div = document.createElement("DIV");
e.parentNode.insertBefore(div, e.nextElementSibling);
e.style.position = "relative";
// otherwise menu won't hide on tab with nothing selected
// needs delay as without that it blocks click selection of an item
Expand All @@ -111,7 +108,7 @@ function init() {
e.addEventListener(
"input",
debounce(() => {
updateSuggestions(e, div);
updateSuggestions(e);
}),
);
},
Expand Down
1 change: 1 addition & 0 deletions war/src/main/js/components/dropdowns/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function generateDropdown(element, callback, immediate) {
tippy(
element,
Object.assign({}, Templates.dropdown(), {
hideOnClick: element.dataset["hideOnClick"] != "false",
onCreate(instance) {
const onload = () => {
if (instance.loaded) {
Expand Down

0 comments on commit 3c67676

Please sign in to comment.