From 806237801694e2669470f541cede2c8cdb4f751d Mon Sep 17 00:00:00 2001 From: shamoon <4887959+shamoon@users.noreply.github.com> Date: Tue, 26 Dec 2023 22:30:07 -0800 Subject: [PATCH] Merge in switch component --- src-ui/src/app/app.module.ts | 2 + .../common/input/switch/switch.component.html | 27 +++++++++++++ .../common/input/switch/switch.component.scss | 0 .../input/switch/switch.component.spec.ts | 39 +++++++++++++++++++ .../common/input/switch/switch.component.ts | 21 ++++++++++ 5 files changed, 89 insertions(+) create mode 100644 src-ui/src/app/components/common/input/switch/switch.component.html create mode 100644 src-ui/src/app/components/common/input/switch/switch.component.scss create mode 100644 src-ui/src/app/components/common/input/switch/switch.component.spec.ts create mode 100644 src-ui/src/app/components/common/input/switch/switch.component.ts diff --git a/src-ui/src/app/app.module.ts b/src-ui/src/app/app.module.ts index 400064f17..ce9902a6c 100644 --- a/src-ui/src/app/app.module.ts +++ b/src-ui/src/app/app.module.ts @@ -108,6 +108,7 @@ import { ProfileEditDialogComponent } from './components/common/profile-edit-dia import { PdfViewerComponent } from './components/common/pdf-viewer/pdf-viewer.component' import { DocumentLinkComponent } from './components/common/input/document-link/document-link.component' import { PreviewPopupComponent } from './components/common/preview-popup/preview-popup.component' +import { SwitchComponent } from './components/common/input/switch/switch.component' import localeAf from '@angular/common/locales/af' import localeAr from '@angular/common/locales/ar' @@ -263,6 +264,7 @@ function initializeApp(settings: SettingsService) { PdfViewerComponent, DocumentLinkComponent, PreviewPopupComponent, + SwitchComponent, ], imports: [ BrowserModule, diff --git a/src-ui/src/app/components/common/input/switch/switch.component.html b/src-ui/src/app/components/common/input/switch/switch.component.html new file mode 100644 index 000000000..1b2f5e6d9 --- /dev/null +++ b/src-ui/src/app/components/common/input/switch/switch.component.html @@ -0,0 +1,27 @@ +
+
+ @if (horizontal) { +
+ + @if (removable) { + + } +
+ } +
+
+ + @if (!horizontal) { + + } + @if (hint) { +
{{hint}}
+ } +
+
+
+
diff --git a/src-ui/src/app/components/common/input/switch/switch.component.scss b/src-ui/src/app/components/common/input/switch/switch.component.scss new file mode 100644 index 000000000..e69de29bb diff --git a/src-ui/src/app/components/common/input/switch/switch.component.spec.ts b/src-ui/src/app/components/common/input/switch/switch.component.spec.ts new file mode 100644 index 000000000..08a4598a3 --- /dev/null +++ b/src-ui/src/app/components/common/input/switch/switch.component.spec.ts @@ -0,0 +1,39 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing' +import { SwitchComponent } from './switch.component' +import { + FormsModule, + NG_VALUE_ACCESSOR, + ReactiveFormsModule, +} from '@angular/forms' + +describe('SwitchComponent', () => { + let component: SwitchComponent + let fixture: ComponentFixture + let input: HTMLInputElement + + beforeEach(async () => { + TestBed.configureTestingModule({ + declarations: [SwitchComponent], + providers: [], + imports: [FormsModule, ReactiveFormsModule], + }).compileComponents() + + fixture = TestBed.createComponent(SwitchComponent) + fixture.debugElement.injector.get(NG_VALUE_ACCESSOR) + component = fixture.componentInstance + fixture.detectChanges() + input = component.inputField.nativeElement + }) + + it('should support use of checkbox', () => { + input.checked = true + input.dispatchEvent(new Event('change')) + fixture.detectChanges() + expect(component.value).toBeTruthy() + + input.checked = false + input.dispatchEvent(new Event('change')) + fixture.detectChanges() + expect(component.value).toBeFalsy() + }) +}) diff --git a/src-ui/src/app/components/common/input/switch/switch.component.ts b/src-ui/src/app/components/common/input/switch/switch.component.ts new file mode 100644 index 000000000..44e095baa --- /dev/null +++ b/src-ui/src/app/components/common/input/switch/switch.component.ts @@ -0,0 +1,21 @@ +import { Component, forwardRef } from '@angular/core' +import { NG_VALUE_ACCESSOR } from '@angular/forms' +import { AbstractInputComponent } from '../abstract-input' + +@Component({ + providers: [ + { + provide: NG_VALUE_ACCESSOR, + useExisting: forwardRef(() => SwitchComponent), + multi: true, + }, + ], + selector: 'pngx-input-switch', + templateUrl: './switch.component.html', + styleUrls: ['./switch.component.scss'], +}) +export class SwitchComponent extends AbstractInputComponent { + constructor() { + super() + } +}