Skip to content

Commit 0c7df23

Browse files
committed
fix updating value input programmatically
1 parent f5f4344 commit 0c7df23

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [1.3.1] - 2022-05-22
5+
### Bug Fixed
6+
- Fix issue updating `value` input property programmatically ([#34](https://github.com/vicmans/ng-toggle-button/issues/34))
7+
48
## [1.3.0] - 2022-03-06
59
### Change
610
- Update to Angular 13

projects/demo/src/app/app.component.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,28 @@ <h2 class="subtitle">
143143
</div>
144144
</div>
145145

146+
<div class="field is-horizontal">
147+
<div class="field-label">
148+
<label class="label" for="disabled">value: </label>
149+
</div>
150+
<div class="field-body">
151+
<div class="control">
152+
<ng-toggle
153+
[(value)]="config.value"
154+
[values]="{
155+
checked: config.values.checked,
156+
unchecked: config.values.unchecked
157+
}"
158+
[labels]="{
159+
checked: config.values.checked,
160+
unchecked: config.values.unchecked
161+
}"
162+
width="80"
163+
></ng-toggle> {{config.value}}
164+
</div>
165+
</div>
166+
</div>
167+
146168
<div class="field is-horizontal">
147169
<div class="field-label">
148170
<label class="label" for="disabled">width: </label>

projects/ng-toggle/src/lib/ng-toggle.component.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@
2929
[ngStyle]="labelStyle"
3030
*ngIf="!toggled"
3131
>
32-
<ng-container slot="unchecked">
33-
{{labelUnchecked}}
34-
</ng-container>
32+
{{labelUnchecked}}
3533
</span>
3634
</ng-container>
3735
</label>

projects/ng-toggle/src/lib/ng-toggle.component.spec.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { SimpleChange } from '@angular/core';
12
import { ComponentFixture, TestBed } from '@angular/core/testing';
23

34
import { NgToggleComponent } from './ng-toggle.component';
@@ -38,9 +39,20 @@ describe('NgToggleComponent', () => {
3839
let label = fixture.debugElement.nativeElement.querySelector('label');
3940
label.click();
4041
fixture.detectChanges();
41-
4242
expect(component.value).toBeFalsy();
4343
})
44+
45+
it('should change the value programmatically via value input', () => {
46+
component.value = false;
47+
component.ngOnChanges({
48+
value: new SimpleChange(true, false, true)
49+
});
50+
fixture.detectChanges();
51+
let label = fixture.nativeElement.querySelector('.ng-toggle-switch-label');
52+
expect(component.value).toBe(false);
53+
expect(component.toggled).toBeFalsy();
54+
expect(label.getAttribute('class')).toContain('ng-toggle-right');
55+
})
4456
});
4557

4658
describe('NgToggleComponent with values forRoot module imports', () => {

projects/ng-toggle/src/lib/ng-toggle.component.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit, Input, forwardRef, Output, EventEmitter } from '@angular/core';
1+
import { Component, OnInit, Input, forwardRef, Output, EventEmitter, OnChanges, SimpleChanges } from '@angular/core';
22
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
33
import { NgToggleConfig } from './ng-toggle.config';
44

@@ -22,7 +22,7 @@ const DISABLED_BUTTON_COLOR = 'silver'
2222
}
2323
]
2424
})
25-
export class NgToggleComponent implements OnInit, ControlValueAccessor {
25+
export class NgToggleComponent implements OnInit, ControlValueAccessor, OnChanges {
2626

2727
@Input() value: any = this.config.value || true
2828
@Input() name: string = this.config.name || ''
@@ -57,6 +57,7 @@ export class NgToggleComponent implements OnInit, ControlValueAccessor {
5757

5858
onInput(value: boolean) {
5959
this.value = value;
60+
console.log(this.value, value, 'calue')
6061
this.onTouch();
6162
this.onChange(this.value);
6263
}
@@ -82,6 +83,14 @@ export class NgToggleComponent implements OnInit, ControlValueAccessor {
8283
this.toggled = Object.keys(this.values)[index] == 'checked' ? true : false
8384
}
8485

86+
ngOnChanges(changes: SimpleChanges) {
87+
for (const propName in changes) {
88+
const chng = changes[propName];
89+
if(propName == 'value')
90+
this.writeValue(chng.currentValue)
91+
}
92+
}
93+
8594
get coreStyle () {
8695
return {
8796
width: px(this.width),

0 commit comments

Comments
 (0)