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

feat(module:input-number): add nzReadOnly property #7372

Merged
merged 8 commits into from Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions components/input-number/doc/index.en-US.md
Expand Up @@ -24,6 +24,7 @@ import { NzInputNumberModule } from 'ng-zorro-antd/input-number';
| `[ngModel]` | current value, double binding | `number \| string` \| `string` | - |
| `[nzAutoFocus]` | get focus when component mounted | `boolean` | `false` |
| `[nzDisabled]` | disable the input | `boolean` | `false` |
| `[nzReadOnly]` | If readonly the input | `boolean` | `false` |
| `[nzMax]` | max value | `number` | `Infinity` |
| `[nzMin]` | min value | `number` | `-Infinity` |
| `[nzFormatter]` | Specifies the format of the value presented | `(value: number \| string) => string \| number` | - |
Expand Down
1 change: 1 addition & 0 deletions components/input-number/doc/index.zh-CN.md
Expand Up @@ -25,6 +25,7 @@ import { NzInputNumberModule } from 'ng-zorro-antd/input-number';
| `[ngModel]` | 当前值,可双向绑定 | `number \| string` \| `string` | - |
| `[nzAutoFocus]` | 自动获取焦点 | `boolean` | `false` |
| `[nzDisabled]` | 禁用 | `boolean` | `false` |
| `[nzReadOnly]` | 只读 | `boolean` | `false` |
| `[nzMax]` | 最大值 | `number` | `Infinity` |
| `[nzMin]` | 最小值 | `number` | `-Infinity` |
| `[nzFormatter]` | 指定输入框展示值的格式 | `(value: number \| string) => string \| number` | - |
Expand Down
4 changes: 4 additions & 0 deletions components/input-number/input-number.component.ts
Expand Up @@ -70,6 +70,7 @@ import { InputBoolean, isNotNil } from 'ng-zorro-antd/core/util';
[attr.max]="nzMax"
[placeholder]="nzPlaceHolder"
[attr.step]="nzStep"
[readOnly]="nzReadOnly"
[attr.inputmode]="nzInputMode"
[ngModel]="displayValue"
(ngModelChange)="onModelChange($event)"
Expand All @@ -91,11 +92,13 @@ import { InputBoolean, isNotNil } from 'ng-zorro-antd/core/util';
'[class.ant-input-number-lg]': `nzSize === 'large'`,
'[class.ant-input-number-sm]': `nzSize === 'small'`,
'[class.ant-input-number-disabled]': 'nzDisabled',
'[class.ant-input-number-readonly]': 'nzReadOnly',
'[class.ant-input-number-rtl]': `dir === 'rtl'`
}
})
export class NzInputNumberComponent implements ControlValueAccessor, AfterViewInit, OnChanges, OnInit, OnDestroy {
static ngAcceptInputType_nzDisabled: BooleanInput;
static ngAcceptInputType_nzReadOnly: BooleanInput;
static ngAcceptInputType_nzAutoFocus: BooleanInput;

private autoStepTimer?: number;
Expand Down Expand Up @@ -127,6 +130,7 @@ export class NzInputNumberComponent implements ControlValueAccessor, AfterViewIn
@Input() nzInputMode: string = 'decimal';
@Input() nzId: string | null = null;
@Input() @InputBoolean() nzDisabled = false;
@Input() @InputBoolean() nzReadOnly = false;
@Input() @InputBoolean() nzAutoFocus = false;
@Input() nzFormatter: (value: number) => string | number = value => value;

Expand Down
42 changes: 41 additions & 1 deletion components/input-number/input-number.spec.ts
Expand Up @@ -14,7 +14,11 @@ describe('input number', () => {
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
imports: [NzInputNumberModule, FormsModule, ReactiveFormsModule],
declarations: [NzTestInputNumberBasicComponent, NzTestInputNumberFormComponent]
declarations: [
NzTestInputNumberBasicComponent,
NzTestInputNumberFormComponent,
NzTestReadOnlyInputNumberBasicComponent
]
});
TestBed.compileComponents();
}));
Expand Down Expand Up @@ -467,6 +471,34 @@ describe('input number', () => {
expect(testComponent.formGroup.get('inputNumber')!.value).toBe(10);
}));
});
describe('input number readOnly', () => {
let fixture: ComponentFixture<NzTestReadOnlyInputNumberBasicComponent>;
let testComponent: NzTestReadOnlyInputNumberBasicComponent;
let inputNumber: DebugElement;
let inputElement: HTMLInputElement;

beforeEach(fakeAsync(() => {
fixture = TestBed.createComponent(NzTestReadOnlyInputNumberBasicComponent);
fixture.detectChanges();
flush();
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;

inputNumber = fixture.debugElement.query(By.directive(NzInputNumberComponent));
inputElement = inputNumber.nativeElement.querySelector('input');
}));
it('should readOnly work', () => {
fixture.detectChanges();
testComponent.readonly = true;
testComponent.nzInputNumberComponent.nzReadOnly = true;
testComponent.nzInputNumberComponent.ngAfterViewInit();
fixture.detectChanges();
expect(inputElement.attributes.getNamedItem('readOnly')!.name).toBe('readonly');
testComponent.readonly = false;
fixture.detectChanges();
expect(inputElement.attributes.getNamedItem('readOnly')).toBe(null);
});
});
});

@Component({
Expand Down Expand Up @@ -505,6 +537,14 @@ export class NzTestInputNumberBasicComponent {
modelChange = jasmine.createSpy('change callback');
}

@Component({
template: ` <nz-input-number [nzReadOnly]="readonly"></nz-input-number> `
})
export class NzTestReadOnlyInputNumberBasicComponent {
@ViewChild(NzInputNumberComponent, { static: false }) nzInputNumberComponent!: NzInputNumberComponent;
readonly = false;
}

@Component({
template: `
<form [formGroup]="formGroup">
Expand Down