flotilla/src/lib/components/DateTimeInput.svelte

79 lines
2.1 KiB
Svelte
Raw Normal View History

2024-09-12 23:56:33 +00:00
<script lang="ts">
2026-07-03 21:07:53 +00:00
import {onMount} from "svelte"
2026-07-03 21:43:00 +00:00
import {DatePicker} from "@svelte-plugins/datepicker"
2025-09-10 22:26:39 +00:00
import CloseCircle from "@assets/icons/close-circle.svg?dataurl"
import CalendarMinimalistic from "@assets/icons/calendar-minimalistic.svg?dataurl"
2026-07-03 21:43:00 +00:00
import {anchorDatepicker} from "@lib/html"
2024-09-23 20:58:01 +00:00
import Icon from "@lib/components/Icon.svelte"
import Button from "@lib/components/Button.svelte"
2024-09-12 23:56:33 +00:00
2025-02-04 00:37:14 +00:00
interface Props {
2025-02-05 23:05:58 +00:00
value?: number | undefined
2025-02-04 00:37:14 +00:00
}
2025-02-05 18:47:56 +00:00
let {value = $bindable()}: Props = $props()
2024-09-12 23:56:33 +00:00
2026-07-03 21:43:00 +00:00
// The picker works in millisecond timestamps that include the time of day
// selected via its time input; we expose unix seconds
let element: HTMLElement
let isOpen = $state(false)
let startDate: number | null = $state(value ? value * 1000 : null)
let startDateTime = $state("")
2025-01-29 01:43:26 +00:00
2026-07-03 21:43:00 +00:00
// When no date is provided the library initializes its time input from the
// epoch (a locale-dependent afternoon); reset it to something friendlier
onMount(() => {
if (!startDate) startDateTime = "12:00"
})
2025-01-29 01:43:26 +00:00
2026-07-03 21:43:00 +00:00
const toggle = () => {
isOpen = !isOpen
2024-09-12 23:56:33 +00:00
}
const clear = () => {
2026-07-03 21:43:00 +00:00
startDate = null
isOpen = false
2025-01-29 01:43:26 +00:00
}
2026-07-03 21:43:00 +00:00
const fmt = (ts: number) =>
new Date(ts).toLocaleString([], {dateStyle: "medium", timeStyle: "short"})
2025-01-29 01:43:26 +00:00
2026-07-03 21:43:00 +00:00
const display = $derived(startDate ? fmt(startDate) : "")
2026-07-03 21:07:53 +00:00
2025-02-05 18:47:56 +00:00
$effect(() => {
2026-07-03 21:43:00 +00:00
if (isOpen && element) {
return anchorDatepicker(element)
2025-01-29 01:43:26 +00:00
}
2025-02-05 18:47:56 +00:00
})
2025-02-05 23:05:58 +00:00
$effect(() => {
2026-07-03 21:43:00 +00:00
value = startDate ? Math.round(startDate / 1000) : undefined
2025-02-05 23:05:58 +00:00
})
2024-09-12 23:56:33 +00:00
</script>
2026-07-03 21:43:00 +00:00
<div class="relative focus-within:z-modal" bind:this={element}>
<DatePicker
bind:isOpen
bind:startDate
bind:startDateTime
showTimePicker
enableFutureDates
includeFont={false}>
<label class="input input-group cursor-pointer">
<Icon icon={CalendarMinimalistic} />
<input
type="text"
readonly
class="grow cursor-pointer"
placeholder="Select date"
value={display}
onclick={toggle} />
{#if startDate}
<Button onclick={clear} class="h-5">
2025-09-10 22:26:39 +00:00
<Icon icon={CloseCircle} />
2025-01-29 01:43:26 +00:00
</Button>
{/if}
2026-07-03 21:43:00 +00:00
</label>
</DatePicker>
2025-01-29 01:43:26 +00:00
</div>