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

fix(module:nz-input): cannot be disabled if ngModel is missing (#4655) #4688

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions components/input/nz-input.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

import { Directive, ElementRef, Input, Renderer2 } from '@angular/core';
import { Directive, ElementRef, Input, OnChanges, Renderer2, SimpleChanges } from '@angular/core';
import { InputBoolean, NzSizeLDSType } from 'ng-zorro-antd/core';

@Directive({
Expand All @@ -18,11 +18,26 @@ import { InputBoolean, NzSizeLDSType } from 'ng-zorro-antd/core';
'[class.ant-input-sm]': `nzSize === 'small'`
}
})
export class NzInputDirective {
export class NzInputDirective implements OnChanges {
@Input() nzSize: NzSizeLDSType = 'default';
@Input() @InputBoolean() disabled = false;

constructor(renderer: Renderer2, elementRef: ElementRef) {
constructor(private renderer: Renderer2, private elementRef: ElementRef) {
renderer.addClass(elementRef.nativeElement, 'ant-input');
}

ngOnChanges(changes: SimpleChanges): void {
const { disabled } = changes;
if (disabled) {
this.updateDisabledState();
}
}

private updateDisabledState(): void {
if (this.disabled) {
this.renderer.setAttribute(this.elementRef.nativeElement, 'disabled', '');
} else {
this.renderer.removeAttribute(this.elementRef.nativeElement, 'disabled');
}
}
}
2 changes: 2 additions & 0 deletions components/input/nz-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ describe('input', () => {
it('should disabled work', () => {
fixture.detectChanges();
expect(inputElement.nativeElement.classList).not.toContain('ant-input-disabled');
expect(inputElement.nativeElement.attributes.getNamedItem('disabled')).toBeNull();
testComponent.disabled = true;
fixture.detectChanges();
expect(inputElement.nativeElement.classList).toContain('ant-input-disabled');
expect(inputElement.nativeElement.attributes.getNamedItem('disabled')).toBeDefined();
});
it('should nzSize work', () => {
testComponent.size = 'small';
Expand Down