-
Notifications
You must be signed in to change notification settings - Fork 385
/
datePicker.ts
129 lines (102 loc) 路 3.04 KB
/
datePicker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import TuiDatePicker from 'tui-date-picker';
import { CellEditor, CellEditorProps } from './types';
import { cls } from '../helper/dom';
import { deepMergedCopy, isNumber, isString, isUndefined, isNull } from '../helper/common';
import { Dictionary } from '../store/types';
export class DatePickerEditor implements CellEditor {
public el: HTMLDivElement;
private inputEl: HTMLInputElement;
private datePickerEl: TuiDatePicker;
private iconEl?: HTMLElement;
private createWrapper() {
const el = document.createElement('div');
el.className = cls('layer-datepicker');
return el;
}
private createInputElement() {
const inputEl = document.createElement('input');
inputEl.className = cls('content-text');
inputEl.type = 'text';
return inputEl;
}
private createCalendarWrapper() {
const calendarWrapper = document.createElement('div');
calendarWrapper.style.marginTop = '-4px';
this.el.appendChild(calendarWrapper);
return calendarWrapper;
}
private openDatePicker() {
this.datePickerEl.open();
}
private createIcon() {
const icon = document.createElement('i');
icon.className = cls('date-icon');
icon.addEventListener('click', () => {
this.openDatePicker();
});
return icon;
}
private focus() {
this.inputEl.focus();
}
public constructor(props: CellEditorProps) {
this.el = this.createWrapper();
this.inputEl = this.createInputElement();
const datepickerInputContainer = document.createElement('div');
datepickerInputContainer.className = cls('datepicker-input-container');
datepickerInputContainer.appendChild(this.inputEl);
this.el.appendChild(datepickerInputContainer);
const calendarWrapper = this.createCalendarWrapper();
const {
grid: { usageStatistics },
columnInfo,
value
} = props;
const options: Dictionary<any> = {
showIcon: true,
...columnInfo.editor!.options
};
if (options.showIcon) {
const icon = this.createIcon();
this.iconEl = icon;
this.inputEl.className = cls('datepicker-input');
datepickerInputContainer.appendChild(icon);
}
let date = isUndefined(value) || isNull(value) ? '' : new Date();
if (!options.format) {
options.format = 'yyyy-MM-dd';
}
if (isNumber(value) || isString(value)) {
date = new Date(value);
}
const defaultOptions = {
date,
type: 'date',
input: {
element: this.inputEl,
format: options.format
},
usageStatistics
};
this.datePickerEl = new TuiDatePicker(calendarWrapper, deepMergedCopy(defaultOptions, options));
this.datePickerEl.on('close', () => {
this.focus();
});
}
public getElement() {
return this.el;
}
public getValue() {
return this.inputEl.value;
}
public mounted() {
this.inputEl.select();
this.datePickerEl.open();
}
public beforeDestroy() {
if (this.iconEl) {
this.iconEl.removeEventListener('click', this.openDatePicker);
}
this.datePickerEl.destroy();
}
}