Skip to content

Commit

Permalink
added corys custom control and increased angular version
Browse files Browse the repository at this point in the history
  • Loading branch information
Magnus Gudmundsspn committed Nov 14, 2016
1 parent 3459133 commit 15f83ae
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{


"typescript.tsdk": "C:\\Users\\Magnus\\node_modules\\typescript\\lib",
"typescript.tsdk": "/usr/local/lib/node_modules/typescript/lib",


"files.exclude": {
Expand Down
2 changes: 2 additions & 0 deletions forms/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {Form2Component} from './form2.component';
import {Form3Component} from './form3.component';
import {Form4Component} from './form4.component';
import {Form5Component} from './form5.component';
import {SwitchComponent} from './appswitch.component';
import {BananaboxComponent} from './bananabox.component';


Expand All @@ -22,6 +23,7 @@ import { ControlMessages } from './control-messages.component';
Form4Component,
Form5Component,
BananaboxComponent,
SwitchComponent,
ControlMessages ],
bootstrap: [AppComponent]
})
Expand Down
83 changes: 83 additions & 0 deletions forms/app/appswitch.component.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
.switch {
position: relative;
display: inline-block;
vertical-align: top;
width: 80px;
height: 30px;
padding: 7px;
background-color: none;
cursor: pointer;
overflow: visible;
}

.switch-input {
position: absolute;
top: 0;
left: 0;
opacity: 0;
}

.switch-label {
position: relative;
display: block;
height: inherit;
font-size: 16px;
font-weight: normal;
text-transform: uppercase;
background: #ccc;
border-radius: 4px;
transition: 0.15s ease-out;
transition-property: opacity background;
color: #2d2d2d;
}

.switch-label::before, .switch-label::after {
position: absolute;
top: 50%;
margin-top: -.5em;
line-height: 1;
transition: inherit;
}

.switch-label::before {
content: attr(data-off);
right: 11px;
color: #fff;
}

.switch-label::after {
content: attr(data-on);
left: 11px;
color: #fff;
opacity: 0;
}

.switch-handle {
position: absolute;
top: 9px;
left: 12px;
width: 18px;
height: 18px;
background: #fff;
border-radius: 10px;
box-shadow: 1px 1px 5px #2d2d2d;
transition: left 0.15s ease-out;
}

.checked .switch-label {
background: #4CAF50;
box-shadow: inset 0 1px 2px rgba(255, 255, 255, 0.5), inset 0 0 3px rgba(0, 0, 0, 0.15);
}

.checked .switch-label::before {
opacity: 0;
}

.checked .switch-label::after {
opacity: 1;
}

.checked .switch-handle {
left: 50px;
box-shadow: -1px 1px 5px rgba(45, 45, 45, 0.41);
}
5 changes: 5 additions & 0 deletions forms/app/appswitch.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div (click)="switch()" class="switch" [ngClass]="{ 'checked': value }" [attr.title]="label">
<input type="checkbox" class="switch-input" [value]="value" [attr.checked]="value" [attr.aria-label]="label">
<span class="switch-label" data-on="On" data-off="Off"></span>
<span class="switch-handle"></span>
</div>
54 changes: 54 additions & 0 deletions forms/app/appswitch.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

import { Component, Input, forwardRef } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';

@Component({
selector: 'app-switch',
templateUrl: 'app/appswitch.component.html',
styleUrls: ['app/appswitch.component.css'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => SwitchComponent),
multi: true
}
]
})
export class SwitchComponent implements ControlValueAccessor {
@Input() label = 'switch';
@Input('value') _value = false;
onChange: any = () => { };
onTouched: any = () => { };

get value() {
return this._value;
}

set value(val) {
this._value = val;
this.onChange(val);
this.onTouched();
}

constructor() { }

registerOnChange(fn:any) {
this.onChange = fn;
}

registerOnTouched(fn:any) {
this.onTouched = fn;
}

writeValue(value:any) {
if (value) {
this.value = value;
}
}

switch() {
this.value = !this.value;
}
}


8 changes: 5 additions & 3 deletions forms/app/form5.component.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<div class="container">
<p>Form 5 -Observing changes, listening in on valueChanges</p>

