-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use standard dropdowns for combobox (#9462)
Co-authored-by: Zbynek Konecny <[email protected]>
- Loading branch information
Showing
6 changed files
with
132 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import behaviorShim from "@/util/behavior-shim"; | ||
import Utils from "@/components/dropdowns/utils"; | ||
|
||
function init() { | ||
function convertSuggestionToItem(suggestion, e) { | ||
const confirm = () => { | ||
e.value = suggestion.name; | ||
Utils.validateDropdown(e); | ||
e.focus(); | ||
}; | ||
return { | ||
label: suggestion.name, | ||
onClick: confirm, | ||
onKeyPress: (evt) => { | ||
if (evt.key === "Tab") { | ||
confirm(); | ||
e.dropdown.hide(); | ||
evt.preventDefault(); | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
function createAndShowDropdown(e, div, suggestions) { | ||
const items = suggestions | ||
.splice(0, Utils.getMaxSuggestionCount(e, 20)) | ||
.map((s) => convertSuggestionToItem(s, e)); | ||
if (!e.dropdown) { | ||
Utils.generateDropdown( | ||
div, | ||
(instance) => { | ||
e.dropdown = instance; | ||
}, | ||
true, | ||
); | ||
} | ||
e.dropdown.setContent(Utils.generateDropdownItems(items, true)); | ||
e.dropdown.show(); | ||
} | ||
|
||
function updateSuggestions(e, div, items) { | ||
const text = e.value.trim(); | ||
|
||
let filteredItems = text | ||
? items.filter((item) => item.indexOf(text) === 0) | ||
: items; | ||
|
||
const suggestions = filteredItems | ||
.filter((item) => item.indexOf(text) === 0) | ||
.map((item) => { | ||
return { name: item }; | ||
}); | ||
createAndShowDropdown(e, div, suggestions || []); | ||
} | ||
|
||
behaviorShim.specify("INPUT.combobox2", "combobox", 100, function (e) { | ||
// form field with auto-completion support | ||
// insert the auto-completion container | ||
refillOnChange(e, function (params) { | ||
const div = document.createElement("DIV"); | ||
e.parentNode.insertBefore(div, e.nextElementSibling); | ||
e.style.position = "relative"; | ||
|
||
const url = e.getAttribute("fillUrl"); | ||
fetch(url, { | ||
headers: crumb.wrap({ | ||
"Content-Type": "application/x-www-form-urlencoded", | ||
}), | ||
method: "post", | ||
body: new URLSearchParams(params), | ||
}) | ||
.then((rsp) => (rsp.ok ? rsp.json() : {})) | ||
.then((items) => { | ||
e.addEventListener("focus", () => updateSuggestions(e, div, items)); | ||
|
||
// otherwise menu won't hide on tab with nothing selected | ||
// needs delay as without that it blocks click selection of an item | ||
e.addEventListener("focusout", () => | ||
setTimeout(() => e.dropdown.hide(), 200), | ||
); | ||
|
||
e.addEventListener( | ||
"input", | ||
Utils.debounce(() => { | ||
updateSuggestions(e, div, items); | ||
}), | ||
); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
export default { init }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters