Skip to content

Commit

Permalink
add SuffixDirective
Browse files Browse the repository at this point in the history
- add a new column to show SuffixDirective usage
- add officeSurface field to employee.model.ts as a new column value
  • Loading branch information
bugtamer committed Jun 5, 2021
1 parent 835686e commit 888a5cb
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<app-table [data]="employeeList">

<ng-template appInjector="caption">Table description</ng-template>
<ng-template appInjector="caption">Staff</ng-template>

<ng-template appInjector="header">
<th>Id</th>
<th>Name</th>
<th>Surname</th>
<th>Age</th>
<th>foo()</th>
<th>NG Directive</th>
</ng-template>

<ng-template appInjector="body" let-employee>
Expand All @@ -20,6 +21,12 @@
<td>
<button type="button" (click)="foo(employee)">{{employee.name}}</button>
</td>
<td>
<label>
Office surface
<input type="text" [value]="employee.officeSurface" appSuffix="">
</label>
</td>
</ng-template>

<ng-template appInjector="footer">
Expand All @@ -28,6 +35,7 @@
<th>Surname</th>
<th>Age</th>
<th>foo()</th>
<th>Directive test</th>
</ng-template>

</app-table>
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { TableComponent } from './components/table/table.component';
import { InjectorDirective } from './directives/injector/injector.directive';
import { SuffixDirective } from './directives/suffix/suffix.directive';

@NgModule({
declarations: [
AppComponent,
TableComponent,
InjectorDirective
InjectorDirective,
SuffixDirective
],
imports: [
BrowserModule,
Expand Down
52 changes: 52 additions & 0 deletions src/app/directives/suffix/suffix.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { SuffixDirective } from './suffix.directive';

const unformated = '23';
const suffix = 'm²';
const formated = `${unformated} ${suffix}`;


@Component({
template: `<input type="text" autofocus value="${unformated}" appSuffix="${suffix}">`
})
class TestComponent { }


describe('SuffixDirective', () => {

let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
let debugElement: HTMLElement;
let input: HTMLInputElement | null;

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ TestComponent, SuffixDirective ]
});

fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
debugElement = fixture.debugElement.nativeElement;
input = debugElement.querySelector('input');
});

// fit('should create an instance', () => {
// expect(directive).toBeTruthy();
// });

it('should create component', () => {
expect(component).toBeDefined();
});

it(`should show '${unformated}' when the target input is focused`, () => {
expect(input?.value).toBe(unformated);
})

it(`should show '${formated}' when the target input is blured`, () => {
input?.blur();
fixture.detectChanges();
expect(input?.value).toBe(formated);
})

});
40 changes: 40 additions & 0 deletions src/app/directives/suffix/suffix.directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Directive, Input, ElementRef, HostListener } from '@angular/core';

@Directive({
selector: '[appSuffix]'
})
export class SuffixDirective {

@Input('appSuffix') suffix!: string;

constructor(private elementRef: ElementRef) {
this.input = this.elementRef.nativeElement;
}

ngOnInit() {
this.input.value = SuffixDirective.format(this.input.value, this.suffix);
}

@HostListener("focus", ["$event.target.value"])
onFocus(value: string) {
this.input.value = SuffixDirective.unformat(value, this.suffix);
}

@HostListener("blur", ["$event.target.value"])
onBlur(value: string) {
this.input.value = SuffixDirective.format(value, this.suffix);
}

static format(value: string, suffix: string): string {
return value.length > 0 ? `${value} ${suffix}` : `${value}`;
}

static unformat(value: string, suffix: string): string {
const begin = 0;
const end = value.length - suffix.length - 1
return value.substr(begin, end);
}

private input: HTMLInputElement;

}
2 changes: 2 additions & 0 deletions src/app/models/employee.model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export class Employee {

readonly officeSurface: number = Math.trunc( Math.random() * 100 );

constructor(
public readonly id: number,
public readonly name: string,
Expand Down

0 comments on commit 888a5cb

Please sign in to comment.