Skip to content

Commit 0da7496

Browse files
authored
feat(module:input-number): add nzReadOnly property (#7372)
close #7369 * feat(module:input-number): add nzReadOnly property * chore(module:input-number): modify property name * chore(module:input-number): modify test case name * chore: modify doc style * chore: doc style * chore: doc style * chore: input doc style * chore: doc style
1 parent 4dfd9cd commit 0da7496

4 files changed

Lines changed: 47 additions & 1 deletion

File tree

components/input-number/doc/index.en-US.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { NzInputNumberModule } from 'ng-zorro-antd/input-number';
2424
| `[ngModel]` | current value, double binding | `number \| string` \| `string` | - |
2525
| `[nzAutoFocus]` | get focus when component mounted | `boolean` | `false` |
2626
| `[nzDisabled]` | disable the input | `boolean` | `false` |
27+
| `[nzReadOnly]` | If readonly the input | `boolean` | `false` |
2728
| `[nzMax]` | max value | `number` | `Infinity` |
2829
| `[nzMin]` | min value | `number` | `-Infinity` |
2930
| `[nzFormatter]` | Specifies the format of the value presented | `(value: number \| string) => string \| number` | - |

components/input-number/doc/index.zh-CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { NzInputNumberModule } from 'ng-zorro-antd/input-number';
2525
| `[ngModel]` | 当前值,可双向绑定 | `number \| string` \| `string` | - |
2626
| `[nzAutoFocus]` | 自动获取焦点 | `boolean` | `false` |
2727
| `[nzDisabled]` | 禁用 | `boolean` | `false` |
28+
| `[nzReadOnly]` | 只读 | `boolean` | `false` |
2829
| `[nzMax]` | 最大值 | `number` | `Infinity` |
2930
| `[nzMin]` | 最小值 | `number` | `-Infinity` |
3031
| `[nzFormatter]` | 指定输入框展示值的格式 | `(value: number \| string) => string \| number` | - |

components/input-number/input-number.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ import { InputBoolean, isNotNil } from 'ng-zorro-antd/core/util';
7070
[attr.max]="nzMax"
7171
[placeholder]="nzPlaceHolder"
7272
[attr.step]="nzStep"
73+
[readOnly]="nzReadOnly"
7374
[attr.inputmode]="nzInputMode"
7475
[ngModel]="displayValue"
7576
(ngModelChange)="onModelChange($event)"
@@ -91,11 +92,13 @@ import { InputBoolean, isNotNil } from 'ng-zorro-antd/core/util';
9192
'[class.ant-input-number-lg]': `nzSize === 'large'`,
9293
'[class.ant-input-number-sm]': `nzSize === 'small'`,
9394
'[class.ant-input-number-disabled]': 'nzDisabled',
95+
'[class.ant-input-number-readonly]': 'nzReadOnly',
9496
'[class.ant-input-number-rtl]': `dir === 'rtl'`
9597
}
9698
})
9799
export class NzInputNumberComponent implements ControlValueAccessor, AfterViewInit, OnChanges, OnInit, OnDestroy {
98100
static ngAcceptInputType_nzDisabled: BooleanInput;
101+
static ngAcceptInputType_nzReadOnly: BooleanInput;
99102
static ngAcceptInputType_nzAutoFocus: BooleanInput;
100103

101104
private autoStepTimer?: number;
@@ -127,6 +130,7 @@ export class NzInputNumberComponent implements ControlValueAccessor, AfterViewIn
127130
@Input() nzInputMode: string = 'decimal';
128131
@Input() nzId: string | null = null;
129132
@Input() @InputBoolean() nzDisabled = false;
133+
@Input() @InputBoolean() nzReadOnly = false;
130134
@Input() @InputBoolean() nzAutoFocus = false;
131135
@Input() nzFormatter: (value: number) => string | number = value => value;
132136

components/input-number/input-number.spec.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ describe('input number', () => {
1414
beforeEach(fakeAsync(() => {
1515
TestBed.configureTestingModule({
1616
imports: [NzInputNumberModule, FormsModule, ReactiveFormsModule],
17-
declarations: [NzTestInputNumberBasicComponent, NzTestInputNumberFormComponent]
17+
declarations: [
18+
NzTestInputNumberBasicComponent,
19+
NzTestInputNumberFormComponent,
20+
NzTestReadOnlyInputNumberBasicComponent
21+
]
1822
});
1923
TestBed.compileComponents();
2024
}));
@@ -467,6 +471,34 @@ describe('input number', () => {
467471
expect(testComponent.formGroup.get('inputNumber')!.value).toBe(10);
468472
}));
469473
});
474+
describe('input number readOnly', () => {
475+
let fixture: ComponentFixture<NzTestReadOnlyInputNumberBasicComponent>;
476+
let testComponent: NzTestReadOnlyInputNumberBasicComponent;
477+
let inputNumber: DebugElement;
478+
let inputElement: HTMLInputElement;
479+
480+
beforeEach(fakeAsync(() => {
481+
fixture = TestBed.createComponent(NzTestReadOnlyInputNumberBasicComponent);
482+
fixture.detectChanges();
483+
flush();
484+
fixture.detectChanges();
485+
testComponent = fixture.debugElement.componentInstance;
486+
487+
inputNumber = fixture.debugElement.query(By.directive(NzInputNumberComponent));
488+
inputElement = inputNumber.nativeElement.querySelector('input');
489+
}));
490+
it('should readOnly work', () => {
491+
fixture.detectChanges();
492+
testComponent.readonly = true;
493+
testComponent.nzInputNumberComponent.nzReadOnly = true;
494+
testComponent.nzInputNumberComponent.ngAfterViewInit();
495+
fixture.detectChanges();
496+
expect(inputElement.attributes.getNamedItem('readOnly')!.name).toBe('readonly');
497+
testComponent.readonly = false;
498+
fixture.detectChanges();
499+
expect(inputElement.attributes.getNamedItem('readOnly')).toBe(null);
500+
});
501+
});
470502
});
471503

472504
@Component({
@@ -505,6 +537,14 @@ export class NzTestInputNumberBasicComponent {
505537
modelChange = jasmine.createSpy('change callback');
506538
}
507539

540+
@Component({
541+
template: ` <nz-input-number [nzReadOnly]="readonly"></nz-input-number> `
542+
})
543+
export class NzTestReadOnlyInputNumberBasicComponent {
544+
@ViewChild(NzInputNumberComponent, { static: false }) nzInputNumberComponent!: NzInputNumberComponent;
545+
readonly = false;
546+
}
547+
508548
@Component({
509549
template: `
510550
<form [formGroup]="formGroup">

0 commit comments

Comments
 (0)