svelte-date-input 0.0.0-stage → 0.1.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,99 @@
|
|
|
1
|
+
const tokenPattern = /y+|M{1,4}|d{1,2}|H{1,2}|h{1,2}|m{1,2}|s{1,2}|a|'(?:[^']|'')*'|[^yMdHhmsa']+/g;
|
|
2
|
+
const segmentByTokenLetter = {
|
|
3
|
+
H: 'hour24',
|
|
4
|
+
M: 'month',
|
|
5
|
+
a: 'amPm',
|
|
6
|
+
d: 'day',
|
|
7
|
+
h: 'hour12',
|
|
8
|
+
m: 'minute',
|
|
9
|
+
s: 'second',
|
|
10
|
+
y: 'year',
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Parses a format based on Unicode Technical Standard #35, e.g. `y-MM-dd h:mm:ss a`.
|
|
14
|
+
*/
|
|
15
|
+
export function parseFormat(format) {
|
|
16
|
+
const parts = [];
|
|
17
|
+
for (const match of format.matchAll(tokenPattern)) {
|
|
18
|
+
const text = match[0];
|
|
19
|
+
const first = text.charAt(0);
|
|
20
|
+
const segment = segmentByTokenLetter[first];
|
|
21
|
+
if (first === "'") {
|
|
22
|
+
parts.push({ type: 'literal', text: text.slice(1, -1).replace(/''/g, "'") });
|
|
23
|
+
}
|
|
24
|
+
else if (segment) {
|
|
25
|
+
parts.push({ type: 'segment', segment, token: text });
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
parts.push({ type: 'literal', text });
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return parts;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Builds the format a given locale uses, limited to the fields needed by the input kind and detail.
|
|
35
|
+
*/
|
|
36
|
+
export function getDefaultFormatParts(locale, kind, detail) {
|
|
37
|
+
const options = {};
|
|
38
|
+
if (kind !== 'time') {
|
|
39
|
+
options.year = 'numeric';
|
|
40
|
+
if (detail !== 'year') {
|
|
41
|
+
options.month = 'numeric';
|
|
42
|
+
options.day = 'numeric';
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (kind !== 'date') {
|
|
46
|
+
options.hour = 'numeric';
|
|
47
|
+
if (detail === 'minute' || detail === 'second') {
|
|
48
|
+
options.minute = 'numeric';
|
|
49
|
+
}
|
|
50
|
+
if (detail === 'second') {
|
|
51
|
+
options.second = 'numeric';
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const formatter = new Intl.DateTimeFormat(locale, options);
|
|
55
|
+
const uses12Hours = formatter.resolvedOptions().hour12 === true;
|
|
56
|
+
const parts = [];
|
|
57
|
+
for (const part of formatter.formatToParts(new Date(2017, 0, 1, 13, 2, 3))) {
|
|
58
|
+
switch (part.type) {
|
|
59
|
+
case 'year':
|
|
60
|
+
parts.push({ type: 'segment', segment: 'year', token: 'y' });
|
|
61
|
+
break;
|
|
62
|
+
case 'month':
|
|
63
|
+
parts.push({ type: 'segment', segment: 'month', token: 'M' });
|
|
64
|
+
break;
|
|
65
|
+
case 'day':
|
|
66
|
+
parts.push({ type: 'segment', segment: 'day', token: 'd' });
|
|
67
|
+
break;
|
|
68
|
+
case 'hour':
|
|
69
|
+
parts.push(uses12Hours
|
|
70
|
+
? { type: 'segment', segment: 'hour12', token: 'h' }
|
|
71
|
+
: { type: 'segment', segment: 'hour24', token: 'H' });
|
|
72
|
+
break;
|
|
73
|
+
case 'minute':
|
|
74
|
+
parts.push({ type: 'segment', segment: 'minute', token: 'm' });
|
|
75
|
+
break;
|
|
76
|
+
case 'second':
|
|
77
|
+
parts.push({ type: 'segment', segment: 'second', token: 's' });
|
|
78
|
+
break;
|
|
79
|
+
case 'dayPeriod':
|
|
80
|
+
parts.push({ type: 'segment', segment: 'amPm', token: 'a' });
|
|
81
|
+
break;
|
|
82
|
+
case 'literal':
|
|
83
|
+
parts.push({ type: 'literal', text: part.value });
|
|
84
|
+
break;
|
|
85
|
+
default:
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return parts;
|
|
89
|
+
}
|
|
90
|
+
export function getMonthNames(locale, style) {
|
|
91
|
+
const formatter = new Intl.DateTimeFormat(locale, { month: style });
|
|
92
|
+
return Array.from({ length: 12 }, (_, index) => formatter.format(new Date(2017, index, 1)));
|
|
93
|
+
}
|
|
94
|
+
export function getAmPmLabels(locale) {
|
|
95
|
+
const formatter = new Intl.DateTimeFormat(locale, { hour: 'numeric', hour12: true });
|
|
96
|
+
const label = (hour) => formatter.formatToParts(new Date(2017, 0, 1, hour)).find((part) => part.type === 'dayPeriod')
|
|
97
|
+
?.value ?? (hour < 12 ? 'AM' : 'PM');
|
|
98
|
+
return [label(5), label(17)];
|
|
99
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type InputKind = 'date' | 'time' | 'datetime';
|
|
2
|
+
export type SegmentName = 'year' | 'month' | 'day' | 'hour12' | 'hour24' | 'minute' | 'second' | 'amPm';
|
|
3
|
+
export type DateDetail = 'year' | 'month';
|
|
4
|
+
export type TimeDetail = 'hour' | 'minute' | 'second';
|
|
5
|
+
export type InputDetail = DateDetail | TimeDetail;
|
|
6
|
+
export type LooseValuePiece = string | Date | null;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,71 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-date-input",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Segmented date, time and datetime input fields for your Svelte app. Base package of svelte-time-input and svelte-datetime-input.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"date",
|
|
7
|
+
"date-input",
|
|
8
|
+
"input",
|
|
9
|
+
"svelte"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": {
|
|
13
|
+
"name": "Adhi Ravishankar",
|
|
14
|
+
"email": "adhiravishankar@gmail.com"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist/**/*",
|
|
18
|
+
"src/**/*",
|
|
19
|
+
"!**/*.spec.*"
|
|
20
|
+
],
|
|
21
|
+
"type": "module",
|
|
22
|
+
"sideEffects": [
|
|
23
|
+
"*.css",
|
|
24
|
+
"*.svelte"
|
|
25
|
+
],
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"svelte": "./dist/index.js",
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"svelte": "./dist/index.js",
|
|
32
|
+
"default": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./dist/*.css": "./dist/*.css"
|
|
35
|
+
},
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public",
|
|
38
|
+
"provenance": true
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "svelte-package --input src --output dist",
|
|
42
|
+
"clean": "node -e \"fs.rmSync('./dist', { recursive: true, force: true })\"",
|
|
43
|
+
"format": "vp fmt --check",
|
|
44
|
+
"lint": "vp lint",
|
|
45
|
+
"prepack": "npm run clean && npm run build",
|
|
46
|
+
"test": "npm run lint && npm run tsc && npm run format && npm run unit",
|
|
47
|
+
"tsc": "svelte-check --tsconfig ./tsconfig.json",
|
|
48
|
+
"unit": "vp test",
|
|
49
|
+
"watch": "svelte-package --input src --output dist --watch"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@wojtekmaj/date-utils": "^2.0.2",
|
|
53
|
+
"get-user-locale": "^3.0.0"
|
|
54
|
+
},
|
|
55
|
+
"devDependencies": {
|
|
56
|
+
"@sveltejs/package": "^3.0.1",
|
|
57
|
+
"@sveltejs/vite-plugin-svelte": "^7.3.1",
|
|
58
|
+
"@vitest/browser-playwright": "5.0.5",
|
|
59
|
+
"playwright": "^1.64.1",
|
|
60
|
+
"svelte": "^5.57.2",
|
|
61
|
+
"svelte-check": "^4.7.7",
|
|
62
|
+
"typescript": "^6.0.3",
|
|
63
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@1.4.0",
|
|
64
|
+
"vite-plus": "1.4.0",
|
|
65
|
+
"vitest": "5.0.5",
|
|
66
|
+
"vitest-browser-svelte": "^3.1.0"
|
|
67
|
+
},
|
|
68
|
+
"peerDependencies": {
|
|
69
|
+
"svelte": "^5.0.0"
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import SegmentedInput from './SegmentedInput.svelte';
|
|
3
|
+
|
|
4
|
+
import type { ComponentProps } from 'svelte';
|
|
5
|
+
import type { DateDetail, LooseValuePiece } from './shared/types.js';
|
|
6
|
+
|
|
7
|
+
type DateInputProps = Omit<
|
|
8
|
+
ComponentProps<typeof SegmentedInput>,
|
|
9
|
+
| 'hourAriaLabel'
|
|
10
|
+
| 'hourPlaceholder'
|
|
11
|
+
| 'kind'
|
|
12
|
+
| 'maxDetail'
|
|
13
|
+
| 'minuteAriaLabel'
|
|
14
|
+
| 'minutePlaceholder'
|
|
15
|
+
| 'onChange'
|
|
16
|
+
| 'secondAriaLabel'
|
|
17
|
+
| 'secondPlaceholder'
|
|
18
|
+
| 'amPmAriaLabel'
|
|
19
|
+
| 'value'
|
|
20
|
+
> & {
|
|
21
|
+
/**
|
|
22
|
+
* The most detailed field the user shall see.
|
|
23
|
+
*
|
|
24
|
+
* @default 'month'
|
|
25
|
+
*/
|
|
26
|
+
maxDetail?: DateDetail;
|
|
27
|
+
onChange?: (value: Date | null, shouldCloseWidgets: boolean) => void;
|
|
28
|
+
value?: LooseValuePiece;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
let { onChange, ...props }: DateInputProps = $props();
|
|
32
|
+
</script>
|
|
33
|
+
|
|
34
|
+
<SegmentedInput
|
|
35
|
+
{...props}
|
|
36
|
+
kind="date"
|
|
37
|
+
onChange={(value, shouldCloseWidgets) => onChange?.(value as Date | null, shouldCloseWidgets)}
|
|
38
|
+
/>
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
type DividerProps = {
|
|
3
|
+
class: string;
|
|
4
|
+
text: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
let { class: className, text }: DividerProps = $props();
|
|
8
|
+
|
|
9
|
+
// Keep whitespace from collapsing between input fields
|
|
10
|
+
const content = $derived(text.replace(/[ ]/g, ' '));
|
|
11
|
+
</script>
|
|
12
|
+
|
|
13
|
+
<span class={className}>{content}</span>
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
type SegmentInputProps = {
|
|
3
|
+
ariaLabel?: string;
|
|
4
|
+
/** Base class name; the input gets `${className}__input` and `${className}__${name}`. */
|
|
5
|
+
className: string;
|
|
6
|
+
disabled?: boolean;
|
|
7
|
+
/** Characters that move focus to the next field when typed. */
|
|
8
|
+
dividers?: string;
|
|
9
|
+
form?: string;
|
|
10
|
+
max: number;
|
|
11
|
+
maxLength: number;
|
|
12
|
+
min: number;
|
|
13
|
+
name: string;
|
|
14
|
+
onChange: (value: string) => void;
|
|
15
|
+
onNavigate: (element: HTMLElement, direction: -1 | 1) => void;
|
|
16
|
+
placeholder: string;
|
|
17
|
+
required?: boolean;
|
|
18
|
+
showLeadingZeros?: boolean;
|
|
19
|
+
value: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
let {
|
|
23
|
+
ariaLabel,
|
|
24
|
+
className,
|
|
25
|
+
disabled,
|
|
26
|
+
dividers = '',
|
|
27
|
+
form,
|
|
28
|
+
max,
|
|
29
|
+
maxLength,
|
|
30
|
+
min,
|
|
31
|
+
name,
|
|
32
|
+
onChange,
|
|
33
|
+
onNavigate,
|
|
34
|
+
placeholder,
|
|
35
|
+
required,
|
|
36
|
+
showLeadingZeros = false,
|
|
37
|
+
value,
|
|
38
|
+
}: SegmentInputProps = $props();
|
|
39
|
+
|
|
40
|
+
const hasLeadingZero = $derived(showLeadingZeros && value !== '' && Number(value) < 10);
|
|
41
|
+
|
|
42
|
+
function onInput(event: Event & { currentTarget: HTMLInputElement }) {
|
|
43
|
+
const element = event.currentTarget;
|
|
44
|
+
const raw = element.value.replace(/\D/g, '');
|
|
45
|
+
const normalized = raw === '' ? '' : String(Number(raw));
|
|
46
|
+
|
|
47
|
+
element.value = normalized;
|
|
48
|
+
onChange(normalized);
|
|
49
|
+
|
|
50
|
+
if (raw.length >= maxLength || (raw !== '' && Number(raw) * 10 > max)) {
|
|
51
|
+
onNavigate(element, 1);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function onKeyDown(event: KeyboardEvent & { currentTarget: HTMLInputElement }) {
|
|
56
|
+
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const element = event.currentTarget;
|
|
61
|
+
|
|
62
|
+
if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
|
|
63
|
+
event.preventDefault();
|
|
64
|
+
onNavigate(element, event.key === 'ArrowLeft' ? -1 : 1);
|
|
65
|
+
} else if (event.key === 'Backspace' && element.value === '') {
|
|
66
|
+
event.preventDefault();
|
|
67
|
+
onNavigate(element, -1);
|
|
68
|
+
} else if (event.key.length === 1 && dividers.includes(event.key)) {
|
|
69
|
+
event.preventDefault();
|
|
70
|
+
onNavigate(element, 1);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function onFocus(event: FocusEvent & { currentTarget: HTMLInputElement }) {
|
|
75
|
+
event.currentTarget.select();
|
|
76
|
+
}
|
|
77
|
+
</script>
|
|
78
|
+
|
|
79
|
+
{#if hasLeadingZero}
|
|
80
|
+
<span class="{className}__leadingZero">0</span>
|
|
81
|
+
{/if}
|
|
82
|
+
<input
|
|
83
|
+
aria-label={ariaLabel}
|
|
84
|
+
class={[
|
|
85
|
+
`${className}__input`,
|
|
86
|
+
`${className}__${name.replace(/12|24/, '')}`,
|
|
87
|
+
hasLeadingZero && `${className}__input--hasLeadingZero`,
|
|
88
|
+
]}
|
|
89
|
+
data-input="true"
|
|
90
|
+
data-segment={name}
|
|
91
|
+
{disabled}
|
|
92
|
+
{form}
|
|
93
|
+
inputmode="numeric"
|
|
94
|
+
{max}
|
|
95
|
+
{min}
|
|
96
|
+
{name}
|
|
97
|
+
oninput={onInput}
|
|
98
|
+
onfocus={onFocus}
|
|
99
|
+
onkeydown={onKeyDown}
|
|
100
|
+
{placeholder}
|
|
101
|
+
{required}
|
|
102
|
+
step="1"
|
|
103
|
+
style:width="{Math.max(1, (value || placeholder).length)}ch"
|
|
104
|
+
type="number"
|
|
105
|
+
{value}
|
|
106
|
+
/>
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
type SegmentSelectProps = {
|
|
3
|
+
ariaLabel?: string;
|
|
4
|
+
className: string;
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
form?: string;
|
|
7
|
+
name: string;
|
|
8
|
+
onChange: (value: string) => void;
|
|
9
|
+
onNavigate: (element: HTMLElement, direction: -1 | 1) => void;
|
|
10
|
+
options: { label: string; value: string }[];
|
|
11
|
+
placeholder: string;
|
|
12
|
+
required?: boolean;
|
|
13
|
+
value: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
let {
|
|
17
|
+
ariaLabel,
|
|
18
|
+
className,
|
|
19
|
+
disabled,
|
|
20
|
+
form,
|
|
21
|
+
name,
|
|
22
|
+
onChange,
|
|
23
|
+
onNavigate,
|
|
24
|
+
options,
|
|
25
|
+
placeholder,
|
|
26
|
+
required,
|
|
27
|
+
value,
|
|
28
|
+
}: SegmentSelectProps = $props();
|
|
29
|
+
|
|
30
|
+
function onKeyDown(event: KeyboardEvent & { currentTarget: HTMLSelectElement }) {
|
|
31
|
+
if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
if (event.key === 'ArrowLeft' || event.key === 'ArrowRight') {
|
|
36
|
+
event.preventDefault();
|
|
37
|
+
onNavigate(event.currentTarget, event.key === 'ArrowLeft' ? -1 : 1);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
</script>
|
|
41
|
+
|
|
42
|
+
<select
|
|
43
|
+
aria-label={ariaLabel}
|
|
44
|
+
class={[`${className}__input`, `${className}__${name}`]}
|
|
45
|
+
data-input="true"
|
|
46
|
+
data-segment={name}
|
|
47
|
+
data-select="true"
|
|
48
|
+
{disabled}
|
|
49
|
+
{form}
|
|
50
|
+
{name}
|
|
51
|
+
onchange={(event) => onChange(event.currentTarget.value)}
|
|
52
|
+
onkeydown={onKeyDown}
|
|
53
|
+
{required}
|
|
54
|
+
{value}
|
|
55
|
+
>
|
|
56
|
+
<option value="">{placeholder}</option>
|
|
57
|
+
{#each options as option (option.value)}
|
|
58
|
+
<option value={option.value}>{option.label}</option>
|
|
59
|
+
{/each}
|
|
60
|
+
</select>
|