LP1995623 DateTime picker gets min/max date support
authorBill Erickson <berickxx@gmail.com>
Thu, 3 Nov 2022 16:34:54 +0000 (12:34 -0400)
committerGalen Charlton <gmc@equinoxOLI.org>
Sat, 28 Jan 2023 19:53:25 +0000 (14:53 -0500)
Note this only affects the date select, since the time picker has no
notion of a minimum time.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
Signed-off-by: Susan Morrison <smorrison@georgialibraries.org>
Signed-off-by: Galen Charlton <gmc@equinoxOLI.org>

Open-ILS/src/eg2/src/app/share/datetime-select/datetime-select.component.html
Open-ILS/src/eg2/src/app/share/datetime-select/datetime-select.component.ts
Open-ILS/src/eg2/src/app/share/util/date.ts

index f25e29a..48f10cb 100644 (file)
         role="alert"
         class="alert alert-danger">
         <span class="material-icons">error</span>
-       {{firstError(controlDir.control.errors)}}
+        {{firstError(controlDir.control.errors)}}
       </div>
       <ngb-datepicker #datePicker
+        [minDate]="minDate"
+        [maxDate]="maxDate"
         formControlName="date"
         [footerTemplate]="time"
         (touch)="onTouched()">
index ed20e84..d9564a3 100644 (file)
@@ -4,6 +4,7 @@ import {AbstractControl, ControlValueAccessor, FormControl, FormGroup, NgControl
 import {NgbDatepicker, NgbTimeStruct, NgbDateStruct} from '@ng-bootstrap/ng-bootstrap';
 import {DatetimeValidator} from '@eg/share/validators/datetime_validator.directive';
 import * as moment from 'moment-timezone';
+import {DateUtil} from '@eg/share/util/date';
 
 @Component({
     selector: 'eg-datetime-select',
@@ -18,6 +19,10 @@ export class DateTimeSelectComponent implements OnInit, ControlValueAccessor {
     @Input() showTZ = true;
     @Input() timezone: string = this.format.wsOrgTimezone;
     @Input() readOnly = false;
+    @Input() noPast = false;
+    @Input() noFuture = false;
+    @Input() minDate: any;
+    @Input() maxDate: any;
     @Output() onChangeAsIso: EventEmitter<string>;
 
     dateTimeForm: FormGroup;
@@ -53,6 +58,12 @@ export class DateTimeSelectComponent implements OnInit, ControlValueAccessor {
     }
 
     ngOnInit() {
+        if (this.noPast) {
+            this.minDate = DateUtil.localYmdPartsFromDate();
+        }
+        if (this.noFuture) {
+            this.maxDate = DateUtil.localYmdPartsFromDate();
+        }
         if (!this.timezone) {
             this.timezone = this.format.wsOrgTimezone;
         }
index bca1596..681ba18 100644 (file)
@@ -1,6 +1,12 @@
 
 /* Utility code for dates */
 
+export interface YmdParts {
+    year: number;
+    month: number;
+    day: number;
+}
+
 export class DateUtil {
 
     /**
@@ -66,5 +72,15 @@ export class DateUtil {
             ((now.getMonth() + 1) + '').padStart(2, '0') + '-' +
             (now.getDate() + '').padStart(2, '0');
     }
+
+    static localYmdPartsFromDate(date?: Date): YmdParts {
+        const ymd = DateUtil.localYmdFromDate(date);
+        const parts = ymd.split(/-/);
+        return {
+            year: Number(parts[0]),
+            month: Number(parts[1]),
+            day: Number(parts[2])
+        };
+    }
 }