Skip to content

Commit 0d1c99f

Browse files
anemoneteaAkshat55
andauthored
fix: Avoid null pointers by not using nativeElement (#2323)
Signed-off-by: anemonetea <[email protected]> Co-authored-by: Akshat Patel <[email protected]>
1 parent 2af2bb7 commit 0d1c99f

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/checkbox/checkbox.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ export class Checkbox implements ControlValueAccessor, AfterViewInit {
314314
*/
315315
setDisabledState(isDisabled: boolean) {
316316
this.disabled = isDisabled;
317-
this.inputCheckbox.nativeElement.disabled = this.disabled;
317+
this.changeDetectorRef.markForCheck();
318318
}
319319

320320
@HostListener("focusout")

src/checkbox/checkbox.stories.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import { DocumentationModule } from "../documentation-component/documentation.mo
1616
selector: "app-reactive-forms",
1717
template: `
1818
<form [formGroup]="formGroup">
19+
<ibm-checkbox formControlName="disabledCheckbox">
20+
Disabled checkbox in a reactive form
21+
</ibm-checkbox>
1922
<ibm-checkbox formControlName="checkbox">
2023
Checkbox in a reactive form
2124
</ibm-checkbox>
@@ -26,19 +29,21 @@ import { DocumentationModule } from "../documentation-component/documentation.mo
2629
})
2730
class ReactiveFormsStory implements OnInit {
2831
public formGroup: FormGroup;
29-
disabled = false;
3032

3133
constructor(protected formBuilder: FormBuilder) { }
3234

3335
ngOnInit() {
3436
this.formGroup = this.formBuilder.group({
35-
checkbox: new FormControl()
37+
disabledCheckbox: this.formBuilder.control({value: true, disabled: true}),
38+
checkbox: this.formBuilder.control(false)
3639
});
3740
}
3841

3942
toggleDisable() {
4043
const checkbox = this.formGroup.get("checkbox");
41-
checkbox.disabled ? checkbox.enable() : checkbox.disable();
44+
if (checkbox != null) {
45+
checkbox.disabled ? checkbox.enable() : checkbox.disable();
46+
}
4247
}
4348
}
4449

0 commit comments

Comments
 (0)