Skip to content

Commit

Permalink
feat(checkbox): enable checkbox global configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmerckx committed May 11, 2018
1 parent 13a9b77 commit 5e4d7a7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/app/demo/checkbox-demo/checkbox-demo.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { RouterModule } from '@angular/router';
import { CheckboxDemoComponent } from './checkbox-demo.component';
import { UiModule } from '../../ui/ui.module';
import { FormsModule } from '@angular/forms';
import { IW_CHECKBOX_CONFIG } from '../../ui/checkbox/checkbox.config';
import { CheckboxConfig } from '../../ui/checkbox/checkbox-config.interface';

@NgModule({
imports: [
Expand All @@ -15,6 +17,12 @@ import { FormsModule } from '@angular/forms';
FormsModule
],
declarations: [CheckboxDemoComponent],
exports: [RouterModule]
exports: [RouterModule],
providers: [{
provide: IW_CHECKBOX_CONFIG,
useValue: <CheckboxConfig> {
containerClass: 'demo-checkbox'
}
}]
})
export class CheckboxDemoModule { }
4 changes: 4 additions & 0 deletions src/app/ui/checkbox/checkbox-config.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export interface CheckboxConfig {
containerClass?: string;
}
4 changes: 4 additions & 0 deletions src/app/ui/checkbox/checkbox.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { InjectionToken } from '@angular/core';
import { CheckboxConfig } from './checkbox-config.interface';

export const IW_CHECKBOX_CONFIG = new InjectionToken<CheckboxConfig>('iw-checkbox.config');
22 changes: 20 additions & 2 deletions src/app/ui/checkbox/checkbox/checkbox.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ import {
HostListener,
HostBinding,
Input,
ChangeDetectorRef
ChangeDetectorRef,
Optional,
Inject,
Renderer,
ElementRef
} from '@angular/core';
import {
ControlValueAccessor,
NG_VALUE_ACCESSOR
} from '@angular/forms';
import { CheckboxConfig } from '../checkbox-config.interface';
import { IW_CHECKBOX_CONFIG } from '../checkbox.config';

@Component({
selector: 'iw-checkbox',
Expand All @@ -36,11 +42,23 @@ export class CheckboxComponent implements OnInit, ControlValueAccessor {
@HostBinding('class.checkbox--disabled')
isDisabled: boolean;

constructor(private changeDetectorRef: ChangeDetectorRef) {
constructor(
private renderer: Renderer,
private elementRef: ElementRef,
private changeDetectorRef: ChangeDetectorRef,
@Optional() @Inject(IW_CHECKBOX_CONFIG) private checkboxConfig: CheckboxConfig
) {
this.onChangeCb = this.onTouchedCb = () => {};
}

ngOnInit() {
if (this.checkboxConfig && this.checkboxConfig.containerClass) {
this.renderer.setElementClass(
this.elementRef.nativeElement,
this.checkboxConfig.containerClass,
true
);
}
}

onKeyup($event: KeyboardEvent) {
Expand Down

0 comments on commit 5e4d7a7

Please sign in to comment.