Skip to content

Commit

Permalink
Merge pull request #26291 from qmonmert/angularviewchild2
Browse files Browse the repository at this point in the history
[Angular] Use viewChild signal
  • Loading branch information
DanielFran authored May 28, 2024
2 parents 3bbe741 + b58ae4c commit 1671c93
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
See the License for the specific language governing permissions and
limitations under the License.
-%>
import { ElementRef } from '@angular/core';
import { ElementRef, signal } from '@angular/core';
import { ComponentFixture, TestBed, inject, tick, fakeAsync } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { FormBuilder } from '@angular/forms';
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('PasswordResetFinishComponent', () => {
const node = {
focus: jest.fn(),
};
comp.newPassword = new ElementRef(node);
comp.newPassword = signal<ElementRef>(new ElementRef(node));

comp.ngAfterViewInit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
See the License for the specific language governing permissions and
limitations under the License.
-%>
import { Component, inject, OnInit, AfterViewInit, ElementRef, ViewChild, signal } from '@angular/core';
import { Component, inject, OnInit, AfterViewInit, ElementRef, signal, viewChild } from '@angular/core';
import { FormGroup, FormControl, Validators, FormsModule, ReactiveFormsModule } from '@angular/forms';
import { ActivatedRoute, RouterModule } from '@angular/router';
import PasswordStrengthBarComponent from 'app/account/password/password-strength-bar/password-strength-bar.component';
Expand All @@ -31,8 +31,7 @@ import { PasswordResetFinishService } from './password-reset-finish.service';
templateUrl: './password-reset-finish.component.html',
})
export default class PasswordResetFinishComponent implements OnInit, AfterViewInit {
@ViewChild('newPassword', { static: false })
newPassword?: ElementRef;
newPassword = viewChild.required<ElementRef>('newPassword');

initialized = signal(false);
doNotMatch = signal(false);
Expand Down Expand Up @@ -64,9 +63,7 @@ export default class PasswordResetFinishComponent implements OnInit, AfterViewIn
}

ngAfterViewInit(): void {
if (this.newPassword) {
this.newPassword.nativeElement.focus();
}
this.newPassword().nativeElement.focus();
}

finishReset(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
See the License for the specific language governing permissions and
limitations under the License.
-%>
import { ElementRef } from '@angular/core';
import { ElementRef, signal } from '@angular/core';
import { ComponentFixture, TestBed, inject } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { FormBuilder } from '@angular/forms';
Expand All @@ -43,7 +43,7 @@ describe('PasswordResetInitComponent', () => {
const node = {
focus: jest.fn(),
};
comp.email = new ElementRef(node);
comp.email = signal<ElementRef>(new ElementRef(node));

comp.ngAfterViewInit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
See the License for the specific language governing permissions and
limitations under the License.
-%>
import { Component, AfterViewInit, ElementRef, inject, ViewChild, signal } from '@angular/core';
import { Component, AfterViewInit, ElementRef, inject, signal, viewChild } from '@angular/core';
import { FormBuilder, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import SharedModule from 'app/shared/shared.module';

Expand All @@ -29,8 +29,7 @@ import { PasswordResetInitService } from './password-reset-init.service';
templateUrl: './password-reset-init.component.html',
})
export default class PasswordResetInitComponent implements AfterViewInit {
@ViewChild('email', { static: false })
email?: ElementRef;
email = viewChild.required<ElementRef>('email');

success = signal(false);
resetRequestForm;
Expand All @@ -45,9 +44,7 @@ export default class PasswordResetInitComponent implements AfterViewInit {
}

ngAfterViewInit(): void {
if (this.email) {
this.email.nativeElement.focus();
}
this.email().nativeElement.focus();
}

requestReset(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
See the License for the specific language governing permissions and
limitations under the License.
-%>
import { Component, AfterViewInit, ElementRef, inject, ViewChild, signal } from '@angular/core';
import { Component, AfterViewInit, ElementRef, inject, signal, viewChild } from '@angular/core';
import { HttpErrorResponse } from '@angular/common/http';
import { RouterModule } from '@angular/router';
import { FormGroup, FormControl, Validators, FormsModule, ReactiveFormsModule } from '@angular/forms';
Expand All @@ -36,8 +36,7 @@ import PasswordStrengthBarComponent from '../password/password-strength-bar/pass
templateUrl: './register.component.html',
})
export default class RegisterComponent implements AfterViewInit {
@ViewChild('login', { static: false })
login?: ElementRef;
login = viewChild.required<ElementRef>('login');

doNotMatch = signal(false);
error = signal(false);
Expand Down Expand Up @@ -73,9 +72,7 @@ export default class RegisterComponent implements AfterViewInit {
private registerService = inject(RegisterService);

ngAfterViewInit(): void {
if (this.login) {
this.login.nativeElement.focus();
}
this.login().nativeElement.focus();
}

register(): void {
Expand Down

0 comments on commit 1671c93

Please sign in to comment.