Skip to content

Commit 27e39e7

Browse files
committed
adding json model to application, the objective is reduce the html code and transfer to typescript component all configurations.
1 parent f9f9cc1 commit 27e39e7

8 files changed

+101
-65
lines changed

ng-package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
{
2-
"$schema": "./node_modules/ng-packagr/ng-package.schema.json",
3-
"lib": {
4-
"entryFile": "public_api.ts"
5-
}
1+
{
2+
"$schema": "./node_modules/ng-packagr/ng-package.schema.json",
3+
"lib": {
4+
"entryFile": "public_api.ts"
5+
}
66
}

src/app/app.component.css

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
.container {
2-
margin: 25px;
3-
}
4-
5-
pre {
6-
display: block;
7-
padding: 9.5px;
8-
margin: 10px 0;
9-
font-size: 13px;
10-
line-height: 1.42857143;
11-
color: #333;
12-
word-break: break-all;
13-
word-wrap: break-word;
14-
background-color: #f5f5f5;
15-
border: 1px solid #ccc;
16-
border-radius: 4px;
1+
.container {
2+
margin: 25px;
3+
}
4+
5+
pre {
6+
display: block;
7+
padding: 9.5px;
8+
margin: 10px 0;
9+
font-size: 13px;
10+
line-height: 1.42857143;
11+
color: #333;
12+
word-break: break-all;
13+
word-wrap: break-word;
14+
background-color: #f5f5f5;
15+
border: 1px solid #ccc;
16+
border-radius: 4px;
1717
}

src/app/app.component.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
<h2>Basic Configuration</h2>
44

5-
<ngx-checkbox [label]="'Checkbox 1'" [color]="'p-success'" [rounded]="true" [(ngModel)]="isSelected" name="isSelected"></ngx-checkbox>
5+
<ngx-checkbox [configuration]="jsonModel" [(ngModel)]="isSelected" name="isSelected"></ngx-checkbox>
66
<pre class="boolean">{{ isSelected }}</pre>
77
<b>Code:</b>
88
<pre><![CDATA[<ngx-checkbox [label]="'Checkbox 1'" [color]="'p-success'" [rounded]="true" [(ngModel)]="isSelected" name="isSelected"></ngx-checkbox>]]></pre>
99

10-
<h4>Colors</h4>
10+
<!-- <h4>Colors</h4>
1111
<ngx-checkbox [label]="'p-primary'" [color]="'p-primary'" [rounded]="true" [(ngModel)]="isSelected" name="isSelected"></ngx-checkbox>
1212
1313
<ngx-checkbox [label]="'p-success'" [color]="'p-success'" [rounded]="true" [(ngModel)]="isSelected" name="isSelected"></ngx-checkbox>
@@ -36,5 +36,5 @@ <h4>Icons and Colors</h4>
3636
<br>
3737
<b>Code:</b>
3838
<pre><![CDATA[ <ngx-checkbox [label]="'Font-Awesome'" [colorHex]="'#18CDCA'" [colorInside]="'#FFF'" [rounded]="true" [icon]="'fa fa-check'" [(ngModel)]="isSelected2" name="isSelected2"></ngx-checkbox>]]></pre>
39-
<pre><![CDATA[ <ngx-checkbox [label]="'Font-Awesome'" [colorHex]="'#FF302C'" [colorInside]="'#FFF'" [rounded]="false" [icon]="'fa fa-times'" [(ngModel)]="isSelected2" name="isSelected2"></ngx-checkbox>]]></pre>
39+
<pre><![CDATA[ <ngx-checkbox [label]="'Font-Awesome'" [colorHex]="'#FF302C'" [colorInside]="'#FFF'" [rounded]="false" [icon]="'fa fa-times'" [(ngModel)]="isSelected2" name="isSelected2"></ngx-checkbox>]]></pre> -->
4040
</div>

src/app/app.component.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
import { Component } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
2+
import { CustomCheckBoxModel } from './custom-checkbox/custom-checkbox.model';
23

34
@Component({
45
selector: 'app-root',
56
templateUrl: './app.component.html',
67
styleUrls: ['./app.component.css']
78
})
8-
export class AppComponent {
9+
10+
export class AppComponent implements OnInit {
911
title = 'app';
1012
isSelected: boolean = true;
1113
isSelected2: boolean = true;
14+
jsonModel: CustomCheckBoxModel;
15+
16+
ngOnInit() {
17+
this.jsonModel = new CustomCheckBoxModel('Test', 'fa fa-check', true, 'p-success', null, '#FFF');
18+
}
1219
}

src/app/custom-checkbox/custom-checkbox.component.css

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div (click)="toggleCheckbox()" [class]="styleCheckBox">
22
<input [value]="isSelected" type="checkbox" [checked]="isSelected" />
33
<div [class]="styleColor">
4-
<i [class]="this.styleIcon" [style.background]="colorHex" [style.color]="colorInside"></i>
4+
<i [class]="styleIcon" [style.background]="configuration.colorHex" [style.color]="configuration.colorInside"></i>
55
<label>{{ label }}</label>
66
</div>
77
</div>

src/app/custom-checkbox/custom-checkbox.component.ts

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,77 @@
1-
import { Component, OnInit, Input, ViewChild, forwardRef } from '@angular/core';
2-
import { NG_VALUE_ACCESSOR, NgModel, ControlValueAccessor } from '@angular/forms';
1+
import {
2+
Component,
3+
OnInit,
4+
Input,
5+
ViewChild,
6+
forwardRef
7+
} from '@angular/core';
8+
import {
9+
NG_VALUE_ACCESSOR,
10+
NgModel,
11+
ControlValueAccessor
12+
} from '@angular/forms';
13+
import {
14+
CustomCheckBoxModel
15+
} from './custom-checkbox.model';
316

417
@Component({
518
selector: 'ngx-checkbox',
619
templateUrl: './custom-checkbox.component.html',
720
styleUrls: ['./custom-checkbox.component.css'],
8-
providers: [
9-
{ provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => CustomCheckboxComponent), multi: true }
10-
]
21+
providers: [{
22+
provide: NG_VALUE_ACCESSOR,
23+
useExisting: forwardRef(() => CustomCheckboxComponent),
24+
multi: true
25+
}]
1126
})
1227
export class CustomCheckboxComponent implements OnInit, ControlValueAccessor {
1328

1429
writeValue(obj: any): void {
1530
this.isSelected = obj;
16-
console.log(obj);
1731
}
1832
registerOnChange(fn: any): void {
1933
this._onChange = fn;
2034
}
2135
registerOnTouched(fn: any): void {
2236
this._onTouched = fn;
2337
}
24-
setDisabledState?(isDisabled: boolean): void {
25-
}
38+
setDisabledState ? (isDisabled: boolean) : void {}
2639

2740
// call if value was changed inside our component
28-
private _onChange = (_: any) => { };
41+
private _onChange = (_: any) => {};
2942
// call if input was "touched" .. !
30-
private _onTouched = () => { };
43+
private _onTouched = () => {};
3144

32-
@Input() label: String;
33-
@Input() icon: string;
34-
@Input() rounded?: boolean = false;
35-
@Input() color?: string;
36-
@Input() colorHex?: string;
37-
@Input() colorInside?: string;
45+
// @Input() label: String;
46+
// @Input() icon: string;
47+
// @Input() rounded ? : boolean = false;
48+
// @Input() color ? : string;
49+
// @Input() colorHex ? : string;
50+
// @Input() colorInside ? : string;
51+
@Input() configuration: CustomCheckBoxModel;
3852

3953
isSelected: boolean;
4054
styleCheckBox: String;
4155
styleColor: String;
4256
styleIcon: String;
43-
constructor() { }
57+
constructor() {}
4458

4559
ngOnInit() {
46-
60+
console.log(this.configuration);
4761
// STYLE CHECKBOX
4862
this.styleCheckBox = 'pretty p-icon p-smooth';
49-
if (this.rounded) this.styleCheckBox += ' p-round';
50-
//if (this.curved) this.styleCheckBox += ' p-curve';
51-
63+
64+
if (this.configuration.rounded) this.styleCheckBox += ' p-round';
65+
5266
// COLORS CHECKBOX
5367
this.styleColor = 'state ';
54-
if (this.color) this.styleColor += this.color;
55-
68+
if (this.configuration.color) this.styleColor = `${this.styleColor}${this.configuration.color}`;
69+
5670
this.styleIcon = 'icon ';
57-
if(this.icon) this.styleIcon += this.icon;
58-
71+
if (this.configuration.icon) this.styleIcon = `${this.styleIcon}${this.configuration.icon}`;
5972
}
6073

61-
ngAfterViewInit() {
62-
}
74+
ngAfterViewInit() {}
6375

6476
toggleCheckbox() {
6577
this.isSelected = !this.isSelected;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export class CustomCheckBoxModel {
2+
label: String;
3+
icon: String;
4+
rounded?: boolean = false;
5+
color?: String;
6+
colorHex?: String;
7+
colorInside?: String;
8+
9+
constructor(label: String, icon: String, rounded?: boolean, color?: String, colorHex?: String, colorInside?: String) {
10+
this.label = label;
11+
this.icon = icon;
12+
this.rounded = rounded;
13+
this.color = color;
14+
this.colorHex = colorHex;
15+
this.colorInside = colorInside;
16+
}
17+
}

0 commit comments

Comments
 (0)