|
10 | 10 | import { useId } from "$lib/internal/use-id.js";
|
11 | 11 | import type { DateRange } from "$lib/shared/index.js";
|
12 | 12 | import { getDefaultDate } from "$lib/internal/date-time/utils.js";
|
| 13 | + import { watch } from "runed"; |
13 | 14 |
|
14 | 15 | let {
|
15 | 16 | open = $bindable(false),
|
|
54 | 55 | let startValue = $state<DateValue | undefined>(value?.start);
|
55 | 56 | let endValue = $state<DateValue | undefined>(value?.end);
|
56 | 57 |
|
57 |
| - if (value === undefined) { |
| 58 | + function handleDefaultValue() { |
| 59 | + if (value !== undefined) return; |
58 | 60 | value = { start: undefined, end: undefined };
|
59 | 61 | }
|
| 62 | +
|
| 63 | + // SSR |
| 64 | + handleDefaultValue(); |
| 65 | +
|
| 66 | + /** |
| 67 | + * Covers an edge case where when a spread props object is reassigned, |
| 68 | + * the props are reset to their default values, which would make value |
| 69 | + * undefined which causes errors to be thrown. |
| 70 | + */ |
| 71 | + watch.pre( |
| 72 | + () => value, |
| 73 | + () => { |
| 74 | + handleDefaultValue(); |
| 75 | + } |
| 76 | + ); |
| 77 | +
|
60 | 78 | const defaultPlaceholder = getDefaultDate({
|
61 | 79 | granularity,
|
62 | 80 | defaultValue: value?.start,
|
63 | 81 | });
|
64 | 82 |
|
65 |
| - if (placeholder === undefined) { |
| 83 | + function handleDefaultPlaceholder() { |
| 84 | + if (placeholder !== undefined) return; |
66 | 85 | placeholder = defaultPlaceholder;
|
67 | 86 | }
|
68 | 87 |
|
| 88 | + // SSR |
| 89 | + handleDefaultPlaceholder(); |
| 90 | +
|
| 91 | + /** |
| 92 | + * Covers an edge case where when a spread props object is reassigned, |
| 93 | + * the props are reset to their default values, which would make placeholder |
| 94 | + * undefined which causes errors to be thrown. |
| 95 | + */ |
| 96 | + watch.pre( |
| 97 | + () => placeholder, |
| 98 | + () => { |
| 99 | + handleDefaultPlaceholder(); |
| 100 | + } |
| 101 | + ); |
| 102 | +
|
69 | 103 | function onRangeSelect() {
|
70 | 104 | if (closeOnRangeSelect) {
|
71 | 105 | open = false;
|
|
0 commit comments