Skip to content
Snippets Groups Projects
Commit 6b42350b authored by jojohoch's avatar jojohoch
Browse files

Refactor SpellCorrectComponent

- Remove function call in template
- Init value with elementModel.value
- Set readonly and disabled properties in template and remove OnInit
- Compact code
parent 7bba8510
No related branches found
No related tags found
No related merge requests found
import { Component, OnInit, ViewChild } from '@angular/core'; import { Component, OnInit, ViewChild } from '@angular/core';
import { MatInput } from '@angular/material/input'; import { MatInput } from '@angular/material/input';
import { MatButton } from '@angular/material/button';
import { FormElementComponent } from '../../directives/form-element-component.directive'; import { FormElementComponent } from '../../directives/form-element-component.directive';
import { SpellCorrectElement } from './spell-correct-element'; import { SpellCorrectElement } from './spell-correct-element';
...@@ -9,16 +8,20 @@ import { SpellCorrectElement } from './spell-correct-element'; ...@@ -9,16 +8,20 @@ import { SpellCorrectElement } from './spell-correct-element';
template: ` template: `
<div fxFlex <div fxFlex
fxLayout="column" fxLayout="column"
[style.width.%]="100"
appInputBackgroundColor [backgroundColor]="elementModel.surfaceProps.backgroundColor" appInputBackgroundColor [backgroundColor]="elementModel.surfaceProps.backgroundColor"
[style.width.%]="100"
[style.height.%]="100"> [style.height.%]="100">
<mat-form-field class="small-input"> <mat-form-field class="small-input">
<input matInput type="text" <input matInput type="text"
[style.text-align]="'center'" [style.text-align]="'center'"
autocomplete="off" autocomplete="off"
[readonly]="elementModel.readOnly"
[value]="elementModel.value"
[formControl]="elementFormControl"> [formControl]="elementFormControl">
</mat-form-field> </mat-form-field>
<button mat-button <button mat-button
type="button"
[disabled]="elementModel.readOnly"
[style.color]="elementModel.fontProps.fontColor" [style.color]="elementModel.fontProps.fontColor"
[style.font-family]="elementModel.fontProps.font" [style.font-family]="elementModel.fontProps.font"
[style.font-size.px]="elementModel.fontProps.fontSize" [style.font-size.px]="elementModel.fontProps.fontSize"
...@@ -26,8 +29,8 @@ import { SpellCorrectElement } from './spell-correct-element'; ...@@ -26,8 +29,8 @@ import { SpellCorrectElement } from './spell-correct-element';
[style.font-style]="elementModel.fontProps.italic ? 'italic' : ''" [style.font-style]="elementModel.fontProps.italic ? 'italic' : ''"
[style.width.%]="100" [style.width.%]="100"
[style.margin-top]="'-20px'" [style.margin-top]="'-20px'"
[style.text-decoration-line]="strikethrough() ? 'line-through' : ''" [style.text-decoration-line]="strikethrough ? 'line-through' : ''"
(click)="onClick($event)"> (click)="onClick()">
&nbsp;{{elementModel.label}}&nbsp; &nbsp;{{elementModel.label}}&nbsp;
</button> </button>
</div> </div>
...@@ -38,37 +41,21 @@ import { SpellCorrectElement } from './spell-correct-element'; ...@@ -38,37 +41,21 @@ import { SpellCorrectElement } from './spell-correct-element';
}) })
export class SpellCorrectComponent extends FormElementComponent implements OnInit { export class SpellCorrectComponent extends FormElementComponent implements OnInit {
elementModel!: SpellCorrectElement; elementModel!: SpellCorrectElement;
@ViewChild(MatInput) inputElement!: MatInput; strikethrough!: boolean;
@ViewChild(MatButton) buttonElement!: MatButton;
ngOnInit(): void { @ViewChild(MatInput) inputElement!: MatInput;
super.ngOnInit();
if (this.inputElement && this.elementModel.readOnly) {
this.inputElement.readonly = true;
}
if (this.buttonElement && this.elementModel.readOnly) {
this.buttonElement.disabled = true;
}
}
strikethrough(): boolean { strikeOut(): boolean {
if (this.inputElement) { this.strikethrough = (this.inputElement && this.inputElement.value) ? this.inputElement.value.length > 0 : false;
const value = this.inputElement.value; return this.strikethrough;
if (value === null) return false;
if (value === undefined) return false;
return value.length > 0;
}
return false;
} }
onClick(event: MouseEvent) : void { onClick() : void {
if (this.strikethrough()) { if (this.strikeOut()) {
this.elementFormControl.setValue(''); this.elementFormControl.setValue('');
} else { } else {
this.elementFormControl.setValue(this.elementModel.label); this.elementFormControl.setValue(this.elementModel.label);
this.inputElement.focus(); this.inputElement.focus();
} }
event.preventDefault();
event.stopPropagation();
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment