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

Fix/datetime two calendars #141

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@angular/material": "^10.0.0 | ^11.0.0"
},
"dependencies": {
"angular2-text-mask": "^9.0.0",
"tslib": "^2.0.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ import { FormioFormFieldComponent } from './components/formio-form-field/formio-
import { LabelComponent } from './components/label/label.component';
import { MaskDirective } from './directives/mask.directive';

import { TextMaskModule } from 'angular2-text-mask';

export {
FormioComponent,
MaterialButtonComponent,
Expand Down Expand Up @@ -144,6 +146,7 @@ export {
MaskDirective
],
imports: [
TextMaskModule,
CommonModule,
FormsModule,
ReactiveFormsModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Component, ViewChild } from '@angular/core'
import { MaterialComponent } from '../MaterialComponent';
import DateTimeComponent from 'formiojs/components/datetime/DateTime.js';
import { momentDate } from 'formiojs/utils/utils.js';
import { momentDate, convertFormatToMoment, convertFormatToMask, getInputMask } from 'formiojs/utils/utils.js';
import {FormControl} from '@angular/forms';
@Component({
selector: 'mat-formio-date',
Expand All @@ -21,29 +21,11 @@ import {FormControl} from '@angular/forms';
</mat-datepicker-toggle>
<mat-form-field class="example-full-width">
<input
*ngIf="enableTime && enableDate"
matInput
type="datetime-local"
[placeholder]="instance.component.placeholder"
[formControl]="displayControl"
(input)="onChangeInput()"
[readonly]="!allowManualInput"
>
<input
*ngIf="enableTime && !enableDate"
matInput
[placeholder]="instance.component.placeholder"
[formControl]="displayControl"
[matMask]="formatTime"
(input)="onChangeInput()"
[readonly]="!allowManualInput"
>
<input
*ngIf="!enableTime && enableDate"
matInput
[placeholder]="instance.component.placeholder"
[formControl]="displayControl"
(input)="onChangeInput()"
[textMask]="{mask: getMask()}"
(input)="onChange"
[readonly]="!allowManualInput"
>
</mat-form-field>
Expand Down Expand Up @@ -87,19 +69,38 @@ export class MaterialDateComponent extends MaterialComponent {
}

setDisplayControlValue(value = null) {
const format = `YYYY-MM-DD${this.enableTime ? 'THH:mm' : ''}`;
const format = this.getFormat();

value = value || this.getDateTimeValue();

if (value) {
this.displayControl.setValue(momentDate(value).format(format));
value = momentDate(value).format(format);
this.displayControl.setValue(value);
}
else {
this.displayControl.setValue('');
}
}

getFormat() {
const {format} = this.instance.component;

if (!format) {
return 'YYYY-MM-DD hh:mm A';
}

return convertFormatToMoment(format);
}

getMask() {
const {format} = this.instance.component;
const formioFormat = convertFormatToMask(format);

return getInputMask(formioFormat);
}

onChangeDate(event) {
this.selectedDate = momentDate(event).utc().format();
this.selectedDate = momentDate(event).format(this.getFormat());
this.control.setValue(this.selectedDate);
this.setDateTime();
}
Expand All @@ -111,14 +112,6 @@ export class MaterialDateComponent extends MaterialComponent {
}
}

onChangeInput() {
const value = this.dateFilter(this.displayControl.value) &&
this.checkMinMax(this.displayControl.value) ? this.displayControl.value : '';

this.control.setValue(value);
this.onChange();
}

getDateTimeValue() {
let newDate = '';
let isSelectedTime = false;
Expand All @@ -138,7 +131,6 @@ export class MaterialDateComponent extends MaterialComponent {
? momentDate(this.selectedDate)
.hours(Number.parseInt(hours))
.minutes(Number.parseInt(minutes))
.utc()
: this.selectedDate;
}

Expand All @@ -151,13 +143,11 @@ export class MaterialDateComponent extends MaterialComponent {
newDate = momentDate(new Date())
.hours(Number.parseInt(hours))
.minutes(Number.parseInt(minutes))
.seconds(0)
.utc();
.seconds(0);
}

return newDate;
}

setDateTime() {
this.control.setValue(this.getDateTimeValue());
this.onChange();
Expand Down Expand Up @@ -206,11 +196,13 @@ export class MaterialDateComponent extends MaterialComponent {
return readonly || disabled || this.instance.root.options.readOnly
}

public formatTime = (value) => {
public formatTime = (value) => {
const format = this.getFormat();

if (!value) {
return this.instance.emptyValue;
return format;
}
return momentDate(value).format(this.instance.component.format);
return momentDate(value).format(format);
}

setValue(value) {
Expand Down