Skip to content
This repository was archived by the owner on Nov 13, 2024. It is now read-only.

Commit a450333

Browse files
authored
Merge pull request #138 from formio/bugfix/datetime-allow-manual-input
Fix(Datetime): support allowManualInput option
2 parents 6b4ff27 + b361646 commit a450333

File tree

1 file changed

+48
-38
lines changed

1 file changed

+48
-38
lines changed

projects/angular-material-formio/src/lib/components/date/date.component.ts

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -21,46 +21,46 @@ import {FormControl} from '@angular/forms';
2121
</mat-datepicker-toggle>
2222
<mat-form-field class="example-full-width">
2323
<input
24-
*ngIf="enableTime && enableDate"
25-
matInput
26-
type="datetime-local"
27-
[placeholder]="instance.component.placeholder"
28-
[formControl]="displayControl"
29-
(input)="onChange()"
30-
readonly
24+
*ngIf="enableTime && enableDate"
25+
matInput
26+
type="datetime-local"
27+
[placeholder]="instance.component.placeholder"
28+
[formControl]="displayControl"
29+
(input)="onChangeInput()"
30+
[readonly]="!allowManualInput"
3131
>
3232
<input
33-
*ngIf="enableTime && !enableDate"
34-
matInput
35-
[placeholder]="instance.component.placeholder"
36-
[formControl]="displayControl"
37-
[matMask]="formatTime"
38-
(input)="onChange()"
39-
readonly
33+
*ngIf="enableTime && !enableDate"
34+
matInput
35+
[placeholder]="instance.component.placeholder"
36+
[formControl]="displayControl"
37+
[matMask]="formatTime"
38+
(input)="onChangeInput()"
39+
[readonly]="!allowManualInput"
4040
>
4141
<input
42-
*ngIf="!enableTime && enableDate"
43-
matInput
44-
[placeholder]="instance.component.placeholder"
45-
[formControl]="displayControl"
46-
(input)="onChange()"
47-
readonly
42+
*ngIf="!enableTime && enableDate"
43+
matInput
44+
[placeholder]="instance.component.placeholder"
45+
[formControl]="displayControl"
46+
(input)="onChangeInput()"
47+
[readonly]="!allowManualInput"
4848
>
4949
</mat-form-field>
5050
5151
<mat-formio-calendar
52-
#calendar
53-
[minDate]="instance.component.datePicker.minDate || ''"
54-
[maxDate]="instance.component.datePicker.maxDate || ''"
55-
[dateFilter]="dateFilter"
56-
[hidden]="!isPickerOpened"
57-
(dateSelectEvent)="onChangeDate($event)"
58-
(timeSelectEvent)="onChangeTime($event)"
59-
[enableDate]="enableDate"
60-
[enableTime]="enableTime"
61-
[hourStep]="instance.component.timePicker.hourStep"
62-
[minuteStep]="instance.component.timePicker.minuteStep"
63-
[instance]="instance"
52+
#calendar
53+
[minDate]="instance.component.datePicker.minDate || ''"
54+
[maxDate]="instance.component.datePicker.maxDate || ''"
55+
[dateFilter]="dateFilter"
56+
[hidden]="!isPickerOpened"
57+
(dateSelectEvent)="onChangeDate($event)"
58+
(timeSelectEvent)="onChangeTime($event)"
59+
[enableDate]="enableDate"
60+
[enableTime]="enableTime"
61+
[hourStep]="instance.component.timePicker.hourStep"
62+
[minuteStep]="instance.component.timePicker.minuteStep"
63+
[instance]="instance"
6464
></mat-formio-calendar>
6565
<mat-error *ngIf="instance.error">{{ instance.error.message }}</mat-error>
6666
</form>
@@ -74,6 +74,7 @@ export class MaterialDateComponent extends MaterialComponent {
7474
public isPickerOpened: boolean;
7575
public selectedDate: any;
7676
public selectedTime: any = '00:00';
77+
public allowManualInput: boolean = true;
7778

7879
@ViewChild('calendar', {static: false}) calendar;
7980

@@ -110,6 +111,14 @@ export class MaterialDateComponent extends MaterialComponent {
110111
}
111112
}
112113

114+
onChangeInput() {
115+
const value = this.dateFilter(this.displayControl.value) &&
116+
this.checkMinMax(this.displayControl.value) ? this.displayControl.value : '';
117+
118+
this.control.setValue(value);
119+
this.onChange();
120+
}
121+
113122
getDateTimeValue() {
114123
let newDate = '';
115124
let isSelectedTime = false;
@@ -160,15 +169,16 @@ export class MaterialDateComponent extends MaterialComponent {
160169
this.isDisabled() ? this.displayControl.disable() : this.displayControl.enable();
161170

162171
if (this.instance) {
172+
this.allowManualInput = this.instance.component.allowInput === false ? false : true;
163173
if (this.instance.component && this.instance.component.datePicker) {
164-
const {minDate: min, maxDate: max} = this.instance.component.datePicker;
174+
const {minDate: min, maxDate: max} = this.instance.component.datePicker;
165175

166-
// It improves the date to the full format if the customer set only a year. Otherwise we will have conflicts into the moment.js.
167-
const { minDate, maxDate } = this.improveMinMaxDate(min, max);
168-
this.instance.component.datePicker.minDate = minDate;
169-
this.instance.component.datePicker.maxDate = maxDate;
176+
// It improves the date to the full format if the customer set only a year. Otherwise we will have conflicts into the moment.js.
177+
const { minDate, maxDate } = this.improveMinMaxDate(min, max);
178+
this.instance.component.datePicker.minDate = minDate;
179+
this.instance.component.datePicker.maxDate = maxDate;
170180
}
171-
}
181+
}
172182
}
173183

174184
toggleCalendar(event) {

0 commit comments

Comments
 (0)