-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(design): pushed CVA implementation to a directive
- Loading branch information
1 parent
a4aee6c
commit 4ca62c6
Showing
10 changed files
with
368 additions
and
55 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
libs/design/src/atoms/form/radio/cva/radio-cva.directive.spec.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { Component } from '@angular/core'; | ||
import { ComponentFixture, async, TestBed } from '@angular/core/testing'; | ||
import { ReactiveFormsModule, FormControl } from '@angular/forms'; | ||
import { By } from '@angular/platform-browser'; | ||
|
||
import { DaffRadioComponent } from '../radio.component'; | ||
import { DaffRadioModule } from '../radio.module'; | ||
|
||
|
||
|
||
@Component({ | ||
template: ` | ||
<daff-radio name='test' value='testValue' [formControl]='radio'></daff-radio> | ||
` | ||
}) | ||
class RadioWrapperComponent { | ||
radio = new FormControl() | ||
} | ||
|
||
describe('DaffRadioControlValueAccessorDirective', () => { | ||
|
||
describe('with the directive', () => { | ||
let radioWrapper: RadioWrapperComponent; | ||
|
||
let component: DaffRadioComponent; | ||
|
||
let fixture: ComponentFixture<RadioWrapperComponent>; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ | ||
RadioWrapperComponent, | ||
], | ||
imports: [ | ||
ReactiveFormsModule, | ||
DaffRadioModule | ||
] | ||
}) | ||
.compileComponents(); | ||
})); | ||
describe('the DaffRadioComponent', () => { | ||
|
||
beforeEach(() => { | ||
|
||
fixture = TestBed.createComponent(RadioWrapperComponent); | ||
radioWrapper = fixture.componentInstance; | ||
component = fixture.debugElement.query(By.css('daff-radio')).componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
it('has the writeValue function for formControls', async () => { | ||
|
||
expect(component.checked).toEqual(false); | ||
|
||
radioWrapper.radio.setValue('testValue'); | ||
|
||
expect(component.checked).toEqual(true); | ||
}); | ||
}); | ||
|
||
}); | ||
describe('without the directive', () => { | ||
let radioWrapper: RadioWrapperComponent; | ||
|
||
let component: DaffRadioComponent; | ||
|
||
let fixture: ComponentFixture<RadioWrapperComponent>; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ | ||
RadioWrapperComponent, | ||
DaffRadioComponent | ||
], | ||
imports: [ | ||
ReactiveFormsModule, | ||
] | ||
}) | ||
.compileComponents(); | ||
})); | ||
describe('the DaffRadioComponent', () => { | ||
|
||
it('throws an error without the directive to give the itself the CVA interface', async () => { | ||
fixture = TestBed.createComponent(RadioWrapperComponent); | ||
radioWrapper = fixture.componentInstance; | ||
expect(() => { | ||
component = fixture.debugElement.query(By.css('daff-radio')).componentInstance; | ||
fixture.detectChanges(); | ||
}).toThrowError() | ||
}); | ||
|
||
}); | ||
}); | ||
}); |
94 changes: 94 additions & 0 deletions
94
libs/design/src/atoms/form/radio/cva/radio-cva.directive.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Directive, Input, OnInit, Self, Optional } from '@angular/core'; | ||
import { NgControl, ControlValueAccessor } from '@angular/forms'; | ||
import { DaffRadioComponent } from '../radio.component'; | ||
import { DaffRadioRegistry } from '../registry/radio-registry'; | ||
|
||
/** | ||
* ControlValueAccessor functionality for the DaffRadio | ||
*/ | ||
@Directive({ | ||
// tslint:disable-next-line: directive-selector | ||
selector: 'daff-radio[ngModel], daff-radio[formControl], daff-radio[formControlName]' | ||
}) | ||
export class DaffRadioControlValueAccessorDirective implements OnInit, ControlValueAccessor { | ||
_onChange: () => void; | ||
_onTouched: () => void; | ||
|
||
/** | ||
* The value of the ControlValueAccessor | ||
*/ | ||
@Input() value: any; | ||
|
||
/** | ||
* The name of the ControlValueAccessor | ||
*/ | ||
@Input() name: string; | ||
|
||
constructor( | ||
@Optional() @Self() public _control: NgControl, | ||
private _registry: DaffRadioRegistry, | ||
private _radio: DaffRadioComponent | ||
) { | ||
if (this._control != null) { | ||
this._control.valueAccessor = this; | ||
} | ||
} | ||
|
||
ngOnInit(): void { | ||
this.writeValue(this._control.value); | ||
this._registry.add(this._control, this); | ||
|
||
this._radio.selectionChange.subscribe( | ||
value => value ? this._onChange() : null | ||
); | ||
} | ||
/** | ||
* | ||
* writeValue function from the CVA interface | ||
*/ | ||
writeValue(value: any): void { | ||
console.log(value); | ||
if (this.value === value) { | ||
this._onChange(); | ||
this.fireSelect(); | ||
} | ||
} | ||
|
||
/** | ||
* registerOnChange implemented from the CVA interface | ||
*/ | ||
registerOnChange(fn: any): void { | ||
this._onChange = () => { | ||
fn(this.value); | ||
this._registry.select(this); | ||
}; | ||
} | ||
|
||
/** | ||
* registerOnTouch implemented from the CVA interface | ||
*/ | ||
registerOnTouched(fn: any): void { | ||
this._onTouched = fn; | ||
} | ||
|
||
/** | ||
* sets the disabled state. | ||
*/ | ||
setDisabledState?(isDisabled: boolean): void { | ||
this._radio.disabled = isDisabled; | ||
} | ||
|
||
/** | ||
calls select function for the radio | ||
*/ | ||
fireSelect() { | ||
this._radio.select(); | ||
} | ||
|
||
/** | ||
calls deselect function for the radio | ||
*/ | ||
fireDeselect() { | ||
this._radio.deselect(); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
17 changes: 17 additions & 0 deletions
17
libs/design/src/atoms/form/radio/registry/radio-registry.spec.ts
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { DaffRadioRegistry } from './radio-registry'; | ||
import { DaffRadioModule } from '../radio.module'; | ||
|
||
describe('DaffRadioRegistry', () => { | ||
beforeEach(() => TestBed.configureTestingModule({ | ||
imports: [ | ||
DaffRadioModule | ||
] | ||
})); | ||
|
||
it('should be created', () => { | ||
const service: DaffRadioRegistry = TestBed.get(DaffRadioRegistry); | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
Oops, something went wrong.