1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 08:43:33 +00:00

[PM-21121] fix(select): update selected item when items input changes (#14609)

This commit is contained in:
Will Martin
2025-05-05 13:25:28 -04:00
committed by GitHub
parent 013a34e042
commit c61bb3dc70
2 changed files with 67 additions and 4 deletions

View File

@@ -41,13 +41,25 @@ let nextId = 0;
export class SelectComponent<T> implements BitFormFieldControl, ControlValueAccessor {
@ViewChild(NgSelectComponent) select: NgSelectComponent;
private _items: Option<T>[] = [];
/** Optional: Options can be provided using an array input or using `bit-option` */
@Input() items: Option<T>[] = [];
@Input()
get items(): Option<T>[] {
return this._items;
}
set items(next: Option<T>[]) {
this._items = next;
this._selectedOption = this.findSelectedOption(next, this.selectedValue);
}
@Input() placeholder = this.i18nService.t("selectPlaceholder");
@Output() closed = new EventEmitter();
protected selectedValue: T;
protected selectedOption: Option<T>;
protected _selectedOption: Option<T>;
get selectedOption() {
return this._selectedOption;
}
protected searchInputId = `bit-select-search-input-${nextId++}`;
private notifyOnChange?: (value: T) => void;
@@ -68,7 +80,6 @@ export class SelectComponent<T> implements BitFormFieldControl, ControlValueAcce
return;
}
this.items = value.toArray();
this.selectedOption = this.findSelectedOption(this.items, this.selectedValue);
}
@HostBinding("class") protected classes = ["tw-block", "tw-w-full", "tw-h-full"];
@@ -90,7 +101,7 @@ export class SelectComponent<T> implements BitFormFieldControl, ControlValueAcce
/**Implemented as part of NG_VALUE_ACCESSOR */
writeValue(obj: T): void {
this.selectedValue = obj;
this.selectedOption = this.findSelectedOption(this.items, this.selectedValue);
this._selectedOption = this.findSelectedOption(this.items, this.selectedValue);
}
/**Implemented as part of NG_VALUE_ACCESSOR */