-
Notifications
You must be signed in to change notification settings - Fork 1
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 #350 from PermanentOrg/PER-9444-comp-library-toggle
PER-9444 toggle component
- Loading branch information
Showing
8 changed files
with
212 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* @format */ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { ToggleComponent } from './components/components/toggle/toggle.component'; | ||
|
||
@NgModule({ | ||
declarations: [ToggleComponent], | ||
imports: [CommonModule], | ||
exports: [ToggleComponent], | ||
}) | ||
export class ComponentsModule {} |
12 changes: 12 additions & 0 deletions
12
src/app/component-library/components/components/toggle/toggle.component.html
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,12 @@ | ||
<!-- @format --> | ||
<div | ||
class="toggle-container" | ||
[class.checked]="isChecked" | ||
[class.disabled]="disabled" | ||
(click)="toggleSwitch()" | ||
> | ||
<div class="toggle-switch"> | ||
<div class="toggle-knob"></div> | ||
</div> | ||
<p>{{ text }}</p> | ||
</div> |
48 changes: 48 additions & 0 deletions
48
src/app/component-library/components/components/toggle/toggle.component.scss
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,48 @@ | ||
/* @format */ | ||
@import 'variables'; | ||
|
||
.toggle-container { | ||
padding: 5px; | ||
display: flex; | ||
width: fit-content; | ||
align-items: center; | ||
|
||
& > p { | ||
margin-left: 10px; | ||
margin-bottom: 0; | ||
} | ||
|
||
&.disabled { | ||
cursor: not-allowed; | ||
} | ||
|
||
& > .toggle-switch { | ||
cursor: pointer; | ||
width: 36px; | ||
height: 20px; | ||
border-radius: 25px; | ||
position: relative; | ||
background-color: $toggle; | ||
transition: background-color 0.2s; | ||
|
||
& > .toggle-knob { | ||
width: 15px; | ||
height: 15px; | ||
background-color: $white; | ||
border-radius: 50%; | ||
position: absolute; | ||
top: 2.5px; | ||
left: 2.5px; | ||
transition: left 0.2s; | ||
} | ||
} | ||
|
||
&.checked { | ||
& > .toggle-switch { | ||
background-color: $toggle-checked; | ||
& > .toggle-knob { | ||
left: 18.5px; | ||
} | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/app/component-library/components/components/toggle/toggle.component.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,60 @@ | ||
/* @format */ | ||
import { Shallow } from 'shallow-render'; | ||
import { ToggleComponent } from './toggle.component'; | ||
|
||
describe('ToggleComponent', () => { | ||
let shallow: Shallow<ToggleComponent>; | ||
|
||
beforeEach(async () => { | ||
shallow = new Shallow(ToggleComponent, Shallow); | ||
}); | ||
|
||
it('should create', async () => { | ||
const { instance } = await shallow.render(); | ||
|
||
expect(instance).toBeTruthy(); | ||
}); | ||
|
||
it('should have the checked class when the toggle is checked', async () => { | ||
const { instance, find, fixture } = await shallow.render(); | ||
instance.isChecked = true; | ||
fixture.detectChanges(); | ||
const toggle = find('.toggle-container').nativeElement; | ||
|
||
expect(toggle.classList).toContain('checked'); | ||
}); | ||
|
||
it('should have the disabled class when the toggle is disabled', async () => { | ||
const { instance, find, fixture } = await shallow.render(); | ||
instance.disabled = true; | ||
fixture.detectChanges(); | ||
const toggle = find('.toggle-container').nativeElement; | ||
|
||
expect(toggle.classList).toContain('disabled'); | ||
}); | ||
|
||
it('should emit the correct value when the toggle is clicked', async () => { | ||
const { instance, find, fixture } = await shallow.render(); | ||
fixture.detectChanges(); | ||
const toggle = find('.toggle-container').nativeElement; | ||
toggle.click(); | ||
|
||
expect(instance.isCheckedChange.emit).toHaveBeenCalledWith(true); | ||
|
||
instance.isChecked = true; | ||
fixture.detectChanges(); | ||
toggle.click(); | ||
|
||
expect(instance.isCheckedChange.emit).toHaveBeenCalledWith(false); | ||
}); | ||
|
||
it('should not emit when the toggle is disabled', async () => { | ||
const { instance, find, fixture } = await shallow.render(); | ||
instance.disabled = true; | ||
fixture.detectChanges(); | ||
const toggle = find('.toggle-container').nativeElement; | ||
toggle.click(); | ||
|
||
expect(instance.isCheckedChange.emit).not.toHaveBeenCalled(); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
src/app/component-library/components/components/toggle/toggle.component.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,27 @@ | ||
/* @format */ | ||
import { Component, EventEmitter, Input, Output, OnInit } from '@angular/core'; | ||
|
||
@Component({ | ||
selector: 'pr-toggle', | ||
templateUrl: './toggle.component.html', | ||
styleUrls: ['./toggle.component.scss'], | ||
}) | ||
export class ToggleComponent implements OnInit { | ||
@Input() isChecked: boolean = false; | ||
@Input() text: string = ''; | ||
@Input() disabled: boolean = false; | ||
@Output() isCheckedChange = new EventEmitter<boolean>(); | ||
|
||
ngOnInit() { | ||
if (this.disabled) { | ||
this.isChecked = false; | ||
} | ||
} | ||
|
||
toggleSwitch() { | ||
if (!this.disabled) { | ||
this.isChecked = !this.isChecked; | ||
this.isCheckedChange.emit(this.isChecked); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/app/component-library/components/components/toggle/toggle.stories.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,45 @@ | ||
/* @format */ | ||
import { | ||
componentWrapperDecorator, | ||
Meta, | ||
moduleMetadata, | ||
StoryObj, | ||
} from '@storybook/angular'; | ||
import { action } from '@storybook/addon-actions'; | ||
import { ComponentsModule } from '../../../components.module'; | ||
import { ToggleComponent } from './toggle.component'; | ||
|
||
const meta: Meta<ToggleComponent> = { | ||
title: 'Toggle', | ||
component: ToggleComponent, | ||
tags: ['autodocs'], | ||
render: (args: ToggleComponent) => ({ | ||
props: { | ||
...args, | ||
isCheckedChange: action('isCheckedChange'), | ||
}, | ||
argTypes: { | ||
isChecked: 'checked', | ||
text: 'text', | ||
disabled: 'disabled', | ||
}, | ||
}), | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<ToggleComponent>; | ||
|
||
export const Enabled: Story = { | ||
args: { | ||
isChecked: true, | ||
text: 'Toggle Example', | ||
disabled: false, | ||
}, | ||
}; | ||
|
||
export const Disabled: Story = { | ||
args: { | ||
disabled: true, | ||
text: 'Disabled Toggle Example', | ||
}, | ||
}; |
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