Skip to content

Commit 1a0bee7

Browse files
committed
feat: added rating for formgroup example
1 parent 36cd02a commit 1a0bee7

File tree

12 files changed

+105
-2
lines changed

12 files changed

+105
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<form class="example-form" [formGroupName]="this.controlKey()" ngNativeValidate>
2+
<mat-slide-toggle formControlName="a">A</mat-slide-toggle>
3+
<mat-slide-toggle formControlName="b">B</mat-slide-toggle>
4+
<mat-slide-toggle formControlName="c">C</mat-slide-toggle>
5+
</form>

src/app/form-filter/components/rating-filter/rating-filter.component.scss

Whitespace-only changes.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { Component, inject, input, OnDestroy, OnInit } from '@angular/core';
2+
import { ControlContainer, FormControl, FormGroup } from '@angular/forms';
3+
import { Subscription } from 'rxjs';
4+
5+
@Component({
6+
standalone: false,
7+
selector: 'app-rating-filter',
8+
templateUrl: './rating-filter.component.html',
9+
styleUrl: './rating-filter.component.scss',
10+
viewProviders: [
11+
{
12+
provide: ControlContainer,
13+
useFactory: () => inject(ControlContainer, { skipSelf: true }),
14+
},
15+
],
16+
})
17+
export class RatingFilterComponent implements OnInit, OnDestroy {
18+
controlKey = input.required<string>();
19+
parentContainer = inject(ControlContainer);
20+
21+
subs = new Subscription();
22+
23+
get parentFormGroup() {
24+
return this.parentContainer.control as FormGroup;
25+
}
26+
27+
constructor() {}
28+
29+
ngOnInit(): void {
30+
console.log('testing', this.controlKey());
31+
this.initialiseForm();
32+
}
33+
34+
ngOnDestroy() {
35+
this.parentFormGroup.removeControl(this.controlKey());
36+
this.subs.unsubscribe();
37+
}
38+
39+
initialiseForm() {
40+
this.parentFormGroup.addControl(
41+
this.controlKey(),
42+
new FormGroup({
43+
a: new FormControl(false),
44+
b: new FormControl(false),
45+
c: new FormControl(false),
46+
}),
47+
);
48+
}
49+
}

src/app/form-filter/constants/filter-default-map.constant.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ export const FILTER_DEFAULTS: SavedFilter = {
2727
displayName: 'Company',
2828
},
2929
},
30+
[FilterKey.Rating]: {
31+
filterForm: { [FilterKey.Rating]: [], key: FilterKey.Rating },
32+
filterConfig: {
33+
displayName: 'Rating',
34+
},
35+
},
3036
};

src/app/form-filter/constants/filter-key.enum.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export enum FilterKey {
33
Date = 'date',
44
Industry = 'industry',
55
Company = 'company',
6+
Rating = 'rating',
67
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const RATING_DATA = ['A', 'B', 'C'];

src/app/form-filter/form-filter.component.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,19 @@
6868
<app-company-filter [controlKey]="filter.key"></app-company-filter>
6969
</mat-expansion-panel>
7070
}
71+
@case (filterKey.Rating) {
72+
<mat-expansion-panel>
73+
<mat-expansion-panel-header>
74+
<mat-panel-title>
75+
{{ filter.value.filterConfig.displayName }}
76+
</mat-panel-title>
77+
<button mat-icon-button (click)="deleteFilter(filter.key)">
78+
<mat-icon>close</mat-icon>
79+
</button>
80+
</mat-expansion-panel-header>
81+
<app-rating-filter [controlKey]="filter.key"></app-rating-filter>
82+
</mat-expansion-panel>
83+
}
7184
}
7285
}
7386
</mat-accordion>

src/app/form-filter/form-filter.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { CompanyFilterComponent } from './components/company-filter/company-filt
66
import { CountryFilterComponent } from './components/country-filter/country-filter.component';
77
import { DateFilterComponent } from './components/date-filter/date-filter.component';
88
import { IndustryFilterComponent } from './components/industry-filter/industry-filter.component';
9+
import { RatingFilterComponent } from './components/rating-filter/rating-filter.component';
910
import { FormFilterComponent } from './form-filter.component';
1011

1112
@NgModule({
@@ -15,6 +16,7 @@ import { FormFilterComponent } from './form-filter.component';
1516
DateFilterComponent,
1617
IndustryFilterComponent,
1718
CompanyFilterComponent,
19+
RatingFilterComponent,
1820
],
1921
imports: [CommonModule, FormsModule, ReactiveFormsModule, MaterialModule],
2022
exports: [FormFilterComponent, CountryFilterComponent, DateFilterComponent],

src/app/form-filter/models/filter-form.model.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ export type FilterForm =
44
| CountryFilter
55
| DateFilter
66
| IndustryFilter
7-
| CompanyFilter;
7+
| CompanyFilter
8+
| RatingFilter;
89

910
export interface CountryFilter {
1011
key: FilterKey.Country;
@@ -25,3 +26,8 @@ export interface CompanyFilter {
2526
key: FilterKey.Company;
2627
[FilterKey.Company]: string[];
2728
}
29+
30+
export interface RatingFilter {
31+
key: FilterKey.Rating;
32+
[FilterKey.Rating]: string[];
33+
}

src/app/form-filter/services/broadcast-factory.service.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export class BroadcastFactoryService {
1717
[FilterKey.Date]: (value) => this.getDatePayload(value),
1818
[FilterKey.Industry]: (value) => this.getIndustryPayload(value),
1919
[FilterKey.Company]: (value) => this.getCompanyPayload(value),
20+
[FilterKey.Rating]: (value) => this.getRatingPayload(value),
2021
};
2122

2223
getBroadcastPayload(value: Partial<{}>): BroadcastPayload[] {
@@ -70,4 +71,13 @@ export class BroadcastFactoryService {
7071
date,
7172
};
7273
}
74+
75+
private getRatingPayload(value: unknown): BroadcastPayload {
76+
const ratings = value as string[];
77+
78+
return {
79+
key: FilterBroadcastKey.Rating,
80+
ratings,
81+
};
82+
}
7383
}

0 commit comments

Comments
 (0)