-
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.
Merge pull request #924 from geonetwork/UI-component-switch-toggle
UI component switch toggle
- Loading branch information
Showing
14 changed files
with
208 additions
and
39 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
31 changes: 31 additions & 0 deletions
31
libs/ui/inputs/src/lib/switch-toggle/switch-toggle.component.css
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,31 @@ | ||
:host { | ||
--mat-standard-button-toggle-height: 32px; | ||
} | ||
|
||
.mat-button-toggle-group-appearance-standard { | ||
background-color: var(--color-gray-200); | ||
padding: 4px; | ||
display: flex; | ||
gap: 4px; | ||
border-radius: 8px; | ||
} | ||
|
||
.mat-button-toggle-appearance-standard { | ||
color: var(--color-main); | ||
background-color: var(--color-gray-200); | ||
border-radius: 4px; | ||
border-left: none; | ||
} | ||
|
||
.mat-button-toggle-appearance-standard.mat-button-toggle-checked { | ||
background-color: var(--color-main); | ||
color: var(--color-primary-white); | ||
} | ||
|
||
button.mat-button-toggle-button.mat-focus-indicator.mat-button-toggle-label-content { | ||
line-height: 32px; | ||
} | ||
|
||
.mat-button-toggle-label-content { | ||
line-height: 32px; | ||
} |
15 changes: 15 additions & 0 deletions
15
libs/ui/inputs/src/lib/switch-toggle/switch-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,15 @@ | ||
<mat-button-toggle-group | ||
#group="matButtonToggleGroup" | ||
multiple="false" | ||
class="flex w-full" | ||
> | ||
<mat-button-toggle | ||
*ngFor="let option of options" | ||
[aria-label]="option.label" | ||
[value]="option.value" | ||
[checked]="option.checked" | ||
(change)="onChange(option)" | ||
[class]="extraClasses" | ||
>{{ option.label }}</mat-button-toggle | ||
> | ||
</mat-button-toggle-group> |
24 changes: 24 additions & 0 deletions
24
libs/ui/inputs/src/lib/switch-toggle/switch-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,24 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing' | ||
|
||
import { SwitchToggleComponent } from './switch-toggle.component' | ||
import { MatButtonToggleModule } from '@angular/material/button-toggle' | ||
import { CommonModule } from '@angular/common' | ||
|
||
describe('SwitchToggleComponent', () => { | ||
let component: SwitchToggleComponent | ||
let fixture: ComponentFixture<SwitchToggleComponent> | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [], | ||
imports: [MatButtonToggleModule, SwitchToggleComponent, CommonModule], | ||
}) | ||
fixture = TestBed.createComponent(SwitchToggleComponent) | ||
component = fixture.componentInstance | ||
fixture.detectChanges() | ||
}) | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy() | ||
}) | ||
}) |
38 changes: 38 additions & 0 deletions
38
libs/ui/inputs/src/lib/switch-toggle/switch-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,38 @@ | ||
import { CommonModule } from '@angular/common' | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
EventEmitter, | ||
Input, | ||
Output, | ||
} from '@angular/core' | ||
import { MatButtonToggleModule } from '@angular/material/button-toggle' | ||
|
||
export type SwitchToggleOption = { | ||
label: string | ||
value: string | ||
checked: boolean | ||
} | ||
|
||
@Component({ | ||
selector: 'gn-ui-switch-toggle', | ||
templateUrl: './switch-toggle.component.html', | ||
styleUrls: ['./switch-toggle.component.css'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
standalone: true, | ||
imports: [MatButtonToggleModule, CommonModule], | ||
}) | ||
export class SwitchToggleComponent { | ||
@Input() options: SwitchToggleOption[] | ||
@Input() ariaLabel? = '' | ||
@Input() extraClasses? = '' | ||
@Output() selectedValue = new EventEmitter<SwitchToggleOption>() | ||
|
||
onChange(selectedOption: SwitchToggleOption) { | ||
this.options.find( | ||
(option) => option.value === selectedOption.value | ||
).checked = true | ||
|
||
this.selectedValue.emit(selectedOption) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
libs/ui/inputs/src/lib/switch-toggle/switch-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,29 @@ | ||
import { Meta, moduleMetadata, StoryObj } from '@storybook/angular' | ||
import { SwitchToggleComponent } from './switch-toggle.component' | ||
import { MatButtonToggleModule } from '@angular/material/button-toggle' | ||
import { CommonModule } from '@angular/common' | ||
|
||
export default { | ||
title: 'Inputs/SwitchToggle', | ||
component: SwitchToggleComponent, | ||
decorators: [ | ||
moduleMetadata({ | ||
declarations: [], | ||
imports: [SwitchToggleComponent, MatButtonToggleModule, CommonModule], | ||
}), | ||
], | ||
} as Meta<SwitchToggleComponent> | ||
|
||
export const Primary: StoryObj<SwitchToggleComponent> = { | ||
args: { | ||
options: [ | ||
{ label: 'city', value: 'city', checked: true }, | ||
{ label: 'municipality', value: 'municipality', checked: false }, | ||
{ label: 'state', value: 'state', checked: false }, | ||
], | ||
extraClasses: 'grow', | ||
}, | ||
render: (args) => ({ | ||
props: { ...args }, | ||
}), | ||
} |
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
Oops, something went wrong.