<p>Form 5 -Observing changes, listening in on valueChanges, added Corys control</p>
<p>see : <a href="https://coryrylan.com/blog/angular-custom-form-controls-with-reactive-forms-and-ngmodel">
https://coryrylan.com/blog/angular-custom-form-controls-with-reactive-forms-and-ngmodel</a></p>
<form [formGroup]="userForm" (ngSubmit)="onSubmit()">
<hr>

Expand All @@ -9,7 +10,7 @@
<hr>
<textarea class="form-control" rows="4" placeholder="Description" formControlName="description"></textarea>

<div formGroupName="contact">
<div formGroupName="contact" style="padding-bottom:10px;">
<input type="text" class="form-control" placeholder="email1" formControlName="email1">
<control-messages [control]="userForm.controls.contact.controls.email1"></control-messages>
<input type="text" class="form-control" placeholder="email2" formControlName="email2">
Expand All @@ -18,6 +19,7 @@
<control-messages [control]="userForm.controls.contact.controls.telephone"></control-messages>
<input type="number" class="form-control" placeholder="Alternative telephone" formControlName="telephone2">
<control-messages [control]="userForm.controls.contact.controls.telephone2"></control-messages>
<app-switch label="the switch" formControlName="isHen"></app-switch>
</div>

<button [disabled]="!userForm.valid" class="btn btn-primary">Save</button>
Expand Down
4 changes: 3 additions & 1 deletion forms/app/form5.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {Component, OnInit} from '@angular/core';
import { FormControl, Validators, FormGroup } from '@angular/forms';
import { ControlMessages } from './control-messages.component';
import { ValidationService } from './validation.service';
import {SwitchComponent} from './appswitch.component';


@Component({
Expand Down Expand Up @@ -36,7 +37,8 @@ export class Form5Component implements OnInit {
email1: new FormControl('', [Validators.required, ValidationService.emailValidator]),
email2: new FormControl('', ValidationService.emptyOrEmailValidator),
telephone: new FormControl('', Validators.nullValidator),
telephone2: new FormControl('', Validators.nullValidator)
telephone2: new FormControl('', Validators.nullValidator),
isHen: new FormControl('',Validators.nullValidator )
})
})

Expand Down
36 changes: 20 additions & 16 deletions forms/package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
{
"name": "angular2-workshop-forms",
"version": "0.0.1",
"version": "0.1.1",
"scripts": {
"typings": "typings",
"postinstall": "typings install"
"start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
"lite": "lite-server",
"tsc": "tsc",
"tsc:w": "tsc -w"
},
"license": "ISC",
"dependencies": {
"@angular/common": "2.0.1",
"@angular/compiler": "2.0.1",
"@angular/core": "2.0.1",
"@angular/http": "2.0.1",
"@angular/platform-browser": "2.0.1",
"@angular/platform-browser-dynamic": "2.0.1",
"@angular/forms": "2.0.1",
"@angular/upgrade": "2.0.1",
"core-js": "^2.4.0",
"@angular/common": "2.1.1",
"@angular/compiler": "2.1.1",
"@angular/core": "2.1.1",
"@angular/http": "2.1.1",
"@angular/platform-browser": "2.1.1",
"@angular/platform-browser-dynamic": "2.1.1",
"@angular/forms": "2.1.1",
"@angular/upgrade": "2.1.1",
"core-js": "^2.4.1",
"bootstrap": "^3.3.6",
"reflect-metadata": "0.1.3",
"rxjs": "5.0.0-beta.12",
"systemjs": "0.19.27",
"angular2-in-memory-web-api": "0.0.21",
"angular2-in-memory-web-api": "^0.0.21",
"zone.js": "^0.6.25",
"typings": "^1.3.0"
},
"devDependencies": {
"lite-server": "^2.2.0",
"typescript": "^2.0.3",
"typings": "^1.3.0"
"@types/core-js": "^0.9.34",
"@types/node": "^6.0.45",
"concurrently": "^3.0.0",
"lite-server": "^2.2.2"

}
}

7 changes: 0 additions & 7 deletions forms/typings.json

This file was deleted.

0 comments on commit 15f83ae

Please sign in to comment.