compote-ui 0.52.2 → 0.52.4

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.
@@ -1,8 +1,15 @@
1
1
  <script lang="ts">
2
2
  import { DateInput } from '@ark-ui/svelte/date-input';
3
3
  import { Field } from '@ark-ui/svelte/field';
4
- import { useLocaleContext } from '@ark-ui/svelte/locale';
5
- import type { DateInputProps } from './types';
4
+ import {
5
+ fromDateToLocal,
6
+ getLocalTimeZone,
7
+ parseAbsolute,
8
+ parseDate,
9
+ parseDateTime,
10
+ parseZonedDateTime
11
+ } from '@internationalized/date';
12
+ import type { DateValue, DateInputProps, NativeDateInput } from './types';
6
13
 
7
14
  let {
8
15
  value = $bindable(),
@@ -13,16 +20,47 @@
13
20
  ...restProps
14
21
  }: DateInputProps = $props();
15
22
 
16
- const locale = useLocaleContext();
23
+ const absoluteDateTimeRe = /(?:Z|[+-]\d{2}:\d{2})$/;
24
+
25
+ function toDateValue(v: NativeDateInput): DateValue | null {
26
+ if (v == null) return null;
27
+ if (typeof v === 'string') {
28
+ if (!v) return null;
29
+ if (v.includes('T')) {
30
+ // ZonedDateTime.toString() format: "2024-01-15T10:30:00+02:00[Europe/Belgrade]"
31
+ if (v.includes('[')) return parseZonedDateTime(v);
32
+ if (absoluteDateTimeRe.test(v)) return parseAbsolute(v, 'UTC');
33
+ return parseDateTime(v);
34
+ }
35
+ return parseDate(v);
36
+ }
37
+ if (v instanceof Date) return fromDateToLocal(v);
38
+ return v;
39
+ }
40
+
41
+ function fromDateValue(dv: DateValue | null, source: NativeDateInput): NativeDateInput {
42
+ if (dv == null) return null;
43
+ if (typeof source === 'string') {
44
+ if (absoluteDateTimeRe.test(source)) return dv.toDate('UTC').toISOString();
45
+ return dv.toString();
46
+ }
47
+ if (source instanceof Date) return dv.toDate(getLocalTimeZone());
48
+ return dv;
49
+ }
50
+
51
+ const arkValue = $derived(toDateValue(value));
52
+ const arkDefault = $derived(toDateValue(defaultValue));
17
53
  </script>
18
54
 
19
55
  <DateInput.Root
20
56
  {...restProps}
21
- locale={locale().locale}
22
- value={value ? [value] : []}
23
- defaultValue={defaultValue ? [defaultValue] : undefined}
57
+ value={arkValue ? [arkValue] : []}
58
+ defaultValue={arkDefault ? [arkDefault] : undefined}
24
59
  onValueChange={(details) => {
25
- value = details.value[0] ?? null;
60
+ const dv = details.value[0] ?? null;
61
+ // Guard: skip if Ark UI echoes back the same date (prevents reactive loop)
62
+ if (dv?.toString() === arkValue?.toString()) return;
63
+ value = fromDateValue(dv, value);
26
64
  onValueChange?.(details);
27
65
  }}
28
66
  >
@@ -1,8 +1,10 @@
1
1
  import type { DateInputRootBaseProps } from '@ark-ui/svelte/date-input';
2
2
  import type { DateInputDateValue as DateValue } from '@ark-ui/svelte/date-input';
3
+ export type { DateValue };
4
+ export type NativeDateInput = DateValue | string | Date | null | undefined;
3
5
  export interface DateInputProps extends Omit<DateInputRootBaseProps, 'value' | 'defaultValue'> {
4
- value?: DateValue | null;
5
- defaultValue?: DateValue;
6
+ value?: NativeDateInput;
7
+ defaultValue?: NativeDateInput;
6
8
  label?: string;
7
9
  name?: string;
8
10
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "compote-ui",
3
- "version": "0.52.2",
3
+ "version": "0.52.4",
4
4
  "license": "MIT",
5
5
  "scripts": {
6
6
  "dev": "vite dev --open",