Skip to content

Commit 730fb8b

Browse files
Davide NegrettiLucaGiamminonni
Davide Negretti
authored andcommitted
Merged in DSC-828 (pull request DSpace#398)
2 parents a0a7dbd + 6dcada2 commit 730fb8b

7 files changed

+120
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<input required #string="ngModel" type="number" name="number-value-{{index}}" class="form-control" id="number-value-{{index}}" [ngModel]="value" (ngModelChange)="setValue($event)"/>
2+
<div *ngIf="string.invalid && (string.dirty || string.touched)"
3+
class="alert alert-danger validation-error">
4+
<div *ngIf="string.errors.required">
5+
{{'process.new.parameter.number.required' | translate}}
6+
</div>
7+
</div>

src/app/process-page/form/process-parameters/parameter-value-input/number-value-input/number-value-input.component.scss

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { ComponentFixture, fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';
2+
3+
import { FormsModule } from '@angular/forms';
4+
import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
5+
import { By } from '@angular/platform-browser';
6+
import { NumberValueInputComponent } from './number-value-input.component';
7+
import { TranslateLoaderMock } from '../../../../../shared/mocks/translate-loader.mock';
8+
9+
describe('NumberValueInputComponent', () => {
10+
let component: NumberValueInputComponent;
11+
let fixture: ComponentFixture<NumberValueInputComponent>;
12+
13+
beforeEach(waitForAsync(() => {
14+
TestBed.configureTestingModule({
15+
imports: [
16+
FormsModule,
17+
TranslateModule.forRoot({
18+
loader: {
19+
provide: TranslateLoader,
20+
useClass: TranslateLoaderMock
21+
}
22+
})],
23+
declarations: [NumberValueInputComponent],
24+
providers: [
25+
]
26+
})
27+
.compileComponents();
28+
}));
29+
30+
beforeEach(() => {
31+
fixture = TestBed.createComponent(NumberValueInputComponent);
32+
component = fixture.componentInstance;
33+
fixture.detectChanges();
34+
});
35+
36+
it('should create', () => {
37+
expect(component).toBeTruthy();
38+
});
39+
40+
it('should not show a validation error if the input field was left untouched but left empty', () => {
41+
const validationError = fixture.debugElement.query(By.css('.validation-error'));
42+
expect(validationError).toBeFalsy();
43+
});
44+
45+
it('should show a validation error if the input field was touched but left empty', fakeAsync(() => {
46+
component.value = '';
47+
fixture.detectChanges();
48+
tick();
49+
50+
const input = fixture.debugElement.query(By.css('input'));
51+
input.triggerEventHandler('blur', null);
52+
53+
fixture.detectChanges();
54+
55+
const validationError = fixture.debugElement.query(By.css('.validation-error'));
56+
expect(validationError).toBeTruthy();
57+
}));
58+
59+
it('should not show a validation error if the input field was touched but not left empty', fakeAsync(() => {
60+
component.value = 'testValue';
61+
fixture.detectChanges();
62+
tick();
63+
64+
const input = fixture.debugElement.query(By.css('input'));
65+
input.triggerEventHandler('blur', null);
66+
67+
fixture.detectChanges();
68+
69+
const validationError = fixture.debugElement.query(By.css('.validation-error'));
70+
expect(validationError).toBeFalsy();
71+
}));
72+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Component, Optional, Input } from '@angular/core';
2+
import { ValueInputComponent } from '../value-input.component';
3+
import { ControlContainer, NgForm } from '@angular/forms';
4+
import { controlContainerFactory } from '../../../process-form.component';
5+
6+
/**
7+
* Represents the user inputted value of a numeric parameter
8+
*/
9+
@Component({
10+
selector: 'ds-number-value-input',
11+
templateUrl: './number-value-input.component.html',
12+
styleUrls: ['./number-value-input.component.scss'],
13+
viewProviders: [ { provide: ControlContainer,
14+
useFactory: controlContainerFactory,
15+
deps: [[new Optional(), NgForm]] } ]
16+
})
17+
export class NumberValueInputComponent extends ValueInputComponent<string> {
18+
/**
19+
* The current value of the string
20+
*/
21+
value: string;
22+
23+
/**
24+
* Initial value of the field
25+
*/
26+
@Input() initialValue;
27+
28+
ngOnInit() {
29+
this.value = this.initialValue;
30+
}
31+
32+
setValue(value) {
33+
this.value = value;
34+
this.updateValue.emit(value);
35+
}
36+
}

src/app/process-page/form/process-parameters/parameter-value-input/parameter-value-input.component.html

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<div [ngSwitch]="parameter?.type">
22
<ds-string-value-input *ngSwitchCase="parameterTypes.STRING" [initialValue]="initialValue" (updateValue)="updateValue.emit($event)" [index]="index"></ds-string-value-input>
3+
<ds-number-value-input *ngSwitchCase="parameterTypes.NUMBER" [initialValue]="initialValue" (updateValue)="updateValue.emit($event)" [index]="index"></ds-number-value-input>
34
<ds-string-value-input *ngSwitchCase="parameterTypes.OUTPUT" [initialValue]="initialValue" (updateValue)="updateValue.emit($event)" [index]="index"></ds-string-value-input>
45
<ds-date-value-input *ngSwitchCase="parameterTypes.DATE" [initialValue]="initialValue" (updateValue)="updateValue.emit($event)" [index]="index"></ds-date-value-input>
56
<ds-file-value-input *ngSwitchCase="parameterTypes.FILE" (updateValue)="updateValue.emit($event)" [index]="index"></ds-file-value-input>

src/app/process-page/process-page.module.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { ProcessDetailFieldComponent } from './detail/process-detail-field/proce
1717
import { ProcessBreadcrumbsService } from './process-breadcrumbs.service';
1818
import { ProcessBreadcrumbResolver } from './process-breadcrumb.resolver';
1919
import { ProcessFormComponent } from './form/process-form.component';
20+
import { NumberValueInputComponent } from './form/process-parameters/parameter-value-input/number-value-input/number-value-input.component';
2021

2122
@NgModule({
2223
imports: [
@@ -37,7 +38,8 @@ import { ProcessFormComponent } from './form/process-form.component';
3738
ProcessOverviewComponent,
3839
ProcessDetailComponent,
3940
ProcessDetailFieldComponent,
40-
ProcessFormComponent
41+
ProcessFormComponent,
42+
NumberValueInputComponent
4143
],
4244
providers: [
4345
ProcessBreadcrumbResolver,

src/app/process-page/scripts/script-parameter-type.model.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
export enum ScriptParameterType {
55
STRING = 'String',
66
DATE = 'date',
7+
NUMBER = 'Integer',
78
BOOLEAN = 'boolean',
89
FILE = 'InputStream',
910
OUTPUT = 'OutputStream'

0 commit comments

Comments
 (0)