flotilla/src/lib/components/DateTimeInput.svelte

96 lines
2.7 KiB
Svelte
Raw Normal View History

2024-09-12 23:56:33 +00:00
<script lang="ts">
import {DateInput} from "date-picker-svelte"
2025-05-05 17:11:02 +00:00
import {secondsToDate, dateToSeconds} from "@welshman/lib"
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"
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
2025-01-29 01:43:26 +00:00
const pad = (n: number) => ("00" + String(n)).slice(-2)
2025-03-18 22:36:52 +00:00
const getTime = (d: Date, m: string) => `${pad(d.getHours())}:${m}`
2025-01-29 01:43:26 +00:00
2025-02-05 23:05:58 +00:00
const setTime = (d: Date, time: string) => {
2025-01-29 01:43:26 +00:00
const [hours, minutes] = time.split(":").map(x => parseInt(x))
2025-02-05 23:05:58 +00:00
const newDate = new Date(d)
2025-01-29 01:43:26 +00:00
2025-02-05 23:05:58 +00:00
newDate.setHours(hours, minutes, 0, 0)
2025-01-29 01:43:26 +00:00
2025-02-05 23:05:58 +00:00
return newDate
2025-01-29 01:43:26 +00:00
}
2025-02-05 23:05:58 +00:00
const onTimeChange = () => {
if (time) {
2025-03-03 21:04:12 +00:00
minutes = time.slice(-2)
2025-02-05 23:05:58 +00:00
date = setTime(date || new Date(), time)
2024-09-12 23:56:33 +00:00
}
}
2025-02-05 23:05:58 +00:00
const focusDate = () => element.querySelector("input")?.focus()
2024-09-12 23:56:33 +00:00
const clear = () => {
2025-02-05 23:05:58 +00:00
date = undefined
time = undefined
2025-01-29 01:43:26 +00:00
}
2025-03-18 22:36:52 +00:00
const initialDate = value ? secondsToDate(value) : undefined
const initialTime = initialDate ? getTime(initialDate, pad(initialDate.getMinutes())) : undefined
const initialMinutes = initialTime ? initialTime.slice(-2) : "00"
let date: Date | undefined = $state(initialDate)
let time: string | undefined = $state(initialTime)
let minutes: string = $state(initialMinutes)
2025-01-29 01:43:26 +00:00
let element: HTMLElement
2025-02-05 23:05:58 +00:00
// Sync date to time and value
2025-02-05 18:47:56 +00:00
$effect(() => {
2025-02-05 23:05:58 +00:00
if (date) {
2025-03-18 22:36:52 +00:00
time = getTime(date, minutes)
2025-02-05 23:05:58 +00:00
value = dateToSeconds(date)
} else {
value = undefined
2025-01-29 01:43:26 +00:00
}
2025-02-05 18:47:56 +00:00
})
2025-02-05 23:05:58 +00:00
// Sync updates to value to date/time
$effect(() => {
const derivedDate = value ? secondsToDate(value) : undefined
2025-03-18 22:36:52 +00:00
const derivedTime = derivedDate ? getTime(derivedDate, minutes) : undefined
2025-02-05 23:05:58 +00:00
date = derivedDate
time = derivedTime
})
2024-09-12 23:56:33 +00:00
</script>
2025-01-29 01:43:26 +00:00
<div class="relative grid grid-cols-2 gap-2" bind:this={element}>
<div class="relative">
2025-02-05 23:05:58 +00:00
<DateInput format="yyyy-MM-dd" placeholder="" bind:value={date} />
2025-01-29 01:43:26 +00:00
<div class="absolute right-2 top-0 flex h-12 cursor-pointer items-center gap-2">
2025-02-05 23:05:58 +00:00
{#if date}
2025-02-05 18:47:56 +00:00
<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>
{:else}
2025-02-05 18:47:56 +00:00
<Button onclick={focusDate} class="h-5">
2025-09-10 22:26:39 +00:00
<Icon icon={CalendarMinimalistic} />
2025-01-29 01:43:26 +00:00
</Button>
{/if}
</div>
2024-09-12 23:56:33 +00:00
</div>
2025-01-29 01:43:26 +00:00
<label class="input input-bordered flex items-center">
2025-02-05 23:05:58 +00:00
<input
list="time-options"
class="grow"
type="time"
step="300"
bind:value={time}
onchange={onTimeChange} />
2025-01-29 01:43:26 +00:00
</label>
</div>