Skip to content

Commit 75b0412

Browse files
committed
Added autofocus on editable task when edit mode is enabled on clicking the task label. Added a directive that does this
1 parent 01ba862 commit 75b0412

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import { HttpModule } from '@angular/http';
66
import { AppComponent } from './app.component';
77
import { TodoComponent } from './todo/todo.component';
88
import { TaskComponent } from './todo/task.component';
9+
import { FocusDirective } from './directive/focus.directive';
910

1011
@NgModule({
1112
declarations: [
1213
AppComponent,
1314
TodoComponent,
14-
TaskComponent
15+
TaskComponent,
16+
FocusDirective
1517
],
1618
imports: [
1719
BrowserModule,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { FocusDirective } from './focus.directive';
2+
3+
describe('FocusDirective', () => {
4+
it('should create an instance', () => {
5+
const directive = new FocusDirective();
6+
expect(directive).toBeTruthy();
7+
});
8+
});

src/app/directive/focus.directive.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Directive, ElementRef, Renderer } from '@angular/core';
2+
3+
@Directive({
4+
selector: '[focus]'
5+
})
6+
export class FocusDirective {
7+
8+
constructor(private elementRef: ElementRef, private renderer: Renderer) { }
9+
10+
ngOnInit() {
11+
this.renderer.invokeElementMethod(
12+
this.elementRef.nativeElement, 'focus', []);
13+
}
14+
}

src/app/todo/task.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
<div class="col-sm-10">
66
<span [ngClass]="task.isDone ? 'task-done' : 'task'" (click)="enableEditing()" *ngIf="!editable">{{task.name}}</span>
77

8-
<form *ngIf="editable" (submit)="editName()">
9-
<input type="text" [(ngModel)] = "editedName" name="newName" (blur)="disableEditing()"/>
8+
<form *ngIf="editable" class="form-group" (submit)="editName()">
9+
<input type="text" class="form-control input-sm" [(ngModel)] = "editedName" name="newName" (blur)="disableEditing()" focus/>
1010
</form>
1111
</div>
1212
<div class="col-sm-1">

0 commit comments

Comments
 (0)