svelte-date-input 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +42 -2
- package/dist/DateInput.svelte +38 -0
- package/dist/DateInput.svelte.d.ts +16 -0
- package/dist/Divider.svelte +13 -0
- package/dist/Divider.svelte.d.ts +7 -0
- package/dist/SegmentInput.svelte +106 -0
- package/dist/SegmentInput.svelte.d.ts +22 -0
- package/dist/SegmentSelect.svelte +60 -0
- package/dist/SegmentSelect.svelte.d.ts +19 -0
- package/dist/SegmentedInput.svelte +363 -0
- package/dist/SegmentedInput.svelte.d.ts +54 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +8 -0
- package/dist/shared/fields.d.ts +51 -0
- package/dist/shared/fields.js +163 -0
- package/dist/shared/format.d.ts +19 -0
- package/dist/shared/format.js +99 -0
- package/dist/shared/types.d.ts +6 -0
- package/dist/shared/types.js +1 -0
- package/package.json +69 -4
- package/src/DateInput.svelte +38 -0
- package/src/Divider.svelte +13 -0
- package/src/SegmentInput.svelte +106 -0
- package/src/SegmentSelect.svelte +60 -0
- package/src/SegmentedInput.svelte +363 -0
- package/src/index.ts +28 -0
- package/src/shared/fields.ts +245 -0
- package/src/shared/format.ts +129 -0
- package/src/shared/types.ts +19 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { InputDetail, InputKind, SegmentName } from './types.js';
|
|
2
|
+
|
|
3
|
+
export type FormatPart =
|
|
4
|
+
| { type: 'literal'; text: string }
|
|
5
|
+
| { type: 'segment'; segment: SegmentName; token: string };
|
|
6
|
+
|
|
7
|
+
const tokenPattern = /y+|M{1,4}|d{1,2}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|a|'(?:[^']|'')*'|[^yMdHhmsa']+/g;
|
|
8
|
+
|
|
9
|
+
const segmentByTokenLetter: Record<string, SegmentName> = {
|
|
10
|
+
H: 'hour24',
|
|
11
|
+
M: 'month',
|
|
12
|
+
a: 'amPm',
|
|
13
|
+
d: 'day',
|
|
14
|
+
h: 'hour12',
|
|
15
|
+
m: 'minute',
|
|
16
|
+
s: 'second',
|
|
17
|
+
y: 'year',
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Parses a format based on Unicode Technical Standard #35, e.g. `y-MM-dd h:mm:ss a`.
|
|
22
|
+
*/
|
|
23
|
+
export function parseFormat(format: string): FormatPart[] {
|
|
24
|
+
const parts: FormatPart[] = [];
|
|
25
|
+
|
|
26
|
+
for (const match of format.matchAll(tokenPattern)) {
|
|
27
|
+
const text = match[0];
|
|
28
|
+
const first = text.charAt(0);
|
|
29
|
+
const segment = segmentByTokenLetter[first];
|
|
30
|
+
|
|
31
|
+
if (first === "'") {
|
|
32
|
+
parts.push({ type: 'literal', text: text.slice(1, -1).replace(/''/g, "'") });
|
|
33
|
+
} else if (segment) {
|
|
34
|
+
parts.push({ type: 'segment', segment, token: text });
|
|
35
|
+
} else {
|
|
36
|
+
parts.push({ type: 'literal', text });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return parts;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Builds the format a given locale uses, limited to the fields needed by the input kind and detail.
|
|
45
|
+
*/
|
|
46
|
+
export function getDefaultFormatParts(
|
|
47
|
+
locale: string | undefined,
|
|
48
|
+
kind: InputKind,
|
|
49
|
+
detail: InputDetail,
|
|
50
|
+
): FormatPart[] {
|
|
51
|
+
const options: Intl.DateTimeFormatOptions = {};
|
|
52
|
+
|
|
53
|
+
if (kind !== 'time') {
|
|
54
|
+
options.year = 'numeric';
|
|
55
|
+
|
|
56
|
+
if (detail !== 'year') {
|
|
57
|
+
options.month = 'numeric';
|
|
58
|
+
options.day = 'numeric';
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (kind !== 'date') {
|
|
63
|
+
options.hour = 'numeric';
|
|
64
|
+
|
|
65
|
+
if (detail === 'minute' || detail === 'second') {
|
|
66
|
+
options.minute = 'numeric';
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (detail === 'second') {
|
|
70
|
+
options.second = 'numeric';
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const formatter = new Intl.DateTimeFormat(locale, options);
|
|
75
|
+
const uses12Hours = formatter.resolvedOptions().hour12 === true;
|
|
76
|
+
|
|
77
|
+
const parts: FormatPart[] = [];
|
|
78
|
+
|
|
79
|
+
for (const part of formatter.formatToParts(new Date(2017, 0, 1, 13, 2, 3))) {
|
|
80
|
+
switch (part.type) {
|
|
81
|
+
case 'year':
|
|
82
|
+
parts.push({ type: 'segment', segment: 'year', token: 'y' });
|
|
83
|
+
break;
|
|
84
|
+
case 'month':
|
|
85
|
+
parts.push({ type: 'segment', segment: 'month', token: 'M' });
|
|
86
|
+
break;
|
|
87
|
+
case 'day':
|
|
88
|
+
parts.push({ type: 'segment', segment: 'day', token: 'd' });
|
|
89
|
+
break;
|
|
90
|
+
case 'hour':
|
|
91
|
+
parts.push(
|
|
92
|
+
uses12Hours
|
|
93
|
+
? { type: 'segment', segment: 'hour12', token: 'h' }
|
|
94
|
+
: { type: 'segment', segment: 'hour24', token: 'H' },
|
|
95
|
+
);
|
|
96
|
+
break;
|
|
97
|
+
case 'minute':
|
|
98
|
+
parts.push({ type: 'segment', segment: 'minute', token: 'm' });
|
|
99
|
+
break;
|
|
100
|
+
case 'second':
|
|
101
|
+
parts.push({ type: 'segment', segment: 'second', token: 's' });
|
|
102
|
+
break;
|
|
103
|
+
case 'dayPeriod':
|
|
104
|
+
parts.push({ type: 'segment', segment: 'amPm', token: 'a' });
|
|
105
|
+
break;
|
|
106
|
+
case 'literal':
|
|
107
|
+
parts.push({ type: 'literal', text: part.value });
|
|
108
|
+
break;
|
|
109
|
+
default:
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return parts;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function getMonthNames(locale: string | undefined, style: 'long' | 'short'): string[] {
|
|
117
|
+
const formatter = new Intl.DateTimeFormat(locale, { month: style });
|
|
118
|
+
|
|
119
|
+
return Array.from({ length: 12 }, (_, index) => formatter.format(new Date(2017, index, 1)));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function getAmPmLabels(locale: string | undefined): [string, string] {
|
|
123
|
+
const formatter = new Intl.DateTimeFormat(locale, { hour: 'numeric', hour12: true });
|
|
124
|
+
const label = (hour: number) =>
|
|
125
|
+
formatter.formatToParts(new Date(2017, 0, 1, hour)).find((part) => part.type === 'dayPeriod')
|
|
126
|
+
?.value ?? (hour < 12 ? 'AM' : 'PM');
|
|
127
|
+
|
|
128
|
+
return [label(5), label(17)];
|
|
129
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type InputKind = 'date' | 'time' | 'datetime';
|
|
2
|
+
|
|
3
|
+
export type SegmentName =
|
|
4
|
+
| 'year'
|
|
5
|
+
| 'month'
|
|
6
|
+
| 'day'
|
|
7
|
+
| 'hour12'
|
|
8
|
+
| 'hour24'
|
|
9
|
+
| 'minute'
|
|
10
|
+
| 'second'
|
|
11
|
+
| 'amPm';
|
|
12
|
+
|
|
13
|
+
export type DateDetail = 'year' | 'month';
|
|
14
|
+
|
|
15
|
+
export type TimeDetail = 'hour' | 'minute' | 'second';
|
|
16
|
+
|
|
17
|
+
export type InputDetail = DateDetail | TimeDetail;
|
|
18
|
+
|
|
19
|
+
export type LooseValuePiece = string | Date | null;
|