-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract enum dropdown into own component
for easier re-use and testing
- Loading branch information
Showing
9 changed files
with
142 additions
and
26 deletions.
There are no files selected for viewing
20 changes: 6 additions & 14 deletions
20
src/app/core/configurable-enum/edit-configurable-enum/edit-configurable-enum.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 |
---|---|---|
@@ -1,14 +1,6 @@ | ||
<mat-form-field [formGroup]="parent"> | ||
<mat-label> | ||
{{ label }} | ||
</mat-label> | ||
<mat-select [formControlName]="formControlName" [multiple]="multi" [compareWith]="compareFun"> | ||
<mat-option *appConfigurableEnum="let o of enumId" [value]="o"> | ||
{{ o.label }} | ||
</mat-option> | ||
</mat-select> | ||
<mat-error *ngIf="formControl.errors?.required" i18n="Error message for any input"> | ||
This field is required | ||
</mat-error> | ||
</mat-form-field> | ||
|
||
<app-enum-dropdown | ||
[form]="formControl" | ||
[label]="label" | ||
[multi]="multi" | ||
[enumId]="enumId" | ||
></app-enum-dropdown> |
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
17 changes: 17 additions & 0 deletions
17
src/app/core/configurable-enum/enum-dropdown/enum-dropdown.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,17 @@ | ||
<mat-form-field> | ||
<mat-label> | ||
{{ label }} | ||
</mat-label> | ||
<mat-select | ||
[formControl]="form" | ||
[multiple]="multi" | ||
[compareWith]="compareFun" | ||
> | ||
<mat-option *appConfigurableEnum="let o of enumId" [value]="o"> | ||
{{ o.label }} | ||
</mat-option> | ||
</mat-select> | ||
<mat-error *ngIf="form.errors?.required" i18n="Error message for any input"> | ||
This field is required | ||
</mat-error> | ||
</mat-form-field> |
Empty file.
35 changes: 35 additions & 0 deletions
35
src/app/core/configurable-enum/enum-dropdown/enum-dropdown.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,35 @@ | ||
import { ComponentFixture, TestBed } from "@angular/core/testing"; | ||
|
||
import { EnumDropdownComponent } from "./enum-dropdown.component"; | ||
import { ConfigService } from "../../config/config.service"; | ||
import { FormControl } from "@angular/forms"; | ||
import { NoopAnimationsModule } from "@angular/platform-browser/animations"; | ||
|
||
describe("EnumDropdownComponent", () => { | ||
let component: EnumDropdownComponent; | ||
let fixture: ComponentFixture<EnumDropdownComponent>; | ||
|
||
let mockConfigService: jasmine.SpyObj<ConfigService>; | ||
|
||
beforeEach(async () => { | ||
mockConfigService = jasmine.createSpyObj(["getConfig"]); | ||
mockConfigService.getConfig.and.returnValue([]); | ||
|
||
await TestBed.configureTestingModule({ | ||
imports: [EnumDropdownComponent, NoopAnimationsModule], | ||
providers: [{ provide: ConfigService, useValue: mockConfigService }], | ||
}).compileComponents(); | ||
|
||
fixture = TestBed.createComponent(EnumDropdownComponent); | ||
component = fixture.componentInstance; | ||
|
||
component.form = new FormControl(); | ||
component.enumId = "test-enum"; | ||
|
||
fixture.detectChanges(); | ||
}); | ||
|
||
it("should create", () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
src/app/core/configurable-enum/enum-dropdown/enum-dropdown.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 @@ | ||
import { Component, Input } from "@angular/core"; | ||
import { MatSelectModule } from "@angular/material/select"; | ||
import { FormControl, ReactiveFormsModule } from "@angular/forms"; | ||
import { ConfigurableEnumDirective } from "../configurable-enum-directive/configurable-enum.directive"; | ||
import { compareEnums } from "../../../utils/utils"; | ||
import { NgIf } from "@angular/common"; | ||
|
||
@Component({ | ||
selector: "app-enum-dropdown", | ||
templateUrl: "./enum-dropdown.component.html", | ||
styleUrls: ["./enum-dropdown.component.scss"], | ||
standalone: true, | ||
imports: [ | ||
MatSelectModule, | ||
ReactiveFormsModule, | ||
ConfigurableEnumDirective, | ||
NgIf, | ||
], | ||
}) | ||
export class EnumDropdownComponent { | ||
@Input() form: FormControl; // cannot be named "formControl" - otherwise the angular directive grabs this | ||
@Input() label: string; | ||
@Input() enumId: string; | ||
@Input() multi?: boolean; | ||
|
||
compareFun = compareEnums; | ||
} |
37 changes: 37 additions & 0 deletions
37
src/app/core/configurable-enum/enum-dropdown/enum-dropdown.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,37 @@ | ||
import { Meta, Story } from "@storybook/angular/types-6-0"; | ||
import { moduleMetadata } from "@storybook/angular"; | ||
import { StorybookBaseModule } from "../../../utils/storybook-base.module"; | ||
import { EnumDropdownComponent } from "./enum-dropdown.component"; | ||
import { FormControl } from "@angular/forms"; | ||
|
||
export default { | ||
title: "Core/EntityComponents/Entity Property Fields/Enum Dropdown", | ||
component: EnumDropdownComponent, | ||
decorators: [ | ||
moduleMetadata({ | ||
imports: [EnumDropdownComponent, StorybookBaseModule], | ||
providers: [], | ||
}), | ||
], | ||
} as Meta; | ||
|
||
const Template: Story<EnumDropdownComponent> = ( | ||
args: EnumDropdownComponent | ||
) => ({ | ||
props: args, | ||
}); | ||
|
||
export const Primary = Template.bind({}); | ||
Primary.args = { | ||
form: new FormControl(""), | ||
label: "test field", | ||
enumId: "center", | ||
}; | ||
|
||
export const Multi = Template.bind({}); | ||
Multi.args = { | ||
form: new FormControl(""), | ||
label: "test field", | ||
enumId: "center", | ||
multi: true, | ||
}; |
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