Skip to content

Commit eb328ad

Browse files
JesmoDeviOvergaard
authored andcommitted
add support for async options
1 parent 84424e7 commit eb328ad

File tree

3 files changed

+24
-10
lines changed

3 files changed

+24
-10
lines changed

packages/uui-combobox-list/lib/UUIComboboxListEvent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { UUIComboboxListElement } from './uui-combobox-list.element';
33

44
export class UUIComboboxListEvent extends UUIEvent<UUIComboboxListElement> {
55
public static readonly CHANGE: string = 'change';
6+
public static readonly SLOT_CHANGE: string = 'slot-change';
67

78
constructor(evName: string, eventInit: any | null = {}) {
89
super(evName, {

packages/uui-combobox-list/lib/uui-combobox-list.element.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ export class UUIComboboxListElement extends LitElement {
2525

2626
/**
2727
* Value of selected option.
28-
* @type { string }
28+
* @type { FormDataEntryValue | FormData }
2929
* @attr
3030
* @default ""
3131
*/
32-
@property({ type: String })
32+
@property()
3333
public get value() {
3434
return this._value;
3535
}
@@ -87,7 +87,7 @@ export class UUIComboboxListElement extends LitElement {
8787
private _activeOptions!: UUIComboboxListOptionElement[];
8888

8989
@state()
90-
private _value: FormDataEntryValue = '';
90+
private _value: FormDataEntryValue | FormData = '';
9191

9292
private __activeElement: UUIComboboxListOptionElement | undefined;
9393
private get _activeElement(): UUIComboboxListOptionElement | undefined {
@@ -150,6 +150,9 @@ export class UUIComboboxListElement extends LitElement {
150150
}
151151

152152
this._updateSelection();
153+
this.dispatchEvent(
154+
new UUIComboboxListEvent(UUIComboboxListEvent.SLOT_CHANGE)
155+
);
153156
};
154157

155158
private _onSelected = (e: Event) => {

packages/uui-combobox/lib/uui-combobox.element.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export class UUIComboboxElement extends FormControlMixin(LitElement) {
9595
`,
9696
];
9797

98+
@property({ attribute: 'value', reflect: true })
9899
get value() {
99100
return this._value;
100101
}
@@ -198,22 +199,26 @@ export class UUIComboboxElement extends FormControlMixin(LitElement) {
198199
UUIComboboxListEvent.CHANGE,
199200
this._onChange
200201
);
202+
this._comboboxList.addEventListener(
203+
UUIComboboxListEvent.SLOT_CHANGE,
204+
this._onSlotChange
205+
);
201206

202-
if (typeof this.value === 'string') {
203-
await this.updateComplete;
204-
this._updateValue(this.value);
205-
}
207+
await this.updateComplete;
208+
this._updateValue(this.value);
206209
}
207210
}
208211

209212
private _onPhoneChange = () => {
210213
this._isPhone = this.phoneMediaQuery.matches;
211214
};
212215

213-
private _updateValue(value: string) {
216+
private _updateValue(value: FormDataEntryValue | FormData) {
214217
if (this._comboboxList) {
215218
this._comboboxList.value = value;
216-
this._displayValue = this._comboboxList?.displayValue || '';
219+
requestAnimationFrame(
220+
() => (this._displayValue = this._comboboxList.displayValue || '')
221+
);
217222
}
218223
}
219224

@@ -238,14 +243,19 @@ export class UUIComboboxElement extends FormControlMixin(LitElement) {
238243
this._open();
239244
};
240245

246+
private _onSlotChange = () => {
247+
if (this.value && this.value !== this._comboboxList?.value) {
248+
this._updateValue(this.value);
249+
}
250+
};
251+
241252
private _onChange = (e: Event) => {
242253
e.stopImmediatePropagation();
243254

244255
this.value = this._comboboxList?.value || '';
245256
this.search = this.value ? this.search : '';
246257

247258
this._onClose();
248-
249259
this.dispatchEvent(new UUIComboboxEvent(UUIComboboxEvent.CHANGE));
250260
};
251261

0 commit comments

Comments
 (0)