Skip to content

Commit

Permalink
feat(CB2-14451): select component attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
tomcrawleyy committed Nov 29, 2024
1 parent 7fe793c commit c837e2b
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ <h1 tag>
</ng-container>
</h1>
</legend>

<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>

<div data-module="govuk-radios">
<div *ngFor="let option of options">
<input
Expand Down
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.
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,15 +197,23 @@ <h1>
</ng-container>

<!-- Coupling type (TRL Only) -->
<div class="govuk-form-group-select" *ngIf="shouldDisplayFormControl('techRecord_couplingType')">
<label>
Coupling type (optional)
</label>
<app-control-errors elementId="techRecord_couplingType" [control]="$any(form.get('techRecord_couplingType'))" />
<select govukSelect formControlName="techRecord_couplingType">
<option [value]="option.value" *ngFor="let option of CouplingTypeOptions">{{ option.label }}</option>
</select>
</div>
<!-- <div class="govuk-form-group-select" *ngIf="shouldDisplayFormControl('techRecord_couplingType')">-->
<!-- <label>-->
<!-- Coupling type (optional)-->
<!-- </label>-->
<!-- <app-control-errors elementId="techRecord_couplingType" [control]="$any(form.get('techRecord_couplingType'))" />-->
<!-- <select govukSelect formControlName="techRecord_couplingType">-->
<!-- <option [value]="option.value" *ngFor="let option of CouplingTypeOptions">{{ option.label }}</option>-->
<!-- </select>-->
<!-- </div>-->

<govuk-form-group-select
*ngIf="shouldDisplayFormControl('techRecord_couplingType')"
formControlName="techRecord_couplingType"
label="Coupling type (optional)"
[options]="CouplingTypeOptions"
>
</govuk-form-group-select>

<!-- Max load on coupling (TRL Only) -->
<div *ngIf="shouldDisplayFormControl('techRecord_maxLoadOnCoupling')" class="govuk-form-group-input">
Expand Down
2 changes: 2 additions & 0 deletions src/app/forms/dynamic-forms.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { DateFocusNextDirective } from '@directives/date-focus-next/date-focus-n
import { PrefixDirective } from '@directives/prefix/prefix.directive';
import { SuffixDirective } from '@directives/suffix/suffix.directive';
import { ApprovalTypeInputComponent } from '@forms/components/approval-type/approval-type.component';
import { GovukFormGroupSelectComponent } from '@forms/components/govuk-form-group-select/govuk-form-group-select.component';
import { AdrCertificateHistoryComponent } from '@forms/custom-sections/adr-certificate-history/adr-certificate-history.component';
import { AdrExaminerNotesHistoryEditComponent } from '@forms/custom-sections/adr-examiner-notes-history-edit/adr-examiner-notes-history.component-edit';
import { AdrPermittedDangerousGoodsComponent } from '@forms/custom-sections/adr-permitted-dangerous-goods/adr-permitted-dangerous-goods.component';
Expand Down Expand Up @@ -166,6 +167,7 @@ import { WeightsComponent } from './custom-sections/weights/weights.component';
NumberOnlyDirective,
GovukCheckboxGroupComponent,
GovukFormGroupRadioComponent,
GovukFormGroupSelectComponent,
],
exports: [
TextInputComponent,
Expand Down

0 comments on commit c837e2b

Please sign in to comment.