Created
March 21, 2017 18:10
-
-
Save indianajone/458bbe258d34e42e722b110662c8a060 to your computer and use it in GitHub Desktop.
Checkbox Component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
checkbox { | |
box-sizing: border-box; | |
display: block; | |
margin-bottom: 16px; | |
white-space: nowrap; | |
cursor: pointer; | |
outline: none; | |
-webkit-user-select: none; | |
-moz-user-select: none; | |
-ms-user-select: none; | |
user-select: none; | |
position: relative; | |
min-width: 20px; | |
min-height: 20px; | |
margin-left: 0; | |
margin-right: 16px; | |
&[disabled] { | |
color: rgba(0, 0, 0, .38); | |
.checkbox-icon { | |
border-color: rgba(0, 0, 0, .38); | |
} | |
} | |
.checkbox-icon { | |
border-color: transparent; | |
} | |
&:not(.checkbox-checked) .checkbox-icon { | |
border-color: rgba(0,0,0,0.54); | |
} | |
} | |
.checkbox { | |
&-container { | |
position: absolute; | |
box-sizing: border-box; | |
display: inline-block; | |
width: 20px; | |
height: 20px; | |
left: 0; | |
right: auto; | |
} | |
&-icon { | |
box-sizing: border-box; | |
-webkit-transition: .24s; | |
transition: .24s; | |
position: absolute; | |
top: 0; | |
left: 0; | |
width: 20px; | |
height: 20px; | |
border-width: 2px; | |
border-style: solid; | |
border-radius: 2px; | |
} | |
&-label { | |
box-sizing: border-box; | |
position: relative; | |
display: inline-block; | |
user-select: none; | |
margin-left: 30px; | |
margin-right: 0; | |
} | |
&-checked { | |
& .checkbox-icon { | |
background-color: rgba(255,82,82,0.87); | |
&:after { | |
border-color: rgba(255,255,255,0.87); | |
box-sizing: border-box; | |
transform: rotate(45deg); | |
position: absolute; | |
left: 4.66667px; | |
top: .22222px; | |
display: table; | |
width: 6.66667px; | |
height: 13.33333px; | |
border-width: 2px; | |
border-style: solid; | |
border-top: 0; | |
border-left: 0; | |
content: ""; | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as angular from 'angular'; | |
/** @ngInject **/ | |
class CheckboxControler implements ng.IComponentController { | |
private ngModelCtrl: ng.INgModelController; | |
constructor( | |
private $element: ng.IRootElementService, | |
private $timeout: ng.ITimeoutService | |
){} | |
$onInit() { | |
this.$element.on('click', (e) => { | |
if (this.$element.attr('disabled')) { | |
e.stopImmediatePropagation(); | |
return; | |
} | |
this.toggle(!this.ngModelCtrl.$viewValue); | |
}); | |
} | |
private toggle(selected: boolean) { | |
if (selected) { | |
this.$element.addClass('checkbox-checked'); | |
this.validate(); | |
} else { | |
this.ngModelCtrl.$setViewValue(selected); | |
this.$element.removeClass('checkbox-checked'); | |
} | |
} | |
private async validate() { | |
let result = await this.fakePromise(); | |
if (!result) { | |
alert('Ahh Haa!'); | |
this.$element.removeClass('checkbox-checked'); | |
} else { | |
this.ngModelCtrl.$setViewValue(result); | |
} | |
} | |
protected fakePromise(): Promise<boolean> { | |
return new Promise( resolve => { | |
let result = !! Math.floor(Math.random() * 2); | |
console.log('result is', result); | |
this.$timeout(() => resolve(result), 1000); | |
}); | |
} | |
} | |
let checkbox: ng.IComponentOptions = { | |
template: ` | |
<div class="checkbox-container"> | |
<div class="checkbox-icon"></div> | |
<div class="checkbox-label"> | |
<span ng-transclude></span> | |
</div> | |
</div> | |
`, | |
controller: CheckboxControler, | |
transclude: true, | |
require: { | |
'ngModelCtrl': 'ngModel' | |
} | |
} | |
export default angular.module('checkbox', []) | |
.component('checkbox', checkbox) | |
.name; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<checkbox ng-model="$ctrl.checked"> | |
Checkbox1: {{ $ctrl.checked }} | |
</checkbox> | |
<checkbox ng-model="$ctrl.checked" ng-disabled="!$ctrl.checked"> | |
Checkbox2: {{ $ctrl.checked }} | |
</checkbox> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment