-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from CentreForDigitalHumanities/feature/desig…
…nators-component Feature/designators component
- Loading branch information
Showing
8 changed files
with
245 additions
and
0 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
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
18 changes: 18 additions & 0 deletions
18
frontend/src/app/data-entry/shared/data-entry-shared.module.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,18 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { SharedModule } from '@shared/shared.module'; | ||
import { DesignatorsControlComponent } from './designators-control/designators-control.component'; | ||
|
||
|
||
|
||
@NgModule({ | ||
declarations: [ | ||
DesignatorsControlComponent | ||
], | ||
imports: [ | ||
SharedModule | ||
], | ||
exports: [ | ||
DesignatorsControlComponent, | ||
] | ||
}) | ||
export class DataEntrySharedModule { } |
33 changes: 33 additions & 0 deletions
33
frontend/src/app/data-entry/shared/designators-control/designators-control.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,33 @@ | ||
<div class="mb-3"> | ||
<p>Designators</p> | ||
<p *ngIf="designators$.value.length === 0"> | ||
<i>No designators added.</i> | ||
</p> | ||
<ul class="list-group"> | ||
<li *ngFor="let designator of designators$.value; let i = index" | ||
class="list-group-item hstack gap-3"> | ||
<span>{{designator}}</span> | ||
<button class="btn ms-auto" type="button" | ||
[disabled]="disabled" | ||
(blur)="blur$.next()" | ||
(click)="removeDesignator(i)"> | ||
<lc-icon [icon]="actionIcons.remove" | ||
[attr.aria-label]="designatorRemoveLabel(designator)" /> | ||
</button> | ||
</li> | ||
</ul> | ||
</div> | ||
|
||
<form class="mb-3" [formGroup]="addForm" (ngSubmit)="onAddFormSubmit()"> | ||
<label class="form-label" for="input-new-designator">Add a designator</label> | ||
<div class="input-group mb-3"> | ||
<input type="text" class="form-control" id="input-new-designator" | ||
[formControl]="addForm.controls.designator" | ||
(blur)="blur$.next()"> | ||
<button class="btn btn-primary" type="submit" | ||
[disabled]="disabled" | ||
(blur)="blur$.next()"> | ||
<lc-icon [icon]="actionIcons.add" aria-label="add designator" /> | ||
</button> | ||
</div> | ||
</form> |
Empty file.
23 changes: 23 additions & 0 deletions
23
frontend/src/app/data-entry/shared/designators-control/designators-control.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,23 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { DesignatorsControlComponent } from './designators-control.component'; | ||
import { SharedTestingModule } from '@shared/shared-testing.module'; | ||
|
||
describe('DesignatorsControlComponent', () => { | ||
let component: DesignatorsControlComponent; | ||
let fixture: ComponentFixture<DesignatorsControlComponent>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [DesignatorsControlComponent], | ||
imports: [SharedTestingModule], | ||
}); | ||
fixture = TestBed.createComponent(DesignatorsControlComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
86 changes: 86 additions & 0 deletions
86
frontend/src/app/data-entry/shared/designators-control/designators-control.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,86 @@ | ||
import { Component, forwardRef, Input, OnDestroy } from '@angular/core'; | ||
import { ControlValueAccessor, FormControl, FormGroup, NG_VALUE_ACCESSOR, Validators } from '@angular/forms'; | ||
import { actionIcons } from '@shared/icons'; | ||
import { BehaviorSubject, Subject, Subscription } from 'rxjs'; | ||
import _ from 'underscore'; | ||
|
||
@Component({ | ||
selector: 'lc-designators-control', | ||
templateUrl: './designators-control.component.html', | ||
styleUrls: ['./designators-control.component.scss'], | ||
providers: [ | ||
{ | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: forwardRef(() => DesignatorsControlComponent), | ||
multi: true, | ||
} | ||
], | ||
}) | ||
export class DesignatorsControlComponent implements ControlValueAccessor, OnDestroy { | ||
@Input({ required: true }) formControl!: FormControl<string[]>; | ||
|
||
designators$ = new BehaviorSubject<string[]>([]); | ||
blur$ = new Subject<void>(); | ||
disabled = false; | ||
|
||
addForm = new FormGroup({ | ||
designator: new FormControl<string>('', { | ||
nonNullable: true, | ||
validators: [ | ||
Validators.required, | ||
Validators.minLength(1), | ||
] | ||
}) | ||
}); | ||
|
||
actionIcons = actionIcons; | ||
|
||
private onChangeSubscription?: Subscription; | ||
private onTouchedSubscription?: Subscription; | ||
|
||
writeValue(obj: string[]): void { | ||
this.designators$.next(obj); | ||
} | ||
|
||
registerOnChange(fn: (_: unknown) => void): void { | ||
this.onChangeSubscription?.unsubscribe(); | ||
this.onChangeSubscription = this.designators$.subscribe(fn); | ||
} | ||
|
||
registerOnTouched(fn: (_: unknown) => void): void { | ||
this.onTouchedSubscription?.unsubscribe(); | ||
this.onTouchedSubscription = this.blur$.subscribe(fn); | ||
} | ||
|
||
ngOnDestroy(): void { | ||
this.designators$.complete(); | ||
this.blur$.complete(); | ||
} | ||
|
||
setDisabledState(isDisabled: boolean): void { | ||
this.disabled = isDisabled; | ||
if (isDisabled) { | ||
this.addForm.disable(); | ||
} else { | ||
this.addForm.enable(); | ||
} | ||
} | ||
|
||
onAddFormSubmit() { | ||
if (this.addForm.valid) { | ||
const designator = this.addForm.controls.designator.value; | ||
this.designators$.next(this.designators$.value.concat([designator])); | ||
this.addForm.reset({ designator: '' }); | ||
} | ||
} | ||
|
||
removeDesignator(index: number) { | ||
const spliced = _.clone(this.designators$.value) | ||
spliced.splice(index, 1); | ||
this.designators$.next(spliced); | ||
} | ||
|
||
designatorRemoveLabel(designator: string): string { | ||
return `remove designator "${designator}"`; | ||
} | ||
} |
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