Skip to content

Commit 6b1ee6f

Browse files
authored
fix: various issues of combobox and related (#276)
* use path as target, since target can be the same for multiple elements now that we are using bubbling events * set values correctly * use composedPath()
1 parent d7cecac commit 6b1ee6f

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

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

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ export class UUIComboboxElement extends FormControlMixin(LitElement) {
8383
return this._value;
8484
}
8585
set value(newValue) {
86-
super.value = newValue;
87-
8886
if (typeof newValue === 'string') {
8987
this._updateValue(newValue);
9088
}
89+
90+
super.value = newValue;
9191
}
9292

9393
/**
@@ -185,7 +185,7 @@ export class UUIComboboxElement extends FormControlMixin(LitElement) {
185185
private _onBlur = () =>
186186
requestAnimationFrame(() => {
187187
if (document.activeElement !== this) {
188-
this._close();
188+
this._onClose();
189189
}
190190
});
191191

@@ -201,10 +201,9 @@ export class UUIComboboxElement extends FormControlMixin(LitElement) {
201201
e.stopImmediatePropagation();
202202

203203
this.value = this._comboboxList?.value || '';
204-
this._displayValue = this._comboboxList?.displayValue || '';
205204
this.search = this.value ? this.search : '';
206205

207-
this._close();
206+
this._onClose();
208207

209208
this.dispatchEvent(new UUIComboboxEvent(UUIComboboxEvent.CHANGE));
210209
};
@@ -214,16 +213,14 @@ export class UUIComboboxElement extends FormControlMixin(LitElement) {
214213
this.open = true;
215214
};
216215

217-
private _close = () => {
216+
private _onClose = () => {
218217
if (!this.open) return;
219218

220219
this.open = false;
221-
222-
// Reset display and input value:
223-
this._displayValue = this._comboboxList?.displayValue || '';
220+
this.search = '';
221+
// Reset input(search-input) value:
224222
this._input.value = this._displayValue;
225223

226-
this.search = '';
227224
this.dispatchEvent(new UUIComboboxEvent(UUIComboboxEvent.SEARCH));
228225
};
229226

@@ -238,21 +235,22 @@ export class UUIComboboxElement extends FormControlMixin(LitElement) {
238235
}
239236

240237
if (e.key === 'Escape' || e.key === 'Enter') {
241-
this._close();
238+
this._onClose();
242239
}
243240
};
244241

245-
private _clear = (e: any) => {
242+
private _onClear = (e: any) => {
246243
if (e.key && e.key !== 'Enter') return;
247244

248245
e.preventDefault(); // TODO: could we avoid this.
249246
e.stopImmediatePropagation(); // TODO: could we avoid this.
250247

251-
this._displayValue = '';
252-
this._input.value = this._displayValue;
253-
this.search = '';
254248
this.value = '';
255-
this._comboboxList.value = this.value;
249+
this.search = '';
250+
// Reset input(search-input) value:
251+
this._input.value = this._displayValue;
252+
253+
this.dispatchEvent(new UUIComboboxEvent(UUIComboboxEvent.SEARCH));
256254
this.dispatchEvent(new UUIComboboxEvent(UUIComboboxEvent.CHANGE));
257255
};
258256

@@ -263,7 +261,6 @@ export class UUIComboboxElement extends FormControlMixin(LitElement) {
263261
label="combobox-input"
264262
type="text"
265263
.value=${this._displayValue}
266-
.placeholder=${this._displayValue}
267264
autocomplete="off"
268265
@click=${this._open}
269266
@input=${this._onInput}
@@ -284,8 +281,8 @@ export class UUIComboboxElement extends FormControlMixin(LitElement) {
284281
return html`<uui-button
285282
id="clear-button"
286283
class=${this.value || this.search ? '--show' : ''}
287-
@click=${this._clear}
288-
@keydown=${this._clear}
284+
@click=${this._onClear}
285+
@keydown=${this._onClear}
289286
label="clear"
290287
slot="append"
291288
compact
@@ -307,7 +304,7 @@ export class UUIComboboxElement extends FormControlMixin(LitElement) {
307304
<uui-popover
308305
.open=${this.open}
309306
.margin=${-1}
310-
@close=${() => this._close()}>
307+
@close=${() => this._onClose()}>
311308
${this._renderInput()} ${this._renderDropdown()}
312309
</uui-popover>
313310
`;

packages/uui-form-validation-message/lib/uui-form-validation-message.element.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class UUIFormValidationMessageElement extends LitElement {
7878
private _messages = new Map<FormControlMixinInterface, string>();
7979

8080
private _onControlInvalid = (e: UUIFormControlEvent) => {
81-
const ctrl = e.target;
81+
const ctrl = (e as any).composedPath()[0];
8282
if (ctrl.pristine === false) {
8383
// Currently we only show message from components who does have the pristine property. (we only want to show messages from fields that are NOT pristine aka. that are dirty or in a from that has been submitted)
8484
this._messages.set(ctrl, ctrl.validationMessage);
@@ -89,7 +89,8 @@ export class UUIFormValidationMessageElement extends LitElement {
8989
};
9090

9191
private _onControlValid = (e: UUIFormControlEvent) => {
92-
this._messages.delete(e.target);
92+
const ctrl = (e as any).composedPath()[0];
93+
this._messages.delete(ctrl);
9394
this.requestUpdate();
9495
};
9596

0 commit comments

Comments
 (0)