mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-04 08:50:33 +08:00
* WIP * Fix type 1 * Missing types info * Some fixes * Several types refactoring and fixes * WIP * Fix grammar
63 lines
2.2 KiB
JavaScript
63 lines
2.2 KiB
JavaScript
import { html } from "lit"
|
|
import ITemplate from "../ITemplate.js"
|
|
import MouseIgnore from "../../input/mouse/MouseIgnore.js"
|
|
|
|
/** @extends {ITemplate<DropdownElement>} */
|
|
export default class DropdownTemplate extends ITemplate {
|
|
|
|
/** @type {HTMLSelectElement} */
|
|
#selectElement
|
|
|
|
/** @type {HTMLSelectElement} */
|
|
#referenceSelectElement
|
|
|
|
#changeHandler = e => this.element.selectedOption = /** @type {HTMLSelectElement} */(e.target)
|
|
.selectedOptions[0]
|
|
.value
|
|
|
|
render() {
|
|
return html`
|
|
<select class="ueb-pin-input-content" @change="${this.#changeHandler}">
|
|
${this.element.options.map(([k, v]) => html`
|
|
<option value="${k}" ?selected="${k === this.element.selectedOption}">${v}</option>
|
|
`)}
|
|
</select>
|
|
<select style="visibility: hidden; position: fixed;">
|
|
<option>${this.element.selectedOption}</option>
|
|
</select>
|
|
`
|
|
}
|
|
|
|
/** @param {PropertyValues} changedProperties */
|
|
firstUpdated(changedProperties) {
|
|
super.firstUpdated(changedProperties)
|
|
this.#selectElement = this.element.querySelector("select:first-child")
|
|
this.#referenceSelectElement = this.element.querySelector("select:last-child")
|
|
const event = new Event("input", { bubbles: true })
|
|
this.#selectElement.dispatchEvent(event)
|
|
}
|
|
|
|
/** @param {PropertyValues} changedProperties */
|
|
updated(changedProperties) {
|
|
super.updated(changedProperties)
|
|
const bounding = this.#referenceSelectElement.getBoundingClientRect()
|
|
this.element.style.setProperty("--ueb-dropdown-width", bounding.width + "px")
|
|
}
|
|
|
|
createInputObjects() {
|
|
return [
|
|
...super.createInputObjects(),
|
|
// Prevents creating links when selecting text and other undesired mouse actions detection
|
|
new MouseIgnore(this.element, this.blueprint),
|
|
]
|
|
}
|
|
|
|
setSelectedValue(value) {
|
|
/** @type {HTMLOptionElement} */(this.element.querySelector(`option[value="${value}"]`)).defaultSelected = true
|
|
}
|
|
|
|
getSelectedValue() {
|
|
return this.#selectElement.value
|
|
}
|
|
}
|