Skip to content

Commit

Permalink
fix(input): fix defaultValue when using select
Browse files Browse the repository at this point in the history
  • Loading branch information
clshortfuse committed Jul 15, 2024
1 parent 185c35d commit d3abfee
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions components/Input.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,15 @@ export default CustomElement
this.changeSuggestion({ value: this._listboxValue });
}
},
populateInputFromListbox() {
if (!this._isSelect) return;
if (!this._listbox) return;
this._listbox.value = this._value;
const [option] = this._listbox.selectedOptions;
if (option) {
this._input.value = option.label;
}
},
})
.childEvents({
control: {
Expand Down Expand Up @@ -508,6 +517,7 @@ export default CustomElement

listbox.addEventListener('change', this._onListboxChangeListener);
listbox.addEventListener('click', this._onListboxChangeListener);
this.populateInputFromListbox();
}
},
},
Expand Down Expand Up @@ -608,12 +618,7 @@ export default CustomElement
.overrides({
_onSetValue(value) {
if (this._isSelect) {
this._listbox.value = value;
const [option] = this._listbox.selectedOptions;
if (option) {
this._input.value = option.label;
}
this._value = value;
this.populateInputFromListbox();
} else {
// Apply user value to input and read back result to apply control to parse
this._input.value = value;
Expand All @@ -623,7 +628,9 @@ export default CustomElement
})
.on({
_valueChanged(previous, current) {
if (!this.multiple) {
if (this._isSelect) {
this.populateInputFromListbox();
} else if (!this.multiple) {
this._listboxValue = current;
}
},
Expand All @@ -640,6 +647,7 @@ export default CustomElement
this._onListboxChangeListener = this.onListboxChange.bind(this);
this._onListboxClickListener = this.onListboxClick.bind(this);
this._onPopupFocusoutListener = this.onPopupFocusout.bind(this);
document.addEventListener('DOMContentLoaded', () => this.populateInputFromListbox(), { once: true });
},
})
.html`
Expand Down

0 comments on commit d3abfee

Please sign in to comment.