Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nzAutoFocus] can not work in <nz-input-number> #1706

Closed
wenqi73 opened this issue Jun 23, 2018 · 3 comments · Fixed by #1833
Closed

[nzAutoFocus] can not work in <nz-input-number> #1706

wenqi73 opened this issue Jun 23, 2018 · 3 comments · Fixed by #1833
Assignees

Comments

@wenqi73
Copy link
Member

wenqi73 commented Jun 23, 2018

Version

1.0.0

Environment

mac OS 10.11 Chrome 67

Reproduction link

https://stackblitz.com/edit/nz-input-number?file=src%2Fapp%2Fapp.component.html

Steps to reproduce

Set [nzAutoFocus]="true"

What is expected?

NzInputNumberComponent is focused

What is actually happening?

NzInputNumberComponent is not focused

Other?

@hsuanxyz
Copy link
Member

@wenqi73 Thanks feedback, you can use the following code:

import { Component, ViewChild, OnInit } from '@angular/core';
import { NzInputNumberComponent } from 'ng-zorro-antd';

@Component({
  selector: 'nz-demo-input-number-basic',
  template: `
    <nz-input-number [(ngModel)]="demoValue" [nzMin]="1" [nzMax]="10" [nzStep]="1"></nz-input-number>
  `
})
export class NzDemoInputNumberBasicComponent implements OnInit {
  demoValue = 3;

  @ViewChild(NzInputNumberComponent) numberInput;

  ngOnInit() {
    this.numberInput.focus();
  }
}

@wzhudev wzhudev self-assigned this Jul 16, 2018
@wzhudev
Copy link
Member

wzhudev commented Jul 17, 2018

This method is actually invoked before ngAfterViewInit hook so the input element do not have autofocus attribute. Removing if (this.isInit) would make the input autofocus in Chrome. However, I failed to do the same in Firefox (why?).

updateAutoFocus(): void {
if (this.isInit) {
if (this.nzAutoFocus) {
this.renderer.setAttribute(this.inputElement.nativeElement, 'autofocus', 'autofocus');
} else {
this.renderer.removeAttribute(this.inputElement.nativeElement, 'autofocus');
}
}
}

And this spec cannot represent how things work. Because in this case updateAutoFocus() is invoked after ngAfterViewInit. And it only detects autofocus attribute, we don't know whether the component is focused actually.

it('should autofocus work', () => {
fixture.detectChanges();
testComponent.autofocus = true;
fixture.detectChanges();
expect(inputElement.attributes.getNamedItem('autofocus').name).toBe('autofocus');
testComponent.autofocus = false;
fixture.detectChanges();
expect(inputElement.attributes.getNamedItem('autofocus')).toBe(null);
});

According to HTML5, autofocus would only work right after current page is loaded. Maybe it's not what users want (and seemingly useless for SPA). We could add this.focus() in ngAfterViewInit hook.

@wzhudev wzhudev closed this as completed Jul 17, 2018
@wzhudev wzhudev reopened this Jul 17, 2018
wzhudev pushed a commit to wzhudev/ng-zorro-antd that referenced this issue Jul 17, 2018
wzhudev pushed a commit to wzhudev/ng-zorro-antd that referenced this issue Jul 20, 2018
wzhudev pushed a commit to wzhudev/ng-zorro-antd that referenced this issue Jul 20, 2018
@lock
Copy link

lock bot commented Jul 22, 2019

This thread has been automatically locked because it has not had recent activity. Please open a new issue for related bugs and link to relevant comments in this thread.

@lock lock bot locked as resolved and limited conversation to collaborators Jul 22, 2019
hsuanxyz pushed a commit to hsuanxyz/ng-zorro-antd that referenced this issue Aug 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants