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 ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018–2026 Wojciech Maj
4
+ Copyright (c) 2026 Adhi Ravishankar
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,43 @@
1
- # Temporary Holding Version
1
+ # svelte-date-input
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Segmented date input fields for your Svelte 5 app, plus the engine shared with [svelte-time-input](../svelte-time-input) and [svelte-datetime-input](../svelte-datetime-input). Svelte port of the input part of [React-Date-Picker](https://github.com/wojtekmaj/react-date-picker).
4
+
5
+ The component renders a native `<input type="date">` (hidden, used for form submission and validation) and one field per format token. Fields move focus automatically while typing and on arrow keys, and the value is only reported once all fields form a valid date.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ npm install svelte-date-input
11
+ ```
12
+
13
+ Requires Svelte 5 or later.
14
+
15
+ ## Usage
16
+
17
+ ```svelte
18
+ <script lang="ts">
19
+ import DateInput from 'svelte-date-input';
20
+
21
+ let value = $state<Date | null>(new Date());
22
+ </script>
23
+
24
+ <DateInput name="date" onChange={(nextValue) => (value = nextValue)} {value} />
25
+ ```
26
+
27
+ ## Props
28
+
29
+ `className`, `customInputsForm`, `disabled`, `format`, `locale`, `maxDate`, `minDate`, `name`, `nativeInputAriaLabel`, `onChange(value, shouldCloseWidgets)`, `onInvalidChange`, `required`, `showLeadingZeros`, `value`, `autoFocus`, `dayAriaLabel`, `monthAriaLabel`, `yearAriaLabel`, `dayPlaceholder`, `monthPlaceholder`, `yearPlaceholder`, and `maxDetail` (`"month"` by default, or `"year"`). They behave like the props of [svelte-datetimerange-picker](../svelte-datetimerange-picker).
30
+
31
+ Fields get class names `${className}__input`, `${className}__day` (and `__month`, `__year`, …), `${className}__divider` and `${className}__leadingZero`.
32
+
33
+ ## Building blocks
34
+
35
+ `SegmentedInput` (with a `kind` of `"date"`, `"time"` or `"datetime"`), `SegmentInput`, `SegmentSelect`, `Divider`, and the `parseFormat`, `getDefaultFormatParts`, `getMonthNames` and `getAmPmLabels` helpers are exported for building other inputs.
36
+
37
+ ## Part of a monorepo
38
+
39
+ `svelte-date-input` is part of the [svelte-datetime-pickers monorepo](https://github.com/adhiravishankar/svelte-datetime-pickers). See the [live demo and Storybook](https://adhiravishankar.github.io/svelte-datetime-pickers/).
40
+
41
+ ## License
42
+
43
+ The MIT License.
@@ -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,16 @@
1
+ import SegmentedInput from './SegmentedInput.svelte';
2
+ import type { ComponentProps } from 'svelte';
3
+ import type { DateDetail, LooseValuePiece } from './shared/types.js';
4
+ type DateInputProps = Omit<ComponentProps<typeof SegmentedInput>, 'hourAriaLabel' | 'hourPlaceholder' | 'kind' | 'maxDetail' | 'minuteAriaLabel' | 'minutePlaceholder' | 'onChange' | 'secondAriaLabel' | 'secondPlaceholder' | 'amPmAriaLabel' | 'value'> & {
5
+ /**
6
+ * The most detailed field the user shall see.
7
+ *
8
+ * @default 'month'
9
+ */
10
+ maxDetail?: DateDetail;
11
+ onChange?: (value: Date | null, shouldCloseWidgets: boolean) => void;
12
+ value?: LooseValuePiece;
13
+ };
14
+ declare const DateInput: import("svelte").Component<DateInputProps, {}, "">;
15
+ type DateInput = ReturnType<typeof DateInput>;
16
+ export default DateInput;
@@ -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,7 @@
1
+ type DividerProps = {
2
+ class: string;
3
+ text: string;
4
+ };
5
+ declare const Divider: import("svelte").Component<DividerProps, {}, "">;
6
+ type Divider = ReturnType<typeof Divider>;
7
+ export default Divider;
@@ -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,22 @@
1
+ type SegmentInputProps = {
2
+ ariaLabel?: string;
3
+ /** Base class name; the input gets `${className}__input` and `${className}__${name}`. */
4
+ className: string;
5
+ disabled?: boolean;
6
+ /** Characters that move focus to the next field when typed. */
7
+ dividers?: string;
8
+ form?: string;
9
+ max: number;
10
+ maxLength: number;
11
+ min: number;
12
+ name: string;
13
+ onChange: (value: string) => void;
14
+ onNavigate: (element: HTMLElement, direction: -1 | 1) => void;
15
+ placeholder: string;
16
+ required?: boolean;
17
+ showLeadingZeros?: boolean;
18
+ value: string;
19
+ };
20
+ declare const SegmentInput: import("svelte").Component<SegmentInputProps, {}, "">;
21
+ type SegmentInput = ReturnType<typeof SegmentInput>;
22
+ export default SegmentInput;
@@ -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>
@@ -0,0 +1,19 @@
1
+ type SegmentSelectProps = {
2
+ ariaLabel?: string;
3
+ className: string;
4
+ disabled?: boolean;
5
+ form?: string;
6
+ name: string;
7
+ onChange: (value: string) => void;
8
+ onNavigate: (element: HTMLElement, direction: -1 | 1) => void;
9
+ options: {
10
+ label: string;
11
+ value: string;
12
+ }[];
13
+ placeholder: string;
14
+ required?: boolean;
15
+ value: string;
16
+ };
17
+ declare const SegmentSelect: import("svelte").Component<SegmentSelectProps, {}, "">;
18
+ type SegmentSelect = ReturnType<typeof SegmentSelect>;
19
+ export default SegmentSelect;