-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into fb-RFDAP-1136-custo…
…m-buttons-bar
- Loading branch information
Showing
17 changed files
with
293 additions
and
8 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
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
8 changes: 8 additions & 0 deletions
8
src/ng-xform/date-range-field/date-range-field.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,8 @@ | ||
<ng-xform-form-control-layout [fieldComponent]="instance"> | ||
<input bsDaterangepicker type="text" class="form-control" [formControl]="componentControl" id="{{elementId}}" | ||
[bsConfig]="config" | ||
[bsValue]="field.initialValue" | ||
[placement]="field.placement" | ||
[maxDate]="field.maxDate" | ||
[minDate]="field.minDate" /> | ||
</ng-xform-form-control-layout> |
127 changes: 127 additions & 0 deletions
127
src/ng-xform/date-range-field/date-range-field.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,127 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { Component, ViewChild } from '@angular/core'; | ||
import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing'; | ||
import { FormControl, FormsModule, ReactiveFormsModule } from '@angular/forms'; | ||
import { BsDatepickerModule } from 'ngx-bootstrap/datepicker'; | ||
|
||
import { tickAndDetectChanges } from '../../testing/helpers'; | ||
import { OptionalTagComponent } from '../field-components/optional-tag.component'; | ||
import { ErrorMessagePipe } from '../field-error-message/error-message.pipe'; | ||
import { FieldErrorMessageComponent } from '../field-error-message/field-error-message.component'; | ||
import { DateRangeField } from '../fields'; | ||
import { FormControlLayoutComponent } from '../form-control-layout/form-control-layout.component'; | ||
import { NgXformGroup } from '../ng-xform-group'; | ||
import { DateRangeFieldComponent } from './date-range-field.component'; | ||
|
||
|
||
describe('DateRangeFieldComponent', () => { | ||
it('should create DateRangeField', () => { | ||
const fixture = createTestingModule(` | ||
<ng-xform-date-range-field [field]="field" [form]="form"> | ||
</ng-xform-date-range-field> | ||
`); | ||
|
||
expect(fixture.componentInstance).toBeTruthy(); | ||
}); | ||
|
||
it('should patch value to DateRangeField', fakeAsync(() => { | ||
const fixture = createTestingModule(` | ||
<form [formGroup]="form"> | ||
<ng-xform-date-range-field [field]="field" | ||
[formControlName]="field.key" | ||
[form]="form" | ||
[(ngModel)]="dateValue"> | ||
</ng-xform-date-range-field> | ||
</form> | ||
`); | ||
tickAndDetectChanges(fixture); | ||
const dateRangeField: DateRangeFieldTestComponent = fixture.componentInstance; | ||
|
||
expect(dateRangeField.dateValue).toBe(undefined); | ||
expect(dateRangeField.dateRangeField.componentControl.value).toBe(undefined); | ||
|
||
const changeTo = [new Date(2018, 1, 28, 0, 0, 0), new Date(2018, 1, 28, 0, 0, 0)]; | ||
dateRangeField.form.patchValue({date: changeTo}); | ||
tickAndDetectChanges(fixture); | ||
|
||
expect(dateRangeField.dateRangeField.componentControl.value[0].toISOString()).toBe(changeTo[0].toISOString()); | ||
expect(dateRangeField.dateRangeField.componentControl.value[1].toISOString()).toBe(changeTo[1].toISOString()); | ||
|
||
})); | ||
|
||
it('should be set to null when form resets ', fakeAsync(() => { | ||
const fixture = createTestingModule(` | ||
<form [formGroup]="form"> | ||
<ng-xform-date-range-field [field]="field" | ||
[formControlName]="field.key" | ||
[form]="form" | ||
[(ngModel)]="dateValue"> | ||
</ng-xform-date-range-field> | ||
</form> | ||
`); | ||
tickAndDetectChanges(fixture); | ||
const dateRangeField: DateRangeFieldTestComponent = fixture.componentInstance; | ||
|
||
const changeTo = [new Date(2018, 1, 28, 0, 0, 0), new Date(2018, 1, 28, 0, 0, 0)]; | ||
|
||
dateRangeField.form.patchValue({date: changeTo}); | ||
tickAndDetectChanges(fixture); | ||
|
||
expect(dateRangeField.dateRangeField.componentControl.value[0].toISOString()).toBe(changeTo[0].toISOString()); | ||
expect(dateRangeField.dateRangeField.componentControl.value[1].toISOString()).toBe(changeTo[1].toISOString()); | ||
|
||
|
||
dateRangeField.form.reset(); | ||
tickAndDetectChanges(fixture); | ||
|
||
expect(dateRangeField.dateValue).toBe(null); | ||
expect(dateRangeField.dateRangeField.componentControl.value).toBe(null); | ||
|
||
})); | ||
|
||
}); | ||
|
||
function createTestingModule( template: string): ComponentFixture<any> { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
FormsModule, | ||
ReactiveFormsModule, | ||
CommonModule, | ||
BsDatepickerModule.forRoot(), | ||
], | ||
declarations: [ | ||
DateRangeFieldTestComponent, | ||
DateRangeFieldComponent, | ||
FormControlLayoutComponent, | ||
FieldErrorMessageComponent, | ||
OptionalTagComponent, | ||
ErrorMessagePipe, | ||
] | ||
}).overrideComponent(DateRangeFieldTestComponent, { | ||
set: { | ||
template: template | ||
} | ||
}).compileComponents(); | ||
|
||
const fixture = TestBed.createComponent(DateRangeFieldTestComponent); | ||
fixture.detectChanges(); | ||
|
||
return fixture; | ||
} | ||
|
||
|
||
@Component({ | ||
template: ``, | ||
}) | ||
class DateRangeFieldTestComponent { | ||
@ViewChild(DateRangeFieldComponent) dateRangeField: DateRangeFieldComponent; | ||
|
||
field = new DateRangeField({ | ||
key: 'date', | ||
label: 'Date', | ||
}); | ||
|
||
form = new NgXformGroup({date: new FormControl()}); | ||
|
||
dateValue: Date; | ||
} |
88 changes: 88 additions & 0 deletions
88
src/ng-xform/date-range-field/date-range-field.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,88 @@ | ||
import { DateRangeField } from './../fields/date-range-field'; | ||
import { DatePipe } from '@angular/common'; | ||
import { ControlValueAccessor, FormControl, NG_VALUE_ACCESSOR } from '@angular/forms'; | ||
import { Component, AfterContentInit, ElementRef, AfterViewInit, Inject, LOCALE_ID } from '@angular/core'; | ||
import { BsDatepickerConfig } from 'ngx-bootstrap/datepicker'; | ||
|
||
import { BaseDynamicFieldComponent } from '../field-components/base-dynamic-field.component'; | ||
import { DateField } from '../fields'; | ||
|
||
/** | ||
* Component to generate a bootstrap form field of Date type | ||
* | ||
* :editing: Flag to control component state | ||
* :form: FormGroup containing the field | ||
* :field: Intance of field configurations | ||
*/ | ||
@Component({ | ||
selector: 'ng-xform-date-range-field', | ||
templateUrl: './date-range-field.component.html', | ||
providers: [{ | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: DateRangeFieldComponent, | ||
multi: true | ||
}], | ||
}) | ||
export class DateRangeFieldComponent extends BaseDynamicFieldComponent<DateRangeField> implements AfterViewInit, AfterContentInit, | ||
ControlValueAccessor { | ||
config: Partial<BsDatepickerConfig>; | ||
componentControl = new FormControl(); | ||
private input: HTMLInputElement; | ||
|
||
_onChange = (value: any) => { }; | ||
_onTouched = () => { }; | ||
|
||
// the elementRef will be used to get the input element after the view is initialized. | ||
constructor(private elementRef: ElementRef, @Inject(LOCALE_ID) private locale: string) { | ||
super(); | ||
this.componentControl.valueChanges.subscribe((val: any) => { | ||
// replay changes from view to the form value | ||
this._onChange(val); | ||
}); | ||
} | ||
|
||
ngAfterContentInit() { | ||
this.componentControl.setValidators(this.field.validators); | ||
this.config = { | ||
containerClass: `theme-${this.field.theme}`, | ||
}; | ||
} | ||
|
||
ngAfterViewInit() { | ||
this.input = this.elementRef.nativeElement.querySelector('input'); | ||
if (this.input) { | ||
this.input.onblur = this._onTouched | ||
} | ||
} | ||
|
||
writeValue(obj: any): void { | ||
if (obj instanceof Array) { | ||
this.componentControl.setValue(obj.map(item => new Date(item))); | ||
} else { | ||
this.componentControl.setValue(obj); | ||
} | ||
} | ||
|
||
registerOnChange(fn: any): void { | ||
this._onChange = fn; | ||
} | ||
|
||
registerOnTouched(fn: any): void { | ||
this._onTouched = fn; | ||
} | ||
|
||
setDisabledState?(isDisabled: boolean): void { | ||
this.input.disabled = isDisabled; | ||
} | ||
|
||
get formattedValue(): string { | ||
const dateFormatter = new DatePipe(this.field.locale || this.locale); | ||
if (!this.form.controls[this.elementId].value) { | ||
return '-'; | ||
} | ||
const start = dateFormatter.transform(this.form.controls[this.elementId].value[0], 'mediumDate'); | ||
const end = dateFormatter.transform(this.form.controls[this.elementId].value[1], 'mediumDate'); | ||
return `${start} - ${end}`; | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
src/ng-xform/field-error-message/field-error-message.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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<div class="help-block" *ngIf="control && control.invalid && control.touched"> | ||
<div *ngIf="control.errors"> | ||
<div *ngIf="control.errors" class="alert alert-danger" role="alert" style="padding: 5px;"> | ||
{{ control.errors | errorMessage }} | ||
</div> | ||
</div> |
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,9 @@ | ||
import { DateField } from './date-field'; | ||
|
||
export class DateRangeField extends DateField { | ||
public controlType ? = 'DATERANGE'; | ||
|
||
constructor(options: DateRangeField) { | ||
super(options); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<form [formGroup]="form" [class.form-horizontal]="horizontalForm"> | ||
<ng-xform-form-group [isHorizontal]="horizontalForm" [labelWidth]="labelWidth" [fields]='fields' [form]=form [editing]='editing'></ng-xform-form-group> | ||
</form> | ||
</form> |
Oops, something went wrong.