Compare commits

..

No commits in common. "6c3db0456e41919e00b7fed71a9fb99985989a1d" and "35490ff0e304612b3ef2a886b56dfe9c78e04e0c" have entirely different histories.

2 changed files with 11 additions and 55 deletions

View file

@ -3,6 +3,6 @@
"browser": "src/BetterSelect.js", "browser": "src/BetterSelect.js",
"type": "module", "type": "module",
"license": "MIT", "license": "MIT",
"version": "1.5.0", "version": "1.4.2",
"url": "https://darkwiiplayer.github.io/BetterSelect/" "url": "https://darkwiiplayer.github.io/BetterSelect/"
} }

View file

@ -64,7 +64,7 @@ export class BetterSelect extends HTMLElement {
#internals = this.attachInternals() #internals = this.attachInternals()
static formAssociated = true static formAssociated = true
static observedAttributes = Object.freeze(["placeholder", "search-placeholder", "required"]) static observedAttributes = Object.freeze(["placeholder", "search-placeholder"])
static styleSheet = css` static styleSheet = css`
:host { :host {
@ -235,8 +235,6 @@ export class BetterSelect extends HTMLElement {
event.stopPropagation() event.stopPropagation()
} }
}) })
this.setValidity()
} }
/** /**
@ -367,7 +365,7 @@ export class BetterSelect extends HTMLElement {
/** @param {HTMLElement} option */ /** @param {HTMLElement} option */
setOption(option) { setOption(option) {
return this.setValue(option.dataset.value, option.innerText) this.setValue(option.dataset.value, option.innerText)
} }
/** /**
@ -379,20 +377,14 @@ export class BetterSelect extends HTMLElement {
this.dispatchEvent(new Event("change", {bubbles: true})); this.dispatchEvent(new Event("change", {bubbles: true}));
this.#internals.setFormValue(value, state) this.#internals.setFormValue(value, state)
this.text.innerText = state this.text.innerText = state
this.setValidity()
} }
get value() { return this.#value.value } get value() { return this.#value.value }
set value(value) { set value(value) {
if (value === undefined) { for (const option of this.options) {
this.clear() if (option.value === String(value)) {
} else { this.setValue(option.value, option.innerText)
for (const option of Array.from(this.options)) { return
if (option.value === String(value)) {
this.setValue(option.value, option.innerText)
return
}
} }
} }
throw `No option with value ${value}` throw `No option with value ${value}`
@ -404,7 +396,7 @@ export class BetterSelect extends HTMLElement {
this.list.replaceChildren() this.list.replaceChildren()
for (const option of this.options) { for (const option of this.options) {
this.list.append(f`<li tabindex="0" part="item" data-value="${option.value}">${option.innerText}</li>`) this.list.append(f`<li tabindex="0" part="item" data-value="${option.value}">${option.innerText}</li>`)
if (this.value == undefined && option.selected) { if (option.selected) {
this.value = option.value this.value = option.value
} }
} }
@ -434,51 +426,15 @@ export class BetterSelect extends HTMLElement {
*/ */
set name(name) { this.setAttribute("name", String(name)) } set name(name) { this.setAttribute("name", String(name)) }
/**
* @return {HTMLFormElement}
*/
get form() { return this.#internals.form } get form() { return this.#internals.form }
clear() { clear() {
this.setValue(undefined, "") this.setValue(undefined, "")
} }
/**
* @param {Boolean} disabled
*/
set disabled(disabled) { this.toggleAttribute("disabled", disabled) }
get disabled() { return this.hasAttribute("disabled") }
setValidity() {
if (this.required) {
const valid = Boolean(this.value)
if (valid) {
this.#internals.setValidity({})
} else {
this.#internals.setValidity({valueMissing: true}, "Please select an option.")
}
return valid
} else {
this.#internals.setValidity({})
return true
}
}
checkValidity() { return this.#internals.checkValidity() }
get validity() { return this.#internals.validity }
get validationMessage() { return this.#internals.validationMessage }
get willValidate() { return this.#internals.willValidate }
/**
* @param {Boolean} required
*/
set required(required) { this.toggleAttribute("required", required) }
get required() { return this.hasAttribute("required") }
reportValidity() { return this.#internals.reportValidity() }
requiredChanged() { this.setValidity() }
/** /**
* @param {String} name * @param {String} name
* @param {String} before * @param {String} before