You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte-kit-vice/src/uix/acel/calendar/components/calendar.svelte

127 lines
3.4 KiB

<script lang="ts">
import { watch } from "runed";
import { boxWith, mergeProps } from "../../utilities";
import type { DateValue } from "$lib/glob/lib/date";
import { CalendarRootState } from "../calendar.svelte";
import type { CalendarRootProps } from "../types";
import { useId } from "../../utilities/use-id";
import { noop } from "../../utilities/noop";
import { getDefaultDate } from "../../utilities/datetime/utils";
import { actx } from "$lib/actx/lib/instance_actx.svelte";
let {
child,
children,
id = useId(),
ref = $bindable(null),
value = $bindable(),
onValueChange = noop,
placeholder = $bindable(),
onPlaceholderChange = noop,
weekdayFormat = "narrow",
weekStartsOn = undefined,
pagedNavigation = false,
isDateDisabled = () => false,
isDateUnavailable = () => false,
fixedWeeks = false,
numberOfMonths = 1,
locale: localeProp = undefined,
calendarLabel = "",
disabled = false,
readonly = false,
minValue = undefined,
maxValue = undefined,
preventDeselect = false,
type,
disableDaysOutsideMonth = true,
initialFocus = false,
maxDays = undefined,
monthFormat = "long",
yearFormat = "numeric",
...restProps
}: CalendarRootProps = $props();
// Locale reactivo: usa el prop si se pasó, si no sigue actx.locale automáticamente
const locale = $derived(localeProp ?? actx.locale);
const defaultPlaceholder = getDefaultDate({ defaultValue: value, minValue, maxValue });
function handleDefaultPlaceholder() {
if (placeholder !== undefined) return;
placeholder = defaultPlaceholder;
}
// SSR
handleDefaultPlaceholder();
watch.pre(
() => placeholder,
() => { handleDefaultPlaceholder(); }
);
function handleDefaultValue() {
if (value !== undefined) return;
value = type === "single" ? undefined : [];
}
// SSR
handleDefaultValue();
watch.pre(
() => value,
() => { handleDefaultValue(); }
);
const rootState = CalendarRootState.create({
id: boxWith(() => id),
ref: boxWith(() => ref, (v) => (ref = v)),
weekdayFormat: boxWith(() => weekdayFormat),
weekStartsOn: boxWith(() => weekStartsOn),
pagedNavigation: boxWith(() => pagedNavigation),
isDateDisabled: boxWith(() => isDateDisabled),
isDateUnavailable: boxWith(() => isDateUnavailable),
fixedWeeks: boxWith(() => fixedWeeks),
numberOfMonths: boxWith(() => numberOfMonths),
locale: boxWith(() => locale),
calendarLabel: boxWith(() => calendarLabel),
readonly: boxWith(() => readonly),
disabled: boxWith(() => disabled),
minValue: boxWith(() => minValue),
maxValue: boxWith(() => maxValue),
disableDaysOutsideMonth: boxWith(() => disableDaysOutsideMonth),
initialFocus: boxWith(() => initialFocus),
maxDays: boxWith(() => maxDays),
placeholder: boxWith(
() => placeholder as DateValue,
(v) => {
placeholder = v;
onPlaceholderChange(v as DateValue);
}
),
preventDeselect: boxWith(() => preventDeselect),
value: boxWith(
() => value,
(v) => {
value = v;
// oxlint-disable-next-line no-explicit-any
onValueChange(v as any);
}
),
type: boxWith(() => type),
monthFormat: boxWith(() => monthFormat),
yearFormat: boxWith(() => yearFormat),
onDateSelect: boxWith(() => undefined),
defaultPlaceholder,
});
const mergedProps = $derived(mergeProps(restProps, rootState.props));
</script>
{#if child}
{@render child({ props: mergedProps, ...rootState.snippetProps })}
{:else}
<div {...mergedProps}>
{@render children?.(rootState.snippetProps)}
</div>
{/if}

Powered by TurnKey Linux.