Skip to content

Commit 4598561

Browse files
committed
Set focus and selection on edit
1 parent a3d478b commit 4598561

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

src/app/component/result/result.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<mat-form-field class="w-full">
3939
<input
4040
id="result-title-input"
41+
#resultTitleInput
4142
[(ngModel)]="element.title"
4243
type="text"
4344
matInput

src/app/component/result/result.component.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { SelectionModel } from '@angular/cdk/collections';
3-
import { Component, computed, inject, ViewChild } from '@angular/core';
3+
import {
4+
Component,
5+
computed,
6+
ElementRef,
7+
inject,
8+
ViewChild,
9+
} from '@angular/core';
410
import { FormsModule } from '@angular/forms';
511
import { MatButtonModule } from '@angular/material/button';
612
import { MatDialog, MatDialogConfig } from '@angular/material/dialog';
@@ -39,6 +45,7 @@ export class ResultComponent {
3945
);
4046
@ViewChild(MatPaginator) paginator!: MatPaginator;
4147
@ViewChild(MatSort) sort!: MatSort;
48+
@ViewChild('resultTitleInput') resultTitleInput!: ElementRef;
4249
originalPosts = new Map<number, Post>();
4350
displayedColumns: string[] = ['id', 'userId', 'title', 'body', 'action'];
4451
createDisabled = true;
@@ -81,6 +88,12 @@ export class ResultComponent {
8188
onEditClick(element: any): void {
8289
element.editable = true;
8390
this.originalPosts.set(element.id, JSON.parse(JSON.stringify(element)));
91+
92+
// Use setTimeout to ensure the element is fully rendered
93+
setTimeout(() => {
94+
this.resultTitleInput.nativeElement.focus();
95+
this.resultTitleInput.nativeElement.select();
96+
}, 0);
8497
}
8598

8699
onDeleteClick(element: Post): void {

0 commit comments

Comments
 (0)