Files
clients/libs/components/src/select/select.component.ts
Matt Gibson 9c1e2ebd67 Typescript-strict-plugin (#12235)
* Use typescript-strict-plugin to iteratively turn on strict

* Add strict testing to pipeline

Can be executed locally through either `npm run test:types` for full type checking including spec files, or `npx tsc-strict` for only tsconfig.json included files.

* turn on strict for scripts directory

* Use plugin for all tsconfigs in monorepo

vscode is capable of executing tsc with plugins, but uses the most relevant tsconfig to do so. If the plugin is not a part of that config, it is skipped and developers get no feedback of strict compile time issues. These updates remedy that at the cost of slightly more complex removal of the plugin when the time comes.

* remove plugin from configs that extend one that already has it

* Update workspace settings to honor strict plugin

* Apply strict-plugin to native message test runner

* Update vscode workspace to use root tsc version

* `./node_modules/.bin/update-strict-comments` 🤖

This is a one-time operation. All future files should adhere to strict type checking.

* Add fixme to `ts-strict-ignore` comments

* `update-strict-comments` 🤖

repeated for new merge files
2024-12-09 20:58:50 +01:00

170 lines
4.7 KiB
TypeScript

// FIXME: Update this file to be type safe and remove this and next line
// @ts-strict-ignore
import {
Component,
ContentChildren,
HostBinding,
Input,
Optional,
QueryList,
Self,
ViewChild,
Output,
EventEmitter,
} from "@angular/core";
import { ControlValueAccessor, NgControl, Validators } from "@angular/forms";
import { NgSelectComponent } from "@ng-select/ng-select";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { BitFormFieldControl } from "../form-field";
import { Option } from "./option";
import { OptionComponent } from "./option.component";
let nextId = 0;
@Component({
selector: "bit-select",
templateUrl: "select.component.html",
providers: [{ provide: BitFormFieldControl, useExisting: SelectComponent }],
})
export class SelectComponent<T> implements BitFormFieldControl, ControlValueAccessor {
@ViewChild(NgSelectComponent) select: NgSelectComponent;
/** Optional: Options can be provided using an array input or using `bit-option` */
@Input() items: Option<T>[] = [];
@Input() placeholder = this.i18nService.t("selectPlaceholder");
@Output() closed = new EventEmitter();
protected selectedValue: T;
protected selectedOption: Option<T>;
protected searchInputId = `bit-select-search-input-${nextId++}`;
private notifyOnChange?: (value: T) => void;
private notifyOnTouched?: () => void;
constructor(
private i18nService: I18nService,
@Optional() @Self() private ngControl?: NgControl,
) {
if (ngControl != null) {
ngControl.valueAccessor = this;
}
}
@ContentChildren(OptionComponent)
protected set options(value: QueryList<OptionComponent<T>>) {
if (value == null || value.length == 0) {
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"];
// Usings a separate getter for the HostBinding to get around an unexplained angular error
@HostBinding("attr.disabled")
get disabledAttr() {
return this.disabled || null;
}
@Input()
get disabled() {
return this._disabled ?? this.ngControl?.disabled ?? false;
}
set disabled(value: any) {
this._disabled = value != null && value !== false;
}
private _disabled: boolean;
/**Implemented as part of NG_VALUE_ACCESSOR */
writeValue(obj: T): void {
this.selectedValue = obj;
this.selectedOption = this.findSelectedOption(this.items, this.selectedValue);
}
/**Implemented as part of NG_VALUE_ACCESSOR */
registerOnChange(fn: (value: T) => void): void {
this.notifyOnChange = fn;
}
/**Implemented as part of NG_VALUE_ACCESSOR */
registerOnTouched(fn: any): void {
this.notifyOnTouched = fn;
}
/**Implemented as part of NG_VALUE_ACCESSOR */
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
}
/**Implemented as part of NG_VALUE_ACCESSOR */
protected onChange(option: Option<T> | null) {
if (!this.notifyOnChange) {
return;
}
this.notifyOnChange(option?.value);
}
/**Implemented as part of NG_VALUE_ACCESSOR */
protected onBlur() {
if (!this.notifyOnTouched) {
return;
}
this.notifyOnTouched();
}
/**Implemented as part of BitFormFieldControl */
@HostBinding("attr.aria-describedby")
get ariaDescribedBy() {
return this._ariaDescribedBy;
}
set ariaDescribedBy(value: string) {
this._ariaDescribedBy = value;
this.select?.searchInput.nativeElement.setAttribute("aria-describedby", value);
}
private _ariaDescribedBy: string;
/**Implemented as part of BitFormFieldControl */
get labelForId() {
return this.searchInputId;
}
/**Implemented as part of BitFormFieldControl */
@HostBinding() @Input() id = `bit-multi-select-${nextId++}`;
/**Implemented as part of BitFormFieldControl */
@HostBinding("attr.required")
@Input()
get required() {
return this._required ?? this.ngControl?.control?.hasValidator(Validators.required) ?? false;
}
set required(value: any) {
this._required = value != null && value !== false;
}
private _required: boolean;
/**Implemented as part of BitFormFieldControl */
get hasError() {
return this.ngControl?.status === "INVALID" && this.ngControl?.touched;
}
/**Implemented as part of BitFormFieldControl */
get error(): [string, any] {
const key = Object.keys(this.ngControl?.errors)[0];
return [key, this.ngControl?.errors[key]];
}
private findSelectedOption(items: Option<T>[], value: T): Option<T> | undefined {
return items.find((item) => item.value === value);
}
/**Emits the closed event. */
protected onClose() {
this.closed.emit();
}
}