-
Notifications
You must be signed in to change notification settings - Fork 2
/
cosmoz-datetime-input.js
140 lines (127 loc) · 3.04 KB
/
cosmoz-datetime-input.js
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
130
131
132
133
134
135
136
137
138
139
140
import { html, component, useEffect, useMemo } from '@pionjs/pion';
import { notifyProperty } from '@neovici/cosmoz-utils/hooks/use-notify-property';
import '@neovici/cosmoz-input';
/**
* The possible separators between date and time input values
*/
const separators = ['T', ' '];
/**
* Returns a [date, time] or [date] array via a dateTime string.
* @param {String} aDateTime - The datetime or date string.
* @return {undefined}
*/
const getDateTimeParts = (aDateTime) => {
if (!aDateTime || typeof aDateTime !== 'string') {
return;
}
let parts;
separators.some((split) => {
if (!aDateTime.match(split)) {
return false;
}
parts = aDateTime.split(split);
return true;
});
return parts || [aDateTime];
};
/**
* Gets the min/max-date/time properties via min/max-dateTime strings.
* @param {String} min - The min Date/DateTime.
* @param {String} max - the max Date/DateTime.
* @return {undefined}
*/
const getMinMax = (min, max) => {
const minParts = getDateTimeParts(min),
maxParts = getDateTimeParts(max);
return {
minDate: Array.isArray(minParts) ? minParts.shift() : null,
minTime: Array.isArray(minParts) ? minParts.shift() : null,
maxDate: Array.isArray(maxParts) ? maxParts.shift() : null,
maxTime: Array.isArray(maxParts) ? maxParts.shift() : null,
};
};
export const toValue = (date, time) => {
if (!date && !time) {
return;
}
if (!date && time) {
return `T${time}`;
}
if (date && !time) {
return date;
}
return `${date}T${time}`;
};
export const fromValue = (value) => {
if (!value) {
return;
}
for (const split of separators) {
if (value.match(split)) {
const parts = value.split(split);
return {
date: parts.shift(),
time: parts.shift(),
};
}
}
return { date: value };
};
/**
* `cosmoz-datetime-input`
*
* Mimics `<input type="cosmoz-datetime-input">`
*
* @customElement
* @polymer
* @demo demo/index.html
*/
const DateTimeInput = (host) => {
const { dateLabel, timeLabel, min, max, step = '1', value } = host;
const { minDate, maxDate, minTime, maxTime } = useMemo(
() => getMinMax(min, max),
[min, max],
);
const { date, time } = useMemo(() => fromValue(value) ?? {}, [value]);
useEffect(() => {
host.dispatchEvent(
new CustomEvent('cosmoz-datetime-input-value-changed', {
bubbles: true,
composed: true,
}),
);
}, [value]);
return html`
<style>
:host {
display: flex;
flex-direction: row;
}
</style>
<cosmoz-input
label="${dateLabel}"
type="date"
.value="${date}"
@value-changed="${(e) =>
notifyProperty(host, 'value', toValue(e.target.value, time))}"
.min="${minDate}"
.max="${maxDate}"
></cosmoz-input>
<cosmoz-input
label="${timeLabel}"
type="time"
.value="${time}"
@value-changed="${(e) =>
notifyProperty(host, 'value', toValue(date, e.target.value))}"
step="${step}"
.min="${minTime}"
.max="${maxTime}"
></cosmoz-input>
`;
};
customElements.define(
'cosmoz-datetime-input',
component(DateTimeInput, {
observedAttributes: ['date-label', 'time-label', 'min', 'max', 'step'],
}),
);