-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(CB2-14451): select component attempt
- Loading branch information
1 parent
7fe793c
commit c837e2b
Showing
6 changed files
with
157 additions
and
9 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
24 changes: 24 additions & 0 deletions
24
src/app/forms/components/govuk-form-group-select/govuk-form-group-select.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,24 @@ | ||
<div class="govuk-form-group-select"> | ||
<label> | ||
{{ controlLabel }} | ||
</label> | ||
<ng-container *ngIf="controlHint"> | ||
<div class="govuk-hint" id="{{ hintId }}"> {{ controlHint }} </div> | ||
</ng-container> | ||
|
||
<ng-container *ngIf="hasError"> | ||
<p class="govuk-error-message" id="{{ errorId }}"> | ||
<span class="govuk-visually-hidden">Error: </span> | ||
<span>{{ (control?.errors | keyvalue)?.[0]?.value }}</span> | ||
</p> | ||
</ng-container> | ||
<select | ||
id="{{ controlName }}" | ||
name="{{ controlName }}" | ||
[attr.aria-describedby]="hintId" | ||
[attr.aria-labelledby]="labelId" | ||
[attr.data-testid]="id" | ||
> | ||
<option [value]="option.value" *ngFor="let option of options">{{ option.label }}</option> | ||
</select> | ||
</div> |
Empty file.
102 changes: 102 additions & 0 deletions
102
src/app/forms/components/govuk-form-group-select/govuk-form-group-select.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,102 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { Component, EventEmitter, Input, Output, forwardRef, inject } from '@angular/core'; | ||
import { | ||
ControlContainer, | ||
ControlValueAccessor, | ||
FormsModule, | ||
NG_VALUE_ACCESSOR, | ||
ReactiveFormsModule, | ||
} from '@angular/forms'; | ||
import { DynamicFormsModule } from '@forms/dynamic-forms.module'; | ||
import { MultiOption } from '@models/options.model'; | ||
import { CustomTag } from '@services/dynamic-forms/dynamic-form.types'; | ||
import { SharedModule } from '@shared/shared.module'; | ||
|
||
@Component({ | ||
selector: 'govuk-form-group-select', | ||
standalone: true, | ||
imports: [CommonModule, FormsModule, ReactiveFormsModule, SharedModule, DynamicFormsModule], | ||
templateUrl: './govuk-form-group-select.component.html', | ||
styleUrls: ['./govuk-form-group-select.component.scss'], | ||
providers: [ | ||
{ | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: forwardRef(() => GovukFormGroupSelectComponent), | ||
multi: true, | ||
}, | ||
], | ||
}) | ||
export class GovukFormGroupSelectComponent implements ControlValueAccessor { | ||
@Output() blur = new EventEmitter<FocusEvent>(); | ||
@Output() focus = new EventEmitter<FocusEvent>(); | ||
|
||
@Input() | ||
value: string | number | boolean | null = null; | ||
|
||
@Input() | ||
disabled = false; | ||
|
||
@Input() | ||
tags: CustomTag[] = []; | ||
|
||
@Input({ required: true }) | ||
options!: MultiOption[]; | ||
|
||
@Input({ alias: 'hint' }) | ||
controlHint = ''; | ||
|
||
@Input({ alias: 'formControlName', required: true }) | ||
controlName = ''; | ||
|
||
@Input({ alias: 'label', required: true }) | ||
controlLabel = ''; | ||
|
||
@Input({ alias: 'id' }) | ||
controlId = ''; | ||
|
||
controlContainer = inject(ControlContainer); | ||
|
||
get control() { | ||
return this.controlContainer.control?.get(this.controlName); | ||
} | ||
|
||
get id() { | ||
return this.controlId || this.controlName; | ||
} | ||
|
||
get hintId() { | ||
return `${this.id}-hint`; | ||
} | ||
|
||
get labelId() { | ||
return `${this.id}-label`; | ||
} | ||
|
||
get errorId() { | ||
return `${this.id}-error`; | ||
} | ||
|
||
get hasError() { | ||
return this.control?.invalid && this.control?.touched && this.control?.errors; | ||
} | ||
|
||
onChange = (_: any) => {}; | ||
onTouched = () => {}; | ||
|
||
writeValue(obj: any): void { | ||
this.value = obj; | ||
this.onChange(obj); | ||
} | ||
|
||
registerOnChange(fn: any): void { | ||
this.onChange = fn; | ||
} | ||
|
||
registerOnTouched(fn: any): void { | ||
this.onTouched = fn; | ||
} | ||
|
||
setDisabledState?(isDisabled: boolean): void { | ||
this.disabled = isDisabled; | ||
} | ||
} |
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