Skip to content

Commit 092f594

Browse files
authored
Merge branch 'master' into dTsvetkov/fix-9782-master
2 parents 530bc16 + da16344 commit 092f594

File tree

7 files changed

+84
-19
lines changed

7 files changed

+84
-19
lines changed

projects/igniteui-angular/src/lib/checkbox/checkbox.component.spec.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, ViewChild } from '@angular/core';
22
import { fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';
3-
import { FormsModule } from '@angular/forms';
3+
import { FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms';
44
import { By } from '@angular/platform-browser';
55
import { IgxRippleModule } from '../directives/ripple/ripple.directive';
66
import { IgxCheckboxComponent } from './checkbox.component';
@@ -22,9 +22,10 @@ describe('IgxCheckbox', () => {
2222
CheckboxExternalLabelComponent,
2323
CheckboxInvisibleLabelComponent,
2424
CheckboxDisabledTransitionsComponent,
25+
CheckboxFormGroupComponent,
2526
IgxCheckboxComponent
2627
],
27-
imports: [FormsModule, IgxRippleModule, NoopAnimationsModule]
28+
imports: [FormsModule, ReactiveFormsModule, IgxRippleModule, NoopAnimationsModule]
2829
})
2930
.compileComponents();
3031
}));
@@ -34,7 +35,7 @@ describe('IgxCheckbox', () => {
3435
fixture.detectChanges();
3536

3637
const checkbox = fixture.componentInstance.cb;
37-
const nativeCheckbox = fixture.debugElement.query(By.css('input')).nativeElement;
38+
const nativeCheckbox = checkbox.nativeCheckbox.nativeElement;
3839
const nativeLabel = checkbox.nativeLabel.nativeElement;
3940
const placeholderLabel = fixture.debugElement.query(By.css('.igx-checkbox__label')).nativeElement;
4041

@@ -69,7 +70,7 @@ describe('IgxCheckbox', () => {
6970
fixture.detectChanges();
7071

7172
expect(nativeCheckbox.checked).toBe(false);
72-
expect(checkboxInstance.checked).toBe(false);
73+
expect(checkboxInstance.checked).toBe(null);
7374

7475
testInstance.subscribed = true;
7576
checkboxInstance.name = 'my-checkbox';
@@ -85,6 +86,22 @@ describe('IgxCheckbox', () => {
8586
expect(checkboxInstance.name).toEqual('my-checkbox');
8687
}));
8788

89+
it('Initializes with form group', () => {
90+
const fixture = TestBed.createComponent(CheckboxFormGroupComponent);
91+
fixture.detectChanges();
92+
93+
const testInstance = fixture.componentInstance;
94+
const checkboxInstance = testInstance.cb;
95+
const form = testInstance.myForm;
96+
97+
form.setValue({ checkbox: true });
98+
expect(checkboxInstance.checked).toBe(true);
99+
100+
form.reset();
101+
102+
expect(checkboxInstance.checked).toBe(null);
103+
});
104+
88105
it('Initializes with external label', () => {
89106
const fixture = TestBed.createComponent(CheckboxExternalLabelComponent);
90107
const checkboxInstance = fixture.componentInstance.cb;
@@ -197,7 +214,7 @@ describe('IgxCheckbox', () => {
197214
fixture.detectChanges();
198215

199216
// Should not update
200-
expect(checkboxInstance.checked).toBe(false);
217+
expect(checkboxInstance.checked).toBe(null);
201218
expect(testInstance.subscribed).toBe(false);
202219
});
203220

@@ -398,3 +415,14 @@ class CheckboxInvisibleLabelComponent {
398415
class CheckboxDisabledTransitionsComponent {
399416
@ViewChild('cb', { static: true }) public cb: IgxCheckboxComponent;
400417
}
418+
419+
@Component({
420+
template: `<form [formGroup]="myForm"><igx-checkbox #cb formControlName="checkbox">Form Group</igx-checkbox></form>`
421+
})
422+
class CheckboxFormGroupComponent {
423+
@ViewChild('cb', { static: true }) public cb: IgxCheckboxComponent;
424+
425+
public myForm = this.fb.group({ checkbox: [''] });
426+
427+
constructor(private fb: FormBuilder) {}
428+
}

projects/igniteui-angular/src/lib/checkbox/checkbox.component.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,9 +428,7 @@ export class IgxCheckboxComponent implements ControlValueAccessor, EditorProvide
428428

429429
/** @hidden @internal */
430430
public writeValue(value: boolean) {
431-
if (typeof value === 'boolean') {
432-
this._checked = value;
433-
}
431+
this._checked = value;
434432
}
435433

436434
/** @hidden @internal */

projects/igniteui-angular/src/lib/grids/cell.component.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,9 @@
9191
</igx-input-group>
9292
</ng-container>
9393
<ng-container *ngIf="column.dataType === 'boolean'">
94-
<!-- The additional [checked] binding is needed as after the initial call of the checked getter
95-
it is never called again as no change detection is triggered in the grid after that, resulting in wrong
96-
rendered state of the checkbox. -->
9794
<igx-checkbox
98-
[(ngModel)]="editValue"
9995
[checked]="editValue"
96+
(change)="editValue = $event.checked"
10097
[igxFocus]="true"
10198
[disableRipple]="true"
10299
></igx-checkbox>

projects/igniteui-angular/src/lib/switch/switch.component.spec.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Component, ViewChild } from '@angular/core';
22
import { TestBed, waitForAsync } from '@angular/core/testing';
3-
import { FormsModule } from '@angular/forms';
3+
import { FormBuilder, FormsModule, ReactiveFormsModule } from '@angular/forms';
44
import { By } from '@angular/platform-browser';
55
import { IgxRippleModule } from '../directives/ripple/ripple.directive';
66
import { IgxSwitchComponent } from './switch.component';
@@ -19,9 +19,10 @@ describe('IgxSwitch', () => {
1919
SwitchDisabledComponent,
2020
SwitchExternalLabelComponent,
2121
SwitchInvisibleLabelComponent,
22+
SwitchFormGroupComponent,
2223
IgxSwitchComponent
2324
],
24-
imports: [FormsModule, IgxRippleModule, NoopAnimationsModule]
25+
imports: [FormsModule, ReactiveFormsModule, IgxRippleModule, NoopAnimationsModule]
2526
})
2627
.compileComponents();
2728
}));
@@ -68,7 +69,7 @@ describe('IgxSwitch', () => {
6869
fixture.detectChanges();
6970

7071
expect(nativeCheckbox.checked).toBe(false);
71-
expect(switchInstance.checked).toBe(false);
72+
expect(switchInstance.checked).toBe(null);
7273

7374
testInstance.subscribed = true;
7475
switchInstance.name = 'my-switch';
@@ -79,6 +80,22 @@ describe('IgxSwitch', () => {
7980
expect(switchInstance.name).toEqual('my-switch');
8081
});
8182

83+
it('Initializes with form group', () => {
84+
const fixture = TestBed.createComponent(SwitchFormGroupComponent);
85+
fixture.detectChanges();
86+
87+
const testInstance = fixture.componentInstance;
88+
const switchInstance = testInstance.switch;
89+
const form = testInstance.myForm;
90+
91+
form.setValue({ switch: true });
92+
expect(switchInstance.checked).toBe(true);
93+
94+
form.reset();
95+
96+
expect(switchInstance.checked).toBe(null);
97+
});
98+
8299
it('Initializes with external label', () => {
83100
const fixture = TestBed.createComponent(SwitchExternalLabelComponent);
84101
const switchInstance = fixture.componentInstance.switch;
@@ -149,7 +166,7 @@ describe('IgxSwitch', () => {
149166
fixture.detectChanges();
150167

151168
// Should not update
152-
expect(switchInstance.checked).toBe(false);
169+
expect(switchInstance.checked).toBe(null);
153170
expect(testInstance.subscribed).toBe(false);
154171
});
155172

@@ -256,3 +273,14 @@ class SwitchInvisibleLabelComponent {
256273
@ViewChild('switch', { static: true }) public switch: IgxSwitchComponent;
257274
public label = 'Invisible Label';
258275
}
276+
277+
@Component({
278+
template: `<form [formGroup]="myForm"><igx-switch #switch formControlName="switch">Form Group</igx-switch></form>`
279+
})
280+
class SwitchFormGroupComponent {
281+
@ViewChild('switch', { static: true }) public switch: IgxSwitchComponent;
282+
283+
public myForm = this.fb.group({ switch: [] });
284+
285+
constructor(private fb: FormBuilder) {}
286+
}

projects/igniteui-angular/src/lib/switch/switch.component.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,7 @@ export class IgxSwitchComponent implements ControlValueAccessor, EditorProvider
337337
* @internal
338338
*/
339339
public writeValue(value: boolean) {
340-
if (typeof value === 'boolean') {
341-
this._checked = value;
342-
}
340+
this._checked = value;
343341
}
344342

345343
/**

src/app/input/input.sample.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ <h4 class="sample-title">Checkbox sample</h4>
122122
<span igxListLineTitle>Brian Vaughan</span>
123123
<igx-checkbox igxListAction [(ngModel)]="user.registered" [indeterminate]="true" [disabled]="true">Value</igx-checkbox>
124124
</igx-list-item>
125+
126+
<igx-list-item>
127+
<form [formGroup]="myForm">
128+
<igx-checkbox formControlName="checkbox">Form Group Checkbox</igx-checkbox>
129+
<igx-switch formControlName="switch">Form Group Switch</igx-switch>
130+
</form>
131+
<button igxButton (click)="myForm.reset()">Reset Form Group</button>
132+
</igx-list-item>
125133
</igx-list>
126134
</div>
127135
</article>

src/app/input/input.sample.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, ChangeDetectionStrategy } from '@angular/core';
2+
import { FormBuilder } from '@angular/forms';
23
import { IChangeRadioEventArgs } from 'igniteui-angular';
34

45
@Component({
@@ -12,6 +13,11 @@ export class InputSampleComponent {
1213
public selected = 'option1';
1314
public airplaneMode = false;
1415

16+
public myForm = this.fb.group({
17+
checkbox: [],
18+
switch: []
19+
});
20+
1521
public user = {
1622
comment: '',
1723
firstName: 'John',
@@ -50,6 +56,8 @@ export class InputSampleComponent {
5056
disabled: true
5157
}];
5258

59+
constructor(private fb: FormBuilder) {}
60+
5361
public onClick(event: MouseEvent) {
5462
console.log(event);
5563
}

0 commit comments

Comments
 (0)