Last active
May 9, 2019 08:25
-
-
Save armanozak/9cfe9d087bd39981335c624929ef3288 to your computer and use it in GitHub Desktop.
Two Input Components Extending Abstract Input Component #blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ChangeDetectionStrategy, Component, forwardRef, Input } from '@angular/core'; | |
import { NG_VALUE_ACCESSOR } from '@angular/forms'; | |
import { AbstractInputComponent } from '../abstracts/input.component'; | |
@Component({ | |
selector: 'app-input-text', | |
template: ` | |
<input [id]="cid" | |
[type]="type" | |
[placeholder]="placeholder" | |
[readonly]="inputReadonly" | |
[required]="inputRequired" | |
(blur)="onBlur.next()" | |
(focus)="onFocus.next()" | |
[(ngModel)]="value"> | |
`, | |
providers: [ | |
{ | |
provide: NG_VALUE_ACCESSOR, | |
useExisting: forwardRef(() => InputTextComponent), | |
multi: true, | |
}, | |
], | |
changeDetection: ChangeDetectionStrategy.OnPush | |
}) | |
export class InputTextComponent extends AbstractInputComponent {} | |
@Component({ | |
selector: 'app-input-text-with-label', | |
template: ` | |
<div> | |
<label [attr.for]="cid">{{ label + asterix }}</label> | |
<input [id]="cid" | |
[type]="type" | |
[placeholder]="placeholder" | |
[readonly]="inputReadonly" | |
[required]="inputRequired" | |
(blur)="onBlur.next()" | |
(focus)="onFocus.next()" | |
[(ngModel)]="value"> | |
</div> | |
`, | |
providers: [ | |
{ | |
provide: NG_VALUE_ACCESSOR, | |
useExisting: forwardRef(() => InputTextWithLabelComponent), | |
multi: true, | |
}, | |
], | |
changeDetection: ChangeDetectionStrategy.OnPush | |
}) | |
export class InputTextWithLabelComponent extends AbstractInputComponent { | |
@Input() | |
label: string = ''; | |
get asterix(): string { | |
return this.inputRequired ? ' *' : ''; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment