1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-27 05:33:59 +00:00
Files
browser/libs/components/src/input/input.directive.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

146 lines
3.8 KiB
TypeScript

// FIXME: Update this file to be type safe and remove this and next line
// @ts-strict-ignore
import {
Directive,
ElementRef,
HostBinding,
HostListener,
Input,
NgZone,
Optional,
Self,
} from "@angular/core";
import { NgControl, Validators } from "@angular/forms";
import { BitFormFieldControl, InputTypes } from "../form-field/form-field-control";
import { BitFormFieldComponent } from "../form-field/form-field.component";
// Increments for each instance of this component
let nextId = 0;
export function inputBorderClasses(error: boolean) {
return [
"tw-border",
"!tw-border-solid",
error ? "tw-border-danger-600" : "tw-border-secondary-500",
"focus:tw-outline-none",
];
}
@Directive({
selector: "input[bitInput], select[bitInput], textarea[bitInput]",
providers: [{ provide: BitFormFieldControl, useExisting: BitInputDirective }],
})
export class BitInputDirective implements BitFormFieldControl {
@HostBinding("class") @Input() get classList() {
const classes = [
"tw-block",
"tw-w-full",
"tw-h-full",
"tw-text-main",
"tw-placeholder-text-muted",
"tw-bg-background",
"tw-border-none",
"focus:tw-outline-none",
"[&:is(input,textarea):disabled]:tw-bg-secondary-100",
];
if (this.parentFormField === null) {
classes.push(...inputBorderClasses(this.hasError), ...this.standaloneInputClasses);
}
return classes.filter((s) => s != "");
}
@HostBinding() @Input() id = `bit-input-${nextId++}`;
@HostBinding("attr.aria-describedby") ariaDescribedBy: string;
@HostBinding("attr.aria-invalid") get ariaInvalid() {
return this.hasError ? true : undefined;
}
@HostBinding("attr.type") @Input() type?: InputTypes;
@HostBinding("attr.spellcheck") @Input() spellcheck?: boolean;
@HostBinding()
@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;
@Input() hasPrefix = false;
@Input() hasSuffix = false;
@Input() showErrorsWhenDisabled? = false;
get labelForId(): string {
return this.id;
}
@HostListener("input")
onInput() {
this.ngControl?.control?.markAsUntouched();
}
get hasError() {
if (this.showErrorsWhenDisabled) {
return (
(this.ngControl?.status === "INVALID" || this.ngControl?.status === "DISABLED") &&
this.ngControl?.touched &&
this.ngControl?.errors != null
);
} else {
return this.ngControl?.status === "INVALID" && this.ngControl?.touched;
}
}
get error(): [string, any] {
const key = Object.keys(this.ngControl.errors)[0];
return [key, this.ngControl.errors[key]];
}
constructor(
@Optional() @Self() private ngControl: NgControl,
private ngZone: NgZone,
private elementRef: ElementRef<HTMLInputElement>,
@Optional() private parentFormField: BitFormFieldComponent,
) {}
focus() {
this.ngZone.runOutsideAngular(() => {
const end = this.elementRef.nativeElement.value.length;
this.elementRef.nativeElement.setSelectionRange(end, end);
this.elementRef.nativeElement.focus();
});
}
get readOnly(): boolean {
return this.elementRef.nativeElement.readOnly;
}
get standaloneInputClasses() {
return [
"tw-px-3",
"tw-py-2",
"tw-rounded-lg",
// Hover
this.hasError ? "hover:tw-border-danger-700" : "hover:tw-border-primary-600",
// Focus
"focus:hover:tw-border-primary-600",
"disabled:tw-bg-secondary-100",
"disabled:hover:tw-border-secondary-500",
"focus:tw-border-primary-600",
"focus:tw-ring-1",
"focus:tw-ring-inset",
"focus:tw-ring-primary-600",
"focus:tw-z-10",
];
}
}