-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathct-date.ts
More file actions
executable file
·426 lines (393 loc) · 11.4 KB
/
Copy pathct-date.ts
File metadata and controls
executable file
·426 lines (393 loc) · 11.4 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import "./ct-input-container.js";
import { TemplateResult, css, html } from "lit";
import { CtLit, customElement, property, query } from "./ct-lit.js";
/**
* ## `ct-date`
* A simple cross-platform date input component with optional time selection.
*
* ### Usage
* ```html
* <!-- Basic date input -->
* <ct-date label="Select Date"></ct-date>
*
* <!-- Date input with time selection -->
* <ct-date label="Select Date and Time" showhour></ct-date>
*
* <!-- Date input without day selection -->
* <ct-date label="Select Month/Year" nodd></ct-date>
* ```
*
* ### Events
* - `value`: Fired when a valid date is selected, with timestamp in seconds as detail
*
* ### Features
* - Supports date input with or without time
* - Optional day field
* - Timezone awareness option
* - Range validation for year input
* - Format parsing from pasted text
*
* @group lit-ct-components
* @element ct-date
* @fires value - Fired when a valid date is selected with timestamp in seconds as detail
*/
@customElement("ct-date")
export class CtDate extends CtLit {
/**
* When true, hides the day input field
*/
@property({ type: Boolean }) nodd = false;
/**
* Show hour and minutes
*/
@property({ type: Boolean }) showhour = false;
/**
* Return timestamp with timezone value
*/
@property({ type: Boolean }) usetimezone = false;
@property({ type: String }) placeholder = "";
@property({ type: Number }) minYYYY = 1800;
@property({ type: Number }) maxYYYY = 2300;
@property({ type: String }) label = "";
@property({ type: Boolean }) required = false;
@property({ type: Boolean }) hidden = false;
@property({ type: Boolean }) invalid: boolean = false;
@property({ type: String }) mm = "";
@property({ type: String }) dd = "";
@property({ type: String }) yyyy = "";
@property({ type: String }) hh = "";
@property({ type: String }) min = "";
@query("#dd") $dd?: HTMLInputElement;
@query("#mm") $mm?: HTMLInputElement;
@query("#yyyy") $yyyy?: HTMLInputElement;
@query("#hh") $hh?: HTMLInputElement;
@query("#min") $min?: HTMLInputElement;
@query("#container") $container?: HTMLElementTagNameMap["ct-input-container"];
static styles: any[] = [
css`
:host {
display: inline-flex;
color: rgba(0, 0, 0, 0.54);
margin-bottom: 8px;
}
span {
display: flex;
align-items: center;
}
*:focus {
outline: 0;
}
input {
font: inherit;
outline: none;
box-shadow: none;
border: none;
width: auto;
text-align: center;
background: transparent;
color: inherit;
height: 100%;
padding: 0;
}
::-webkit-input-placeholder {
color: inherit;
opacity: 0.5;
}
#container {
flex: 1;
margin: 0;
}
#dd,
#mm,
#hh,
#min {
min-width: 2em;
}
#yyyy {
min-width: 3em;
}
`
];
constructor() {
super();
// on paste "12/12/2020" or "12/12/2020 12:12", fill data properly
this.addEventListener("paste", (e: ClipboardEvent) => {
let data = e.clipboardData?.getData("text/plain");
this.plainTextToDate(data);
});
}
/**
* Parses a date string and sets component values
* Supports formats: DD/MM/YYYY, YYYY-MM-DD, and optional time HH:MM
* @param {string} data - The date string to parse
*/
plainTextToDate(data?: string) {
if (data && (data.includes("/") || data.includes("-"))) {
let splitter = data.includes("/") ? "/" : "-";
let parts = data.split(" ");
let date = parts[0].split(splitter);
if (date.length == 3) {
if (date[2].length == 4) {
this.dd = date[0];
this.mm = date[1];
this.yyyy = date[2];
} else if (date[0].length == 4) {
this.dd = date[2];
this.mm = date[1];
this.yyyy = date[0];
}
}
if (parts.length == 2) {
let time = parts[1].split(":");
if (time.length == 2) {
this.hh = time[0];
this.min = time[1];
}
}
}
}
/**
* Renders the date input component
* @returns {TemplateResult} The rendered template
*/
render(): TemplateResult {
return html`
<ct-input-container id="container" ?invalid=${this.invalid} .label="${this.label}" .placeholder="${this.placeholder}" .value=${this.placeholder} ?required="${this.required}">
<slot slot="prefix" name="prefix"></slot>
<span slot="input" @focus=${this._onFocus}>
<span ?hidden="${this.nodd}">
<input
id="dd"
maxlength="2"
size="2"
placeholder="DD"
type="tel"
.value=${this.dd}
@input=${() => this.validX("dd", this.$dd?.value || "", 0, 31)}
onkeypress="return event.charCode >= 48 && event.charCode <= 57"
/>
<span>/</span>
</span>
<input
id="mm"
maxlength="2"
size="2"
placeholder="MM"
type="tel"
.value=${this.mm}
@input=${() => this.validX("mm", this.$mm?.value || "", 0, 12)}
onkeypress="return event.charCode >= 48 && event.charCode <= 57"
/>
<span>/</span>
<input
id="yyyy"
maxlength="4"
size="4"
placeholder="YYYY"
type="tel"
.value=${this.yyyy}
@input=${() => this.validX("yyyy", this.$yyyy?.value || "", this.minYYYY, this.maxYYYY)}
onkeypress="return event.charCode >= 48 && event.charCode <= 57"
/>
${this.showhour
? html`<span>
<span> @ </span>
<input
id="hh"
maxlength="2"
size="2"
placeholder="HH"
type="tel"
.value=${this.hh}
@input=${() => this.validX("hh", this.$hh?.value || "", 0, 24)}
onkeypress="return event.charCode >= 48 && event.charCode <= 57"
/>
<span>:</span>
<input
id="min"
maxlength="2"
size="2"
placeholder="mm"
type="tel"
.value=${this.min}
@input=${() => this.validX("min", this.$min?.value || "", 0, 59)}
onkeypress="return event.charCode >= 48 && event.charCode <= 57"
/>
</span>`
: ""}
</span>
</ct-input-container>
`;
}
/**
* Validates and sets a value for a specific part of the date/time
* @param {string} attr - The attribute to validate (yyyy, mm, dd, hh, min)
* @param {string} val - The value to validate
* @param {number} min - The minimum allowed value
* @param {number} max - The maximum allowed value
*/
validX(attr: "yyyy" | "mm" | "dd" | "hh" | "min", val: string, min: number, max: number) {
this.resetInvalid();
let v = parseInt(val);
if (`${v}` == "NaN") {
this[attr] = ``;
} else if (v < min && `${v}`.length == `${min}`.length) {
this[attr] = this.addZero(min);
} else if (v > max) {
this[attr] = this.addZero(max);
} else {
this[attr] = val;
}
if (!this.showhour) {
this.hh = "";
this.min = "";
}
// focus on next input
if (attr == "dd" && (v > 9 || (this.dd.startsWith("0") && this.dd.length == 2))) {
this.$mm?.focus();
} else if (attr == "mm" && (v > 9 || (this.mm.startsWith("0") && this.mm.length == 2))) {
this.$yyyy?.focus();
}
// this.fireValue();
let unix = Math.floor(
new Date(
`${this.yyyy}-${this.addZero(this.mm)}-${this.addZero(this.dd)}T${this.addZero(this.hh || "00", "0")}:${this.addZero(this.min || "00", "0")}${this.usetimezone ? "" : "Z"}`
).getTime() / 1000
);
if (`NaN` != `${unix}`) {
this.dispatchEvent(new CustomEvent("value", { detail: unix }));
}
}
/**
* Sets the date value from a timestamp or string
* @param {number|string|undefined} val - Timestamp in seconds or date string
*/
set value(val: number | string | undefined) {
this.loadValue(val);
}
/**
* Gets the current date value as a timestamp in seconds
* @returns {number|undefined} Timestamp in seconds or undefined if invalid
*/
get value(): number | undefined {
// Si existe this.nodd, establece this.dd en '02'
if (this.nodd) {
this.dd = "01";
}
// Si this.showhour es verdadero y falta alguna de las propiedades requeridas, retorna undefined
// Si falta alguna de las propiedades requeridas, retorna undefined
if (!this.yyyy || !this.mm || !this.dd) {
return undefined;
} else if (this.showhour && (!this.hh || !this.min)) {
return undefined;
}
// Construye una fecha basada en las propiedades proporcionadas
let dateStr = `${this.yyyy}-${this.addZero(this.mm)}-${this.addZero(this.dd)}T${
this.showhour ? `${this.addZero(this.hh, "0")}:${this.addZero(this.min, "0")}` : "00:00:00"
}${this.usetimezone ? "" : "Z"}`;
// Obtiene el timestamp en segundos de la fecha construida
let val = Math.floor(new Date(dateStr).getTime() / 1000);
// Si val es NaN o 0, retorna undefined; de lo contrario, retorna val
return val || undefined;
}
/**
* Gets the current date in standard format
* @returns {string|undefined} Date in YYYY-MM-DD or YYYY-MM-DDThh:mm format, or undefined if invalid
*/
get valueFormat() {
if (this.showhour && (!this.yyyy || !this.mm || !this.dd || !this.hh || !this.min)) return undefined;
if (!this.yyyy || !this.mm || !this.dd) return undefined;
if (!this.showhour) return `${this.yyyy}-${this.addZero(this.mm)}-${this.addZero(this.dd)}`;
else return `${this.yyyy}-${this.addZero(this.mm)}-${this.addZero(this.dd)}T${this.addZero(this.hh, "0")}:${this.addZero(this.min, "0")}${this.usetimezone ? "" : "Z"}`;
}
/**
* Resets the invalid state
*/
resetInvalid() {
this.invalid = false;
//this.$container?.invalid = false;
}
/**
* Loads a date value from a timestamp or string
* @param {number|string|undefined} value - Timestamp in seconds or date string
*/
loadValue(value?: number | string) {
if (typeof value == "string") {
this.plainTextToDate(value);
} else {
if (value != null && value != -1) {
let d = new Date(value * 1000);
if (this.usetimezone) {
this.dd = `${d.getDate()}`.padStart(2, "0");
this.mm = `${d.getMonth() + 1}`.padStart(2, "0");
this.yyyy = `${d.getFullYear()}`.padStart(4, "0");
this.hh = `${d.getHours()}`.padStart(2, "0");
this.min = `${d.getMinutes()}`.padStart(2, "0");
} else {
this.dd = `${d.getUTCDate()}`.padStart(2, "0");
this.mm = `${d.getUTCMonth() + 1}`.padStart(2, "0");
this.yyyy = `${d.getUTCFullYear()}`.padStart(4, "0");
this.hh = `${d.getUTCHours()}`.padStart(2, "0");
this.min = `${d.getUTCMinutes()}`.padStart(2, "0");
}
} else {
this.dd = "";
this.mm = "";
this.yyyy = "";
this.hh = "";
this.min = "";
}
}
}
/**
* Handles focus event on the input
* @private
*/
_onFocus() {
if (this.$container) {
this.resetInvalid();
}
}
/**
* Validates the input and sets invalid state if needed
* @returns {boolean} True if valid, false otherwise
*/
validate() {
this.resetInvalid();
if (this.value == undefined) {
this.invalid = true;
this.$container!.invalid = true;
}
return !this.invalid;
}
/**
* Checks if a number is valid (not null, not zero, not NaN)
* @param {number} num - The number to validate
* @returns {boolean} True if valid, false otherwise
*/
validNumber(num: number) {
return num != null && num != 0 && `${num}` != `NaN`;
}
/**
* Adds a leading zero to numbers less than 10
* @param {number|string} n - The number to format
* @param {string} onError - The value to use if n is NaN
* @returns {string} Formatted number with leading zero if needed
*/
addZero(n: number | string, onError = "1") {
if (`${n}` == "") return "";
n = Number(n);
if (`${n}` == "NaN") {
return `0${onError}`;
}
if (n < 10) {
return `0${n}`;
}
return `${n}`;
}
}
declare global {
interface HTMLElementTagNameMap {
"ct-date": CtDate;
}
}