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.
138 lines
3.2 KiB
138 lines
3.2 KiB
<script lang="ts">
|
|
import { watch } from "runed";
|
|
import { readableActive, writableActive, createId, mergeProps } from "../../utils/index";
|
|
const noop = () => {};
|
|
import { getDefaultTime, type TimeRange, type TimeValue } from "$terra/adapters/dates";
|
|
import { resolveHourCycleProp, resolveLocaleProp } from "../../base/config/prop-resolvers.ts";
|
|
import { TimeRangeFieldRootState } from "../time-range-field.svelte.ts";
|
|
import type { TimeRangeFieldRootProps } from "../types.js";
|
|
|
|
const uid = $props.id();
|
|
|
|
let {
|
|
id = createId(uid),
|
|
ref = $bindable(null),
|
|
value = $bindable(),
|
|
onValueChange = noop,
|
|
placeholder = $bindable(),
|
|
onPlaceholderChange = noop,
|
|
disabled = false,
|
|
readonly = false,
|
|
required = false,
|
|
hourCycle,
|
|
timeFormat,
|
|
granularity,
|
|
locale,
|
|
hideTimeZone = false,
|
|
validate = noop,
|
|
onInvalid = noop,
|
|
maxValue,
|
|
minValue,
|
|
readonlySegments = [],
|
|
children,
|
|
child,
|
|
onStartValueChange = noop,
|
|
onEndValueChange = noop,
|
|
errorMessageId,
|
|
...restProps
|
|
}: TimeRangeFieldRootProps<TimeValue> = $props();
|
|
|
|
let startValue = $state<TimeValue | undefined>(value?.start);
|
|
let endValue = $state<TimeValue | undefined>(value?.end);
|
|
|
|
function handleDefaultPlaceholder() {
|
|
if (placeholder !== undefined) return;
|
|
const defaultPlaceholder = getDefaultTime({
|
|
granularity,
|
|
defaultValue: value?.start,
|
|
});
|
|
placeholder = defaultPlaceholder;
|
|
}
|
|
|
|
handleDefaultPlaceholder();
|
|
|
|
watch.pre(
|
|
() => placeholder,
|
|
() => {
|
|
handleDefaultPlaceholder();
|
|
}
|
|
);
|
|
|
|
function handleDefaultValue() {
|
|
if (value !== undefined) return;
|
|
value = { start: undefined, end: undefined };
|
|
}
|
|
|
|
handleDefaultValue();
|
|
|
|
watch.pre(
|
|
() => value,
|
|
() => {
|
|
handleDefaultValue();
|
|
}
|
|
);
|
|
|
|
const rootState = TimeRangeFieldRootState.create({
|
|
id: readableActive(() => id),
|
|
ref: writableActive(
|
|
() => ref,
|
|
(v) => (ref = v)
|
|
),
|
|
disabled: readableActive(() => disabled),
|
|
readonly: readableActive(() => readonly),
|
|
required: readableActive(() => required),
|
|
hourCycle: resolveHourCycleProp(() => timeFormat, () => hourCycle),
|
|
granularity: readableActive(() => granularity),
|
|
locale: resolveLocaleProp(() => locale),
|
|
hideTimeZone: readableActive(() => hideTimeZone),
|
|
validate: readableActive(() => validate),
|
|
maxValue: readableActive(() => maxValue),
|
|
minValue: readableActive(() => minValue),
|
|
placeholder: writableActive(
|
|
() => placeholder as TimeValue,
|
|
(v) => {
|
|
placeholder = v;
|
|
onPlaceholderChange(v);
|
|
}
|
|
),
|
|
readonlySegments: readableActive(() => readonlySegments),
|
|
value: writableActive(
|
|
() => value as TimeRange<TimeValue>,
|
|
(v) => {
|
|
value = v;
|
|
onValueChange(v);
|
|
}
|
|
),
|
|
startValue: writableActive(
|
|
() => startValue,
|
|
(v) => {
|
|
startValue = v;
|
|
onStartValueChange(v);
|
|
}
|
|
),
|
|
endValue: writableActive(
|
|
() => endValue,
|
|
(v) => {
|
|
endValue = v;
|
|
onEndValueChange(v);
|
|
}
|
|
),
|
|
onInvalid: readableActive(() => onInvalid),
|
|
errorMessageId: readableActive(() => errorMessageId),
|
|
});
|
|
|
|
const mergedProps = $derived(mergeProps(restProps, rootState.props));
|
|
</script>
|
|
|
|
{#if child}
|
|
{@render child({ props: mergedProps })}
|
|
{:else}
|
|
<div {...mergedProps}>
|
|
{@render children?.()}
|
|
</div>
|
|
{/if}
|
|
|
|
|
|
|
|
|