Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
feat(new-resource-form): make visible the required prop fields (DSP-1…
…115) (#342) * feat(select-properties): check if the prop is required according to its cardinality * test(select-properties): fix test * style(select-properties): fix the alignment label-asterisk * feat(switch-properties): add input valueRequiredValidator * fix(switch-properties): pass to the input valueRequiredValidator a variable instead of false * feat(resource-instance-form): scroll to the first invalid form field * fix(invalid-control-scroll-container): fix the spec file * fix(invalid-control-scroll): fix basic test * refactor(invalid-control-scroll): simplify the directives + update scrolling config * refactor: clean up + add some explanations * refactor(select-properties): update comment * refactor(select-properties): isPropRequired(id) returns a boolean * refactor(switch-properties): update comment * refactor(select-properties): update comments Co-authored-by: flaurens <flavie.laurens@unibas.ch>
- Loading branch information
Showing
13 changed files
with
228 additions
and
69 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
71 changes: 71 additions & 0 deletions
71
src/app/main/directive/invalid-control-scroll.directive.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,71 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; | ||
import { InvalidControlScrollDirective } from './invalid-control-scroll.directive'; | ||
|
||
@Component({ | ||
template: ` | ||
<form [formGroup]="form" (ngSubmit)="onSubmit()" appInvalidControlScroll> | ||
<div class="form-group"> | ||
<label for="control1">Field 1</label> | ||
<input class="form-control" formControlName="control1"/> | ||
</div> | ||
<div class="form-group"> | ||
<label for="control1">Field 2</label> | ||
<input class="form-control" formControlName="control2"/> | ||
</div> | ||
<div class="form-group"> | ||
<label for="control1">Field 3</label> | ||
<input class="form-control" formControlName="control3"/> | ||
</div> | ||
<button type="submit" class="btn btn-primary">Submit</button> | ||
</form> | ||
` | ||
}) | ||
class TestLinkHostComponent implements OnInit { | ||
|
||
form: FormGroup; | ||
|
||
constructor() { } | ||
|
||
ngOnInit() { | ||
this.form = new FormGroup({ | ||
control1: new FormControl(), | ||
control2: new FormControl(), | ||
control3: new FormControl() | ||
}); | ||
} | ||
|
||
onSubmit() { | ||
console.log('form submitted'); | ||
} | ||
} | ||
|
||
describe('InvalidControlScrollDirective', () => { | ||
|
||
let testHostComponent: TestLinkHostComponent; | ||
let testHostFixture: ComponentFixture<TestLinkHostComponent>; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ | ||
InvalidControlScrollDirective, | ||
TestLinkHostComponent | ||
], | ||
imports: [ | ||
ReactiveFormsModule | ||
] | ||
}); | ||
|
||
testHostFixture = TestBed.createComponent(TestLinkHostComponent); | ||
testHostComponent = testHostFixture.componentInstance; | ||
testHostFixture.detectChanges(); | ||
|
||
expect(testHostComponent).toBeTruthy(); | ||
|
||
}); | ||
|
||
it('should create an instance', () => { | ||
expect(testHostComponent).toBeTruthy(); | ||
}); | ||
}); |
37 changes: 37 additions & 0 deletions
37
src/app/main/directive/invalid-control-scroll.directive.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 { Directive, ElementRef, HostListener } from '@angular/core'; | ||
import { FormGroupDirective } from '@angular/forms'; | ||
|
||
@Directive({ | ||
selector: '[appInvalidControlScroll]' | ||
}) | ||
export class InvalidControlScrollDirective { | ||
|
||
constructor( | ||
private _el: ElementRef, | ||
private _formGroupDir: FormGroupDirective | ||
) { } | ||
|
||
@HostListener("ngSubmit") submitData() { | ||
if (this._formGroupDir.control.invalid) { | ||
this._scrollToFirstInvalidControl(); | ||
} | ||
} | ||
|
||
/** | ||
* Target the first invalid element of the resource-instance form (2nd panel property) and scroll to it | ||
*/ | ||
private _scrollToFirstInvalidControl() { | ||
// target the first invalid form field | ||
const firstInvalidControl: HTMLElement = this._el.nativeElement.querySelector( | ||
"form .ng-invalid" | ||
); | ||
|
||
// scroll to the first invalid element in a smooth way | ||
firstInvalidControl.scrollIntoView({ | ||
behavior: "smooth", | ||
block: "nearest", | ||
inline: "nearest" | ||
}); | ||
} | ||
|
||
} |
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
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
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
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
Oops, something went wrong.