From 183b5347753127f93db3d7eacf871e609f18659f Mon Sep 17 00:00:00 2001 From: Max Ehrlicher-Schmidt Date: Thu, 3 Dec 2020 23:17:55 +0100 Subject: [PATCH] Seperated DateRange Inputs --- src/app/components/table/table.component.html | 3 + .../date-range-cell.component.html | 63 +++++++++++-------- .../date-range-cell.component.scss | 8 ++- .../date-range-cell.component.ts | 48 ++++++++++++-- 4 files changed, 90 insertions(+), 32 deletions(-) diff --git a/src/app/components/table/table.component.html b/src/app/components/table/table.component.html index d151df6..fd5c353 100644 --- a/src/app/components/table/table.component.html +++ b/src/app/components/table/table.component.html @@ -184,6 +184,9 @@ (element.newObject && column.required) || (element.isLockedByMe && column.required) " + (validityChange)=" + validityChange(element, column.dataPath, $event) + " [(from)]="element[column.dataPath + '.from']" [(to)]="element[column.dataPath + '.to']" > diff --git a/src/app/components/tableComponents/date-range-cell/date-range-cell.component.html b/src/app/components/tableComponents/date-range-cell/date-range-cell.component.html index 0e27bdc..40e482a 100644 --- a/src/app/components/tableComponents/date-range-cell/date-range-cell.component.html +++ b/src/app/components/tableComponents/date-range-cell/date-range-cell.component.html @@ -1,25 +1,38 @@ - - {{label}} - - - - - - - - -
{{toLocaleDate(from)}} bis {{toLocaleDate(to)}}
-
+
+ {{ label }} + + + + + + bis + + + + + + +
+ +
+ {{ toLocaleDate(from) }} bis {{ toLocaleDate(to) }} +
+
diff --git a/src/app/components/tableComponents/date-range-cell/date-range-cell.component.scss b/src/app/components/tableComponents/date-range-cell/date-range-cell.component.scss index dbc6e25..a0d4828 100644 --- a/src/app/components/tableComponents/date-range-cell/date-range-cell.component.scss +++ b/src/app/components/tableComponents/date-range-cell/date-range-cell.component.scss @@ -1,5 +1,9 @@ -.date-range-input { - min-width: 11em; +.dateRange-wrapper { + display: flex; + align-items: center; + .spacer-string { + margin: 0 0.5em; + } } .date-string { min-width: 10em; diff --git a/src/app/components/tableComponents/date-range-cell/date-range-cell.component.ts b/src/app/components/tableComponents/date-range-cell/date-range-cell.component.ts index 90c46ba..e88f119 100644 --- a/src/app/components/tableComponents/date-range-cell/date-range-cell.component.ts +++ b/src/app/components/tableComponents/date-range-cell/date-range-cell.component.ts @@ -12,7 +12,7 @@ import { FormControl, FormGroup, Validators } from '@angular/forms'; @Component({ selector: 'app-date-range-cell', templateUrl: './date-range-cell.component.html', - styleUrls: ['./date-range-cell.component.scss'] + styleUrls: ['./date-range-cell.component.scss'], }) export class DateRangeCellComponent implements OnInit { @Input() @@ -26,7 +26,7 @@ export class DateRangeCellComponent implements OnInit { @Input() editable = false; - + @Input() required = false; @Input() @@ -34,19 +34,58 @@ export class DateRangeCellComponent implements OnInit { @Output() validityChange = new EventEmitter(); isValid = true; + dateRangeGroup: FormGroup; + constructor(private cdr: ChangeDetectorRef, public datepipe: DatePipe) {} 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 { this.from = this.transformDate(event.value); this.fromChange.emit(this.from); + this.checkIfDateRangeIsValid(); } endDateChange(event) { this.to = this.transformDate(event.value); 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) { @@ -54,10 +93,9 @@ export class DateRangeCellComponent implements OnInit { } toLocaleDate(date: string): string { - if (date == "") { - return ""; + if (date == '') { + return ''; } return new Date(date).toLocaleDateString(); } - }