Seperated DateRange Inputs

pull/8/head
Max Ehrlicher-Schmidt 4 years ago
parent 3f4a01e2a6
commit 183b534775

@ -184,6 +184,9 @@
(element.newObject && column.required) || (element.newObject && column.required) ||
(element.isLockedByMe && column.required) (element.isLockedByMe && column.required)
" "
(validityChange)="
validityChange(element, column.dataPath, $event)
"
[(from)]="element[column.dataPath + '.from']" [(from)]="element[column.dataPath + '.from']"
[(to)]="element[column.dataPath + '.to']" [(to)]="element[column.dataPath + '.to']"
></app-date-range-cell> ></app-date-range-cell>

@ -1,25 +1,38 @@
<mat-form-field *ngIf="editable || label; else stringValue"> <div class="dateRange-wrapper" *ngIf="editable || label; else stringValue" [formGroup]="dateRangeGroup">
<mat-label *ngIf="label">{{label}}</mat-label> <mat-label *ngIf="label">{{ label }}</mat-label>
<mat-date-range-input [rangePicker]="picker" disabled class="date-range-input"> <mat-form-field class="date-range-input">
<input <input
matStartDate matInput
placeholder="Startdatum" placeholder="Startdatum"
[matDatepicker]="fromPicker"
[value]="from" [value]="from"
disabled
formControlName="from"
(dateChange)="startDateChange($event)" (dateChange)="startDateChange($event)"
/> />
<mat-datepicker-toggle matSuffix [for]="fromPicker"></mat-datepicker-toggle>
<mat-datepicker #fromPicker [disabled]="!editable"></mat-datepicker>
</mat-form-field>
<i class="spacer-string">bis</i>
<mat-form-field class="date-range-input">
<input <input
matEndDate matInput
placeholder="Enddatum" placeholder="Enddatum"
[matDatepicker]="toPicker"
[value]="to" [value]="to"
disabled
formControlName="to"
(dateChange)="endDateChange($event)" (dateChange)="endDateChange($event)"
/> />
</mat-date-range-input> <button mat-button *ngIf="editable && to" matSuffix mat-icon-button aria-label="Clear" (click)="endDateChange({value: null})">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle> <mat-icon>close</mat-icon>
<mat-date-range-picker </button>
#picker <mat-datepicker-toggle matSuffix [for]="toPicker"></mat-datepicker-toggle>
[disabled]="!editable" <mat-datepicker #toPicker [disabled]="!editable"></mat-datepicker>
></mat-date-range-picker>
</mat-form-field> </mat-form-field>
<ng-template #stringValue> </div>
<div class="date-string">{{toLocaleDate(from)}} <i>bis</i> {{toLocaleDate(to)}}</div> <ng-template #stringValue>
</ng-template> <div class="date-string">
{{ toLocaleDate(from) }} <i>bis</i> {{ toLocaleDate(to) }}
</div>
</ng-template>

@ -1,5 +1,9 @@
.date-range-input { .dateRange-wrapper {
min-width: 11em; display: flex;
align-items: center;
.spacer-string {
margin: 0 0.5em;
}
} }
.date-string { .date-string {
min-width: 10em; min-width: 10em;

@ -12,7 +12,7 @@ import { FormControl, FormGroup, Validators } from '@angular/forms';
@Component({ @Component({
selector: 'app-date-range-cell', selector: 'app-date-range-cell',
templateUrl: './date-range-cell.component.html', templateUrl: './date-range-cell.component.html',
styleUrls: ['./date-range-cell.component.scss'] styleUrls: ['./date-range-cell.component.scss'],
}) })
export class DateRangeCellComponent implements OnInit { export class DateRangeCellComponent implements OnInit {
@Input() @Input()
@ -34,19 +34,58 @@ export class DateRangeCellComponent implements OnInit {
@Output() validityChange = new EventEmitter<boolean>(); @Output() validityChange = new EventEmitter<boolean>();
isValid = true; isValid = true;
dateRangeGroup: FormGroup;
constructor(private cdr: ChangeDetectorRef, public datepipe: DatePipe) {} constructor(private cdr: ChangeDetectorRef, public datepipe: DatePipe) {}
ngOnInit() { ngOnInit() {
this.dateRangeGroup = new FormGroup({
from: new FormControl(),
to: new FormControl(),
});
this.dateRangeGroup.controls['from'].markAsTouched();
this.dateRangeGroup.controls['to'].markAsTouched();
if (this.editable) {
setTimeout(()=>this.checkIfDateRangeIsValid());
}
} }
startDateChange(event): void { startDateChange(event): void {
this.from = this.transformDate(event.value); this.from = this.transformDate(event.value);
this.fromChange.emit(this.from); this.fromChange.emit(this.from);
this.checkIfDateRangeIsValid();
} }
endDateChange(event) { endDateChange(event) {
this.to = this.transformDate(event.value); this.to = this.transformDate(event.value);
this.toChange.emit(this.to); this.toChange.emit(this.to);
this.checkIfDateRangeIsValid();
}
checkIfDateRangeIsValid() {
if (this.required && this.from == null) {
this.setRangeError(true);
return;
}
if (this.to == null || this.from == null) {
this.setRangeError(false);
return;
}
const isValid = new Date(this.to) >= new Date(this.from);
this.setRangeError(!isValid);
}
setRangeError(error: boolean): void {
console.log("error", error)
this.validityChange.emit(!error);
if (error) {
this.dateRangeGroup.controls['from'].setErrors({ rangeError: true });
this.dateRangeGroup.controls['to'].setErrors({ rangeError: true });
} else {
this.dateRangeGroup.controls['from'].setErrors(null);
this.dateRangeGroup.controls['to'].setErrors(null);
}
} }
transformDate(date) { transformDate(date) {
@ -54,10 +93,9 @@ export class DateRangeCellComponent implements OnInit {
} }
toLocaleDate(date: string): string { toLocaleDate(date: string): string {
if (date == "") { if (date == '') {
return ""; return '';
} }
return new Date(date).toLocaleDateString(); return new Date(date).toLocaleDateString();
} }
} }

Loading…
Cancel
Save