-
-
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): create
DaffSelectable
host directive (#2910)
- Loading branch information
Showing
14 changed files
with
187 additions
and
79 deletions.
There are no files selected for viewing
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
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
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,2 @@ | ||
export { DaffSelectable } from './selectable'; | ||
export { DaffSelectableDirective } from './selectable.directive'; |
78 changes: 78 additions & 0 deletions
78
libs/design/src/core/selectable/selectable.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,78 @@ | ||
import { | ||
Component, | ||
DebugElement, | ||
} from '@angular/core'; | ||
import { | ||
waitForAsync, | ||
ComponentFixture, | ||
TestBed, | ||
} from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
|
||
import { DaffSelectableDirective } from './selectable.directive'; | ||
|
||
@Component({ | ||
template: ` | ||
<div daffSelected | ||
(becameSelected)="becameSelectedFunction($event)" | ||
[selected]="selected"> | ||
</div>`, | ||
}) | ||
|
||
class WrapperComponent { | ||
becameSelected = (val: boolean) => {}; | ||
selected: boolean; | ||
} | ||
|
||
describe('@daffodil/design | DaffSelectableDirective', () => { | ||
let wrapper: WrapperComponent; | ||
let de: DebugElement; | ||
let fixture: ComponentFixture<WrapperComponent>; | ||
let directive: DaffSelectableDirective; | ||
|
||
beforeEach(waitForAsync(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ | ||
WrapperComponent, | ||
], | ||
imports: [ | ||
DaffSelectableDirective, | ||
], | ||
}) | ||
.compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(WrapperComponent); | ||
wrapper = fixture.componentInstance; | ||
de = fixture.debugElement.query(By.css('[daffSelected]')); | ||
directive = de.injector.get(DaffSelectableDirective); | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(wrapper).toBeTruthy(); | ||
expect(directive).toBeTruthy(); | ||
}); | ||
|
||
it('should add a class of "daff-selected" to the host element when selected is true', () => { | ||
wrapper.selected = true; | ||
fixture.detectChanges(); | ||
|
||
expect(de.classes).toEqual(jasmine.objectContaining({ | ||
'daff-selected': true, | ||
})); | ||
}); | ||
|
||
it('should not add a class of "daff-selected" to the host element when selected is false', () => { | ||
expect(de.classes['daff-selected']).toBeUndefined(); | ||
}); | ||
|
||
it('should emit on becameSelected when select is called', () => { | ||
spyOn(directive.becameSelected, 'emit'); | ||
wrapper.selected = true; | ||
directive.select(); | ||
|
||
expect(directive.becameSelected.emit).toHaveBeenCalledWith(); | ||
}); | ||
}); |
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,40 @@ | ||
import { | ||
ChangeDetectorRef, | ||
Directive, | ||
EventEmitter, | ||
HostBinding, | ||
Input, | ||
Output, | ||
} from '@angular/core'; | ||
|
||
import { DaffSelectable } from '../selectable/selectable'; | ||
|
||
@Directive({ | ||
selector: '[daffSelected]', | ||
standalone: true, | ||
}) | ||
|
||
export class DaffSelectableDirective implements DaffSelectable { | ||
/** Whether or not a component implementing the directive is selected */ | ||
@Input() @HostBinding('class.daff-selected') selected = false; | ||
|
||
/** | ||
* An event that fires after the media element becomes selected. | ||
*/ | ||
@Output() becameSelected: EventEmitter<void> = new EventEmitter<void>(); | ||
|
||
constructor(private cd: ChangeDetectorRef) {} | ||
|
||
select() { | ||
this.selected = true; | ||
this.becameSelected.emit(); | ||
this.cd.markForCheck(); | ||
return this; | ||
} | ||
|
||
deselect() { | ||
this.selected = false; | ||
this.cd.markForCheck(); | ||
return this; | ||
} | ||
} |
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,7 @@ | ||
/** | ||
* An interface for giving a component the ability to display a selected UI. | ||
* In order to be selectable, the class must implement this property. | ||
*/ | ||
export interface DaffSelectable { | ||
selected: boolean; | ||
} |
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
Oops, something went wrong.