From 061137b83b56ebccd1b628ac3b0212f01c325531 Mon Sep 17 00:00:00 2001 From: DCoded <dicoded@email.com> Date: Wed, 26 Jan 2022 18:07:08 -0500 Subject: [PATCH] Added Select class --- js/ui/select.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 js/ui/select.js diff --git a/js/ui/select.js b/js/ui/select.js new file mode 100644 index 00000000000..7722f2a7221 --- /dev/null +++ b/js/ui/select.js @@ -0,0 +1,48 @@ +App.UI.Select = class Select { + /** + * Creates a new dropdown with available and unavailable options. + * @param {string[]} options A list of options to display. + * @param {string} [display] The default option to display when one is not selected. + */ + constructor(options, display) { + this.options = options; + this.display = display; + } + + /** + * Creates a list of all options, available and unavailable. + * @returns {HTMLOptionElement[]} + * @private + */ + _createOptions() { + const arr = []; + + this.options.forEach(option => { + arr.push(new Option(option)); + }); + + return arr; + } + + /** + * Renders the dropdown onscreen. + * @returns {HTMLSelectElement} + */ + render() { + const select = document.createElement("select"); + + const options = this._createOptions(); + + options.forEach(option => { + select.add(option); + + if (this.display) { + if (option.nodeValue === this.display) { + option.selected = true; + } + } + }); + + return select; + } +}; -- GitLab