Skip to content

Instantly share code, notes, and snippets.

@armanozak
Last active May 9, 2019 08:25
Show Gist options
  • Save armanozak/9cfe9d087bd39981335c624929ef3288 to your computer and use it in GitHub Desktop.
Save armanozak/9cfe9d087bd39981335c624929ef3288 to your computer and use it in GitHub Desktop.
Two Input Components Extending Abstract Input Component #blog
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