-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1645 from UUDigitalHumanitieslab/feature/toggle-b…
…utton-directive Feature/toggle button directive
- Loading branch information
Showing
8 changed files
with
105 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Component } from '@angular/core'; | ||
import { ToggleButtonDirective } from './toggle-button.directive'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { CommonModule } from '@angular/common'; | ||
|
||
@Component({ | ||
template: ` | ||
<button class="button" iaToggleButton [active]="active" [activeClass]="class"> | ||
Test | ||
</button> | ||
`, | ||
}) | ||
class ToggleButtonTestComponent { | ||
active = false; | ||
class = 'is-primary'; | ||
} | ||
|
||
describe('ToggleButtonDirective', () => { | ||
let fixture: ComponentFixture<ToggleButtonTestComponent>; | ||
let component: ToggleButtonTestComponent; | ||
let button: HTMLButtonElement; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ToggleButtonTestComponent, ToggleButtonDirective], | ||
imports: [CommonModule], | ||
}); | ||
fixture = TestBed.createComponent(ToggleButtonTestComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
const element = fixture.debugElement.nativeElement as Element; | ||
button = element.querySelector('button'); | ||
|
||
}); | ||
|
||
it('should show toggle state', () => { | ||
expect(button.className).toEqual('button'); | ||
expect(button.getAttribute('aria-pressed')).toBe('false'); | ||
|
||
component.active = true; | ||
fixture.detectChanges(); | ||
|
||
expect(button.className).toEqual('button is-primary'); | ||
expect(button.getAttribute('aria-pressed')).toBe('true'); | ||
}); | ||
|
||
it('should set the CSS class through input', () => { | ||
component.class = 'is-danger'; | ||
component.active = true; | ||
fixture.detectChanges(); | ||
|
||
expect(button.className).toEqual('button is-danger'); | ||
}); | ||
}); |
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 { Directive, HostBinding, Input } from '@angular/core'; | ||
|
||
@Directive({ | ||
selector: 'button[iaToggleButton]', | ||
}) | ||
export class ToggleButtonDirective { | ||
@HostBinding('attr.aria-pressed') | ||
@Input() active: boolean; | ||
|
||
/** name of the CSS class that should be applied when active */ | ||
@Input() activeClass: string = 'is-primary'; | ||
|
||
@HostBinding('class') | ||
get classes(): Record<string, boolean> { | ||
return { [this.activeClass]: this.active } | ||
} | ||
} |
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