Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions projects/components/src/checkbox/checkbox.component.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fakeAsync } from '@angular/core/testing';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { RouterTestingModule } from '@angular/router/testing';
import { createHostFactory, Spectator } from '@ngneat/spectator/jest';
import { CheckboxComponent } from './checkbox.component';
Expand All @@ -9,7 +10,7 @@ describe('Checkbox component', () => {

const createHost = createHostFactory({
component: CheckboxComponent,
imports: [TraceCheckboxModule, RouterTestingModule],
imports: [TraceCheckboxModule, RouterTestingModule, ReactiveFormsModule],
providers: [],
declareComponent: false
});
Expand Down Expand Up @@ -52,12 +53,32 @@ describe('Checkbox component', () => {

// Click will toggle the values to true
spectator.click(inputElement);
expect(spectator.component.checked).toBe(true);
expect(spectator.component.isChecked).toBe(true);
expect(checkboxChangeSpy).toHaveBeenCalledWith(true);

// Click will toggle the values to false
spectator.click(inputElement);
expect(spectator.component.checked).toBe(false);
expect(spectator.component.isChecked).toBe(false);
expect(checkboxChangeSpy).toHaveBeenCalledWith(false);
}));

test('should work correctly with control value accessor', () => {
const formControl = new FormControl(false);
spectator = createHost(
`<ht-checkbox [label]="label" [formControl]="formControl">
</ht-checkbox>`,
{
hostProps: {
formControl: formControl
}
}
);
expect(spectator.component.isChecked).toBe(false);

formControl.setValue(true);
expect(spectator.component.isChecked).toBe(true);

formControl.disable();
expect(spectator.component.isDisabled).toBe(true);
});
});
70 changes: 59 additions & 11 deletions projects/components/src/checkbox/checkbox.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, EventEmitter, Input, Output } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
import { MatCheckboxChange } from '@angular/material/checkbox';

@Component({
Expand All @@ -8,31 +9,78 @@ import { MatCheckboxChange } from '@angular/material/checkbox';
template: `
<mat-checkbox
labelPosition="after"
[checked]="this.checked"
[disabled]="this.disabled"
(change)="onCheckboxChange($event)"
[checked]="this.isChecked"
[disabled]="this.isDisabled"
(change)="this.onCheckboxChange($event)"
class="ht-checkbox"
[ngClass]="{ disabled: this.disabled }"
[ngClass]="{ disabled: this.isDisabled }"
>
<ht-label class="label" *ngIf="this.label !== undefined && this.label !== ''" [label]="this.label"></ht-label>
</mat-checkbox>
`
`,
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: CheckboxComponent,
multi: true
}
]
})
export class CheckboxComponent {
export class CheckboxComponent implements ControlValueAccessor {
@Input()
public label?: string;

@Input()
public checked: boolean | undefined;
public set checked(checked: boolean | undefined) {
this.isChecked = checked ?? false;
}

public get checked(): boolean {
return this.isChecked;
}

@Input()
public disabled: boolean | undefined;
public set disabled(disabled: boolean | undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need these setters?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid mutating the input property.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I follow. Can you please provide more info or give example?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid directly mutating the disabled value which is assigned from outside via @Input()

this.isDisabled = disabled ?? false;
}

public get disabled(): boolean {
return this.isDisabled;
}

@Output()
public readonly checkedChange: EventEmitter<boolean> = new EventEmitter();

public isChecked: boolean = false;
public isDisabled: boolean = false;

private onTouched!: () => void;
private onChanged!: (value: boolean) => void;

public constructor(private readonly cdr: ChangeDetectorRef) {}

public onCheckboxChange(event: MatCheckboxChange): void {
this.checked = event.checked;
this.checkedChange.emit(this.checked);
this.isChecked = event.checked;
this.checkedChange.emit(this.isChecked);
this.onChanged(this.isChecked);
this.onTouched();
}

public registerOnChange(fn: (value: boolean) => void): void {
this.onChanged = fn;
}

public registerOnTouched(fn: () => void): void {
this.onTouched = fn;
}

public setDisabledState(isDisabled: boolean): void {
this.isDisabled = isDisabled;
this.cdr.markForCheck();
}

public writeValue(isChecked: boolean | undefined): void {
this.isChecked = isChecked ?? false;
this.cdr.markForCheck();
}
}