Skip to content

Commit

Permalink
fix(module:input-number,checkbox): accept the disabled change from ng…
Browse files Browse the repository at this point in the history
… control (#8979)

* fix(module:input-number): accept the disabled change from ng control

* fix(module:checkbox): accept the disabled change from ng control
  • Loading branch information
HyperLife1119 authored Jan 21, 2025
1 parent 5d261e4 commit 2d8968d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
4 changes: 1 addition & 3 deletions components/checkbox/checkbox-group.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ export class NzCheckboxGroupComponent implements ControlValueAccessor {
}

setDisabledState(disabled: boolean): void {
if (!this.isDisabledFirstChange) {
this.finalDisabled.set(disabled);
}
this.finalDisabled.set((this.isDisabledFirstChange && this.nzDisabled()) || disabled);
this.isDisabledFirstChange = false;
}

Expand Down
20 changes: 19 additions & 1 deletion components/checkbox/checkbox-group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ describe('checkbox group', () => {
}
});

it('should be set disabled by ng control', async () => {
component.controlDisabled = true;
fixture.detectChanges();
await fixture.whenStable();
for (const element of getOptionElements()) {
expect(element.classList).toContain('ant-checkbox-wrapper-disabled');
}
});

it('should be change value', async () => {
component.value = ['A'];
fixture.detectChanges();
Expand Down Expand Up @@ -150,7 +159,15 @@ describe('checkbox group with custom layout', () => {

@Component({
imports: [NzCheckboxModule, FormsModule],
template: ` <nz-checkbox-group [nzOptions]="options" [nzName]="name" [nzDisabled]="disabled" [(ngModel)]="value" /> `
template: `
<nz-checkbox-group
[nzOptions]="options"
[nzName]="name"
[nzDisabled]="disabled"
[(ngModel)]="value"
[disabled]="controlDisabled"
/>
`
})
class CheckboxGroupTestComponent {
options: string[] | number[] | NzCheckboxOption[] = [
Expand All @@ -160,6 +177,7 @@ class CheckboxGroupTestComponent {
];
value: string[] = [];
disabled = false;
controlDisabled = false;
name: string | null = null;
}

Expand Down
10 changes: 10 additions & 0 deletions components/input-number/input-number.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,14 @@ describe('Input number', () => {
expect(hostElement.classList).toContain('ant-input-number-disabled');
});

it('should be set disabled by ng control', async () => {
component.controlDisabled = true;
fixture.detectChanges();
await fixture.whenStable();
expect(hostElement.querySelector('input')!.disabled).toBeTruthy();
expect(hostElement.classList).toContain('ant-input-number-disabled');
});

it('should be set readonly', () => {
component.readonly = true;
fixture.detectChanges();
Expand Down Expand Up @@ -347,6 +355,7 @@ describe('Input number with affixes or addons', () => {
[nzKeyboard]="keyboard"
[nzControls]="controls"
[(ngModel)]="value"
[disabled]="controlDisabled"
/>
`
})
Expand All @@ -366,6 +375,7 @@ class InputNumberTestComponent {
controls = true;

value: number | null = null;
controlDisabled = false;
inputNumber = viewChild.required(NzInputNumberComponent);
}

Expand Down
4 changes: 1 addition & 3 deletions components/input-number/input-number.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,7 @@ export class NzInputNumberComponent implements OnInit, ControlValueAccessor {
}

setDisabledState(disabled: boolean): void {
if (!this.isDisabledFirstChange) {
this.finalDisabled.set(disabled);
}
this.finalDisabled.set((this.isDisabledFirstChange && this.nzDisabled()) || disabled);
this.isDisabledFirstChange = false;
}

Expand Down

0 comments on commit 2d8968d

Please sign in to comment.