Skip to content

Commit 6914f8d

Browse files
authored
Merge pull request #821 from dxc-technology/jialecl-defaultCheckedSwitch
Adding defaultChecked to switch
2 parents c97f0e9 + 901fd16 commit 6914f8d

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

projects/dxc-ngx-cdk-site/src/app/components/examples/switch/properties/switch-table-properties/switch-table-properties.component.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
uncontrolled and the value will be managed internally by the component.
1919
</td>
2020
</tr>
21+
<tr>
22+
<td>defaultChecked: boolean</td>
23+
<td><code>false</code></td>
24+
<td>Initial state of the checkbox, only when it is uncontrolled.</td>
25+
</tr>
2126
<tr>
2227
<td>value: any</td>
2328
<td></td>

projects/dxc-ngx-cdk-site/src/app/pages/theme-builder/previews/switch-preview/switch-preview.component.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
<div>
44
<tbuilder-component-mode text="Default">
55
<dxc-switch
6-
[checked]="checked1"
7-
label="Label before"
8-
(onChange)="onChange1($event)"
6+
[defaultChecked]="true"
7+
label="Label before and default checked"
98
margin="xsmall"
109
></dxc-switch>
1110

projects/dxc-ngx-cdk/src/lib/dxc-select/dxc-select.component.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@ import { v4 as uuidv4 } from "uuid";
2424
import { SelectService } from "./services/select.service";
2525
import { VisualOptionFocus } from "./interfaces/visualFocus.interface";
2626
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
27-
import { SelectProperties, Space, Spacing, EmittedValue } from "./dxc-select.types";
27+
import {
28+
SelectProperties,
29+
Space,
30+
Spacing,
31+
EmittedValue,
32+
} from "./dxc-select.types";
2833

2934
@Component({
3035
selector: "dxc-select",
@@ -66,11 +71,14 @@ export class DxcSelectComponent implements OnInit, ControlValueAccessor {
6671
value: string | string[];
6772

6873
/**
69-
* Helper text to be placed above the select.
74+
* Initial value of the select, only when it is uncontrolled.
7075
*/
7176
@Input()
7277
defaultValue: string | string[];
7378

79+
/**
80+
* Helper text to be placed above the select.
81+
*/
7482
@Input()
7583
helperText: string;
7684

@@ -184,14 +192,14 @@ export class DxcSelectComponent implements OnInit, ControlValueAccessor {
184192
});
185193

186194
/**
187-
* This event will be emitted when the user selects an option. An object including the new value (or values)
195+
* This event will be emitted when the user selects an option. An object including the new value (or values)
188196
* and the error (if the value selected is not valid) will be passed to this function.
189197
*/
190198
@Output()
191199
onChange = new EventEmitter<EmittedValue>();
192200

193201
/**
194-
* This event will be emitted when the select loses the focus. An object including the value (or values)
202+
* This event will be emitted when the select loses the focus. An object including the value (or values)
195203
* and the error (if the value selected is not valid) will be passed to this function.
196204
*/
197205
@Output()
@@ -286,7 +294,7 @@ export class DxcSelectComponent implements OnInit, ControlValueAccessor {
286294
...this.defaultInputs.getValue(),
287295
})}`;
288296
this.controlled = this.value || this.value === "" ? true : false;
289-
if (this.defaultValue || this.defaultValue === "" && !this.controlled) {
297+
if (this.defaultValue || (this.defaultValue === "" && !this.controlled)) {
290298
this.value = this.defaultValue;
291299
this.setDefaultValues();
292300
}

projects/dxc-ngx-cdk/src/lib/dxc-switch/dxc-switch.component.spec.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,15 @@ describe("DxcSwitch tests", () => {
2828
componentProperties: {
2929
label: "test-switch",
3030
onChange: { emit: onClickFunction } as any,
31+
defaultChecked: true
3132
},
3233
imports: [MatSlideToggleModule],
3334
});
3435
const input = <HTMLInputElement>dxcSwitch.getByRole("switch");
35-
expect(input.checked).toBeFalsy();
36-
fireEvent.click(dxcSwitch.getByText("test-switch"));
37-
expect(onClickFunction).toHaveBeenCalledWith(true);
3836
expect(input.checked).toBeTruthy();
37+
fireEvent.click(dxcSwitch.getByText("test-switch"));
38+
expect(onClickFunction).toHaveBeenCalledWith(false);
39+
expect(input.checked).toBeFalsy();
3940
});
4041

4142
test("controlled dxc-switch functionality", async () => {

projects/dxc-ngx-cdk/src/lib/dxc-switch/dxc-switch.component.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ export class DxcSwitchComponent implements OnChanges {
3737
this._checked = coerceBooleanProperty(value);
3838
}
3939
private _checked;
40+
/**
41+
* Initial state of the switch, only when it is uncontrolled.
42+
*/
43+
@Input()
44+
get defaultChecked(): boolean {
45+
return this._defaultChecked;
46+
}
47+
set defaultChecked(value: boolean) {
48+
this._defaultChecked = coerceBooleanProperty(value);
49+
}
50+
private _defaultChecked;
4051
/**
4152
* Will be passed to the value attribute of the html input element. When inside a form, this value will be only submitted if the switch is checked.
4253
*/
@@ -136,7 +147,7 @@ export class DxcSwitchComponent implements OnChanges {
136147
}
137148

138149
ngOnInit() {
139-
this.renderedChecked = this.checked;
150+
this.renderedChecked = this.checked || this.defaultChecked;
140151
this.className = `${this.getDynamicStyle(this.defaultInputs.getValue())}`;
141152
}
142153

0 commit comments

Comments
 (0)