Skip to content

Commit 371cd31

Browse files
committed
feat: added custom values for checked and unchecked states
1 parent e573563 commit 371cd31

File tree

7 files changed

+72
-12
lines changed

7 files changed

+72
-12
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Use
5858
| name | `string` | `undefined` | Name to attach to the generated input field |
5959
| fontSize | `number` | `10` | Font size in pixels |
6060
| fontColor | `string Object` | `undefined` | If `string` - color when checked <br>If `Object` - colors for labels when checked/uncheked <br>Example: `{checked: '#25EF02', unchecked: '#35DB15'}` by default the text color is white.|
61+
| values | `{checked: any, unchecked: any}` | `{checked: true, unchecked: false}` | Values for checked and unchecked states, by default checked value is `true` and unchecked value is `false` <br>Example: `{checked: 1, unchecked: 0}`.|
6162

6263
### Outputs events
6364

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "ng-toggle-button",
33
"description": "A toggle button component",
44
"author": "vicmans",
5-
"version": "0.1.5",
5+
"version": "0.1.6",
66
"homepage": "https://github.com/vicmans/ng-toggle-button#readme",
77
"repository": {
88
"type": "git",

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<nav class="navbar" role="navigation" aria-label="main navigation">
22
<div class="navbar-brand">
3-
<a class="navbar-item" href="https://vicmans.com">
4-
<img src="https://vicmans.com/v.png" width="28" height="28"> Victor
3+
<a class="navbar-item" href="http://vicmans.com">
4+
<img src="http://vicmans.com/v.png" width="28" height="28"> Victor
55
</a>
66
</div>
77
<a class="button is-light" href="https://github.com/vicmans/ng-toggle-button">
@@ -19,7 +19,7 @@ <h2 class="subtitle">Examples:</h2>
1919
></ng-toggle>
2020

2121
<h2 class="subtitle">
22-
ngModel
22+
Custom
2323
</h2>
2424
<ng-toggle
2525
[(ngModel)]="prueba"
@@ -76,6 +76,16 @@ <h2 class="subtitle">
7676
[switchColor]="{checked: '#00a388', unchecked: '#BFCBD9'}"
7777
></ng-toggle>
7878
<br>
79+
<h2 class="subtitle">
80+
Custom values
81+
</h2>
82+
<ng-toggle
83+
[(ngModel)]="custom"
84+
[labels]="{checked: 'yes', unchecked: 'no'}"
85+
[values]="{checked: 'si', unchecked: 'no'}"
86+
[width]="55"
87+
></ng-toggle>
88+
<p class="is-family-code">ngModel: {{custom}}</p>
7989
<h2 class="subtitle">
8090
Configure
8191
</h2>
@@ -92,6 +102,7 @@ <h2 class="subtitle">
92102
[switchColor]="config.switchColor"
93103
[fontColor]="config.fontColor"
94104
[fontSize]="config.fontSize"
105+
[values]="config.values"
95106
(change)="changeEvent()"
96107
></ng-toggle>
97108
<br>
@@ -106,6 +117,7 @@ <h2 class="subtitle">
106117
[labels]="{{config.labels|json}}"
107118
[color]="{{config.color|json}}"
108119
[switchColor]="{{config.switchColor|json}}"
120+
[values]="{{config.values|json}}"
109121
[fontColor]="{{config.fontColor|json}}">
110122
&lt;/ng-toggle>
111123
</pre>
@@ -190,6 +202,29 @@ <h2 class="subtitle">
190202
</div>
191203
</div>
192204

205+
<div class="field is-horizontal">
206+
<div class="field-label">
207+
<label class="label">checked value: </label>
208+
</div>
209+
<div class="field-body">
210+
<div class="control">
211+
<input class="input" type="text" [(ngModel)]="config.values.checked">
212+
</div>
213+
</div>
214+
</div>
215+
216+
<div class="field is-horizontal">
217+
<div class="field-label">
218+
<label class="label">unchecked value: </label>
219+
</div>
220+
<div class="field-body">
221+
<div class="control">
222+
<input class="input" type="text" [(ngModel)]="config.values.unchecked">
223+
</div>
224+
</div>
225+
</div>
226+
227+
193228
<div class="field is-horizontal">
194229
<div class="field-label">
195230
<label class="label">checked color: </label>

projects/demo/src/app/app.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import { FormGroup, FormControl } from '@angular/forms';
88
})
99
export class AppComponent {
1010
title = 'ng-toggle-button';
11-
prueba = false
11+
prueba = true
12+
custom = 'si'
1213

1314
config = {
14-
value: true,
15+
value: 1,
1516
name: '',
1617
disabled: false,
1718
height: 25,
@@ -28,6 +29,10 @@ export class AppComponent {
2829
unchecked: 'off',
2930
checked: 'on',
3031
},
32+
values: {
33+
unchecked: 0,
34+
checked: 1,
35+
},
3136
checkedLabel: '',
3237
uncheckedLabel: '',
3338
fontColor: {checked: '#fafafa', unchecked: '#f45a32'}

projects/ng-toggle/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Use
5858
| name | `string` | `undefined` | Name to attach to the generated input field |
5959
| fontSize | `number` | `10` | Font size in pixels |
6060
| fontColor | `string Object` | `undefined` | If `string` - color when checked <br>If `Object` - colors for labels when checked/uncheked <br>Example: `{checked: '#25EF02', unchecked: '#35DB15'}` by default the text color is white.|
61+
| values | `{checked: any, unchecked: any}` | `{checked: true, unchecked: false}` | Values for checked and unchecked states, by default checked value is `true` and unchecked value is `false` <br>Example: `{checked: 1, unchecked: 0}`.|
6162

6263
### Outputs events
6364

projects/ng-toggle/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ng-toggle-button",
3-
"version": "0.1.5",
3+
"version": "0.1.6",
44
"description": "Angular toggle button switch",
55
"author": "vicmans",
66
"license": "MIT",

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export class NgToggleComponent implements OnInit, ControlValueAccessor {
3838
@Input() checkedLabel: string = ''
3939
@Input() uncheckedLabel: string = ''
4040
@Input() fontColor: string | toggleConfig
41+
@Input() values: valueConfig = {checked: true, unchecked: false}
4142
@Input('ngModel') model: boolean
4243
cssColors: boolean = false
4344

@@ -47,7 +48,7 @@ export class NgToggleComponent implements OnInit, ControlValueAccessor {
4748
constructor() { }
4849

4950
ngOnInit() {
50-
this.toggled = this.model !== undefined ? this.model : this.value
51+
this.setToogle()
5152
}
5253

5354
onChange = (_:any) => { }
@@ -61,9 +62,9 @@ export class NgToggleComponent implements OnInit, ControlValueAccessor {
6162

6263
writeValue(value: any): void {
6364
if (value) {
64-
this.value = value || false;
65+
this.value = value == true ? this.values.checked : this.values.unchecked;
6566
} else {
66-
this.value = false;
67+
this.value = this.values.unchecked;
6768
}
6869
}
6970
registerOnChange(fn: any): void {
@@ -76,6 +77,13 @@ export class NgToggleComponent implements OnInit, ControlValueAccessor {
7677
this.disabled = isDisabled;
7778
}
7879

80+
setToogle() {
81+
const value = this.model !== undefined ? this.model : this.value
82+
let index = Object.values(this.values).findIndex(el => el == value)
83+
if(index > -1)
84+
this.toggled = Object.keys(this.values)[index] == 'checked' ? true : false
85+
}
86+
7987
get coreStyle () {
8088
return {
8189
width: px(this.width),
@@ -177,10 +185,15 @@ export class NgToggleComponent implements OnInit, ControlValueAccessor {
177185
const toggled = !this.toggled;
178186
this.toggled = toggled;
179187

180-
this.value = toggled;
188+
this.value = this.getValue(toggled)
181189
this.onTouch();
182-
this.onChange(toggled);
190+
this.onChange(this.value);
191+
}
192+
193+
getValue(key: boolean) {
194+
return key === true ? this.values['checked'] : this.values['unchecked']
183195
}
196+
184197
}
185198

186199
export const isObject = (value) => {
@@ -206,4 +219,9 @@ export const translate = (x, y) => {
206219
export type toggleConfig = {
207220
checked: string,
208221
unchecked: string
222+
}
223+
224+
export type valueConfig = {
225+
checked: any,
226+
unchecked: any
209227
}

0 commit comments

Comments
 (0)