feat: add date/time picker and fix launcherVersion initialization error
This commit is contained in:
1 parent
54f7f81160
commit
4dc6cb1721
33 files changed
+1074
-13
No files matched your search
@@ -0,0 +1,198 @@
|
||||
<script lang="ts">
|
||||
import Calendar from '$lib/components/ui/calendar/calendar.svelte';
|
||||
import * as Popover from '$lib/components/ui/popover';
|
||||
import { cn } from '$lib/utils.js';
|
||||
import TimePicker from './time-picker.svelte';
|
||||
import { CalendarDate, type DateValue } from '@internationalized/date';
|
||||
import CalendarIcon from '@lucide/svelte/icons/calendar';
|
||||
import X from '@lucide/svelte/icons/x';
|
||||
|
||||
let {
|
||||
value = $bindable(''),
|
||||
withTime = false,
|
||||
includeSeconds = false,
|
||||
placeholder = withTime ? 'Pick date & time...' : 'Pick a date...',
|
||||
disabled = false,
|
||||
clearable = true,
|
||||
class: className = '',
|
||||
contentClass = '',
|
||||
open = $bindable(false),
|
||||
id = undefined,
|
||||
}: {
|
||||
value?: string;
|
||||
withTime?: boolean;
|
||||
includeSeconds?: boolean;
|
||||
placeholder?: string;
|
||||
disabled?: boolean;
|
||||
clearable?: boolean;
|
||||
class?: string;
|
||||
contentClass?: string;
|
||||
open?: boolean;
|
||||
id?: string;
|
||||
} = $props();
|
||||
|
||||
let calendarValue = $state<DateValue | undefined>(undefined);
|
||||
let hours = $state(0);
|
||||
let minutes = $state(0);
|
||||
let seconds = $state(0);
|
||||
let lastEmittedValue = '';
|
||||
|
||||
// Synchronize incoming value -> internal state
|
||||
$effect(() => {
|
||||
if (value === lastEmittedValue) return;
|
||||
lastEmittedValue = value ?? '';
|
||||
|
||||
if (!value || !value.trim()) {
|
||||
calendarValue = undefined;
|
||||
hours = 0;
|
||||
minutes = 0;
|
||||
seconds = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
const timestamp = Date.parse(value);
|
||||
if (!Number.isNaN(timestamp)) {
|
||||
const parsed = new Date(timestamp);
|
||||
calendarValue = new CalendarDate(
|
||||
parsed.getFullYear(),
|
||||
parsed.getMonth() + 1,
|
||||
parsed.getDate()
|
||||
);
|
||||
hours = parsed.getHours();
|
||||
minutes = parsed.getMinutes();
|
||||
seconds = parsed.getSeconds();
|
||||
} else {
|
||||
calendarValue = undefined;
|
||||
}
|
||||
});
|
||||
|
||||
function emitChange() {
|
||||
if (!calendarValue) {
|
||||
lastEmittedValue = '';
|
||||
value = '';
|
||||
return;
|
||||
}
|
||||
|
||||
let next = '';
|
||||
if (withTime) {
|
||||
const d = new Date(
|
||||
calendarValue.year,
|
||||
calendarValue.month - 1,
|
||||
calendarValue.day,
|
||||
hours,
|
||||
minutes,
|
||||
seconds
|
||||
);
|
||||
next = d.toISOString();
|
||||
} else {
|
||||
const pad = (n: number) => String(n).padStart(2, '0');
|
||||
next = `${calendarValue.year}-${pad(calendarValue.month)}-${pad(calendarValue.day)}`;
|
||||
}
|
||||
|
||||
lastEmittedValue = next;
|
||||
value = next;
|
||||
}
|
||||
|
||||
function handleDateChange(newDate: DateValue | undefined) {
|
||||
calendarValue = newDate;
|
||||
if (newDate) {
|
||||
emitChange();
|
||||
if (!withTime) {
|
||||
open = false;
|
||||
}
|
||||
} else {
|
||||
emitChange();
|
||||
}
|
||||
}
|
||||
|
||||
function handleTimeChange() {
|
||||
if (!calendarValue) {
|
||||
const now = new Date();
|
||||
calendarValue = new CalendarDate(now.getFullYear(), now.getMonth() + 1, now.getDate());
|
||||
}
|
||||
emitChange();
|
||||
}
|
||||
|
||||
function handleClear(e: MouseEvent) {
|
||||
e.stopPropagation();
|
||||
e.preventDefault();
|
||||
calendarValue = undefined;
|
||||
hours = 0;
|
||||
minutes = 0;
|
||||
seconds = 0;
|
||||
emitChange();
|
||||
}
|
||||
|
||||
const formattedDisplay = $derived.by(() => {
|
||||
if (!value || !value.trim()) return '';
|
||||
const ts = Date.parse(value);
|
||||
if (Number.isNaN(ts)) return value;
|
||||
const d = new Date(ts);
|
||||
try {
|
||||
if (withTime) {
|
||||
return new Intl.DateTimeFormat(undefined, {
|
||||
dateStyle: 'medium',
|
||||
timeStyle: includeSeconds ? 'medium' : 'short',
|
||||
}).format(d);
|
||||
}
|
||||
return new Intl.DateTimeFormat(undefined, { dateStyle: 'medium' }).format(d);
|
||||
} catch {
|
||||
return value;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<Popover.Root bind:open>
|
||||
<div class="relative flex items-center">
|
||||
<Popover.Trigger
|
||||
{id}
|
||||
{disabled}
|
||||
class={cn(
|
||||
'flex h-8 w-full items-center justify-start gap-2 rounded-md border border-input bg-background px-3 text-xs font-normal text-foreground transition-colors hover:bg-accent hover:text-accent-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50 ring-offset-background',
|
||||
!formattedDisplay && 'text-muted-foreground',
|
||||
className
|
||||
)}
|
||||
>
|
||||
<CalendarIcon class="size-3.5 shrink-0 text-muted-foreground" />
|
||||
<span class="truncate">
|
||||
{formattedDisplay || placeholder}
|
||||
</span>
|
||||
</Popover.Trigger>
|
||||
|
||||
{#if clearable && formattedDisplay && !disabled}
|
||||
<button
|
||||
type="button"
|
||||
class="absolute right-2 flex size-4 items-center justify-center rounded-sm text-muted-foreground transition-colors hover:text-foreground"
|
||||
onclick={handleClear}
|
||||
title="Clear date"
|
||||
>
|
||||
<X class="size-3" />
|
||||
<span class="sr-only">Clear</span>
|
||||
</button>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<Popover.Content
|
||||
align="start"
|
||||
class={cn('w-auto border-border bg-popover p-0 shadow-lg', contentClass)}
|
||||
>
|
||||
<Calendar
|
||||
type="single"
|
||||
value={calendarValue}
|
||||
onValueChange={(val) => handleDateChange(val as DateValue | undefined)}
|
||||
class="p-3"
|
||||
/>
|
||||
|
||||
{#if withTime}
|
||||
<div class="border-t border-border/60 p-3 pt-2">
|
||||
<TimePicker
|
||||
bind:hours
|
||||
bind:minutes
|
||||
bind:seconds
|
||||
{includeSeconds}
|
||||
onchange={handleTimeChange}
|
||||
/>
|
||||
</div>
|
||||
{/if}
|
||||
</Popover.Content>
|
||||
</Popover.Root>
|
||||
@@ -0,0 +1,11 @@
|
||||
import DatePicker from './date-picker.svelte';
|
||||
import TimePicker from './time-picker.svelte';
|
||||
|
||||
export {
|
||||
DatePicker,
|
||||
TimePicker,
|
||||
//
|
||||
DatePicker as Root,
|
||||
};
|
||||
|
||||
export default DatePicker;
|
||||
@@ -0,0 +1,154 @@
|
||||
<script lang="ts">
|
||||
import Button from '$lib/components/ui/button/button.svelte';
|
||||
import { cn } from '$lib/utils.js';
|
||||
import Clock from '@lucide/svelte/icons/clock';
|
||||
|
||||
let {
|
||||
hours = $bindable(0),
|
||||
minutes = $bindable(0),
|
||||
seconds = $bindable(0),
|
||||
includeSeconds = false,
|
||||
disabled = false,
|
||||
class: className = '',
|
||||
onchange,
|
||||
}: {
|
||||
hours?: number;
|
||||
minutes?: number;
|
||||
seconds?: number;
|
||||
includeSeconds?: boolean;
|
||||
disabled?: boolean;
|
||||
class?: string;
|
||||
onchange?: () => void;
|
||||
} = $props();
|
||||
|
||||
const pad = (n: number) => String(Math.max(0, Math.floor(n))).padStart(2, '0');
|
||||
|
||||
function updateHours(val: number) {
|
||||
if (disabled) return;
|
||||
const clamped = Math.max(0, Math.min(23, Number.isNaN(val) ? 0 : val));
|
||||
if (hours !== clamped) {
|
||||
hours = clamped;
|
||||
onchange?.();
|
||||
}
|
||||
}
|
||||
|
||||
function updateMinutes(val: number) {
|
||||
if (disabled) return;
|
||||
const clamped = Math.max(0, Math.min(59, Number.isNaN(val) ? 0 : val));
|
||||
if (minutes !== clamped) {
|
||||
minutes = clamped;
|
||||
onchange?.();
|
||||
}
|
||||
}
|
||||
|
||||
function updateSeconds(val: number) {
|
||||
if (disabled) return;
|
||||
const clamped = Math.max(0, Math.min(59, Number.isNaN(val) ? 0 : val));
|
||||
if (seconds !== clamped) {
|
||||
seconds = clamped;
|
||||
onchange?.();
|
||||
}
|
||||
}
|
||||
|
||||
function setNow() {
|
||||
if (disabled) return;
|
||||
const now = new Date();
|
||||
hours = now.getHours();
|
||||
minutes = now.getMinutes();
|
||||
seconds = now.getSeconds();
|
||||
onchange?.();
|
||||
}
|
||||
|
||||
function setMidnight() {
|
||||
if (disabled) return;
|
||||
hours = 0;
|
||||
minutes = 0;
|
||||
seconds = 0;
|
||||
onchange?.();
|
||||
}
|
||||
|
||||
function setNoon() {
|
||||
if (disabled) return;
|
||||
hours = 12;
|
||||
minutes = 0;
|
||||
seconds = 0;
|
||||
onchange?.();
|
||||
}
|
||||
</script>
|
||||
|
||||
<div class={cn('space-y-2', className)}>
|
||||
<div class="flex items-center justify-between gap-2">
|
||||
<div class="flex items-center gap-1.5 text-xs font-medium text-muted-foreground">
|
||||
<Clock class="size-3.5" />
|
||||
<span>Time (24h)</span>
|
||||
</div>
|
||||
<div class="flex items-center gap-1 font-mono text-xs">
|
||||
<label class="sr-only" for="tp-hours">Hours</label>
|
||||
<input
|
||||
id="tp-hours"
|
||||
type="number"
|
||||
min="0"
|
||||
max="23"
|
||||
{disabled}
|
||||
value={pad(hours)}
|
||||
oninput={(e) => updateHours(parseInt(e.currentTarget.value, 10))}
|
||||
class="h-7 w-11 rounded border border-input bg-background text-center text-xs text-foreground focus:outline-none focus:ring-1 focus:ring-ring disabled:opacity-50 [appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
|
||||
/>
|
||||
<span class="select-none font-bold text-muted-foreground">:</span>
|
||||
<label class="sr-only" for="tp-minutes">Minutes</label>
|
||||
<input
|
||||
id="tp-minutes"
|
||||
type="number"
|
||||
min="0"
|
||||
max="59"
|
||||
{disabled}
|
||||
value={pad(minutes)}
|
||||
oninput={(e) => updateMinutes(parseInt(e.currentTarget.value, 10))}
|
||||
class="h-7 w-11 rounded border border-input bg-background text-center text-xs text-foreground focus:outline-none focus:ring-1 focus:ring-ring disabled:opacity-50 [appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
|
||||
/>
|
||||
{#if includeSeconds}
|
||||
<span class="select-none font-bold text-muted-foreground">:</span>
|
||||
<label class="sr-only" for="tp-seconds">Seconds</label>
|
||||
<input
|
||||
id="tp-seconds"
|
||||
type="number"
|
||||
min="0"
|
||||
max="59"
|
||||
{disabled}
|
||||
value={pad(seconds)}
|
||||
oninput={(e) => updateSeconds(parseInt(e.currentTarget.value, 10))}
|
||||
class="h-7 w-11 rounded border border-input bg-background text-center text-xs text-foreground focus:outline-none focus:ring-1 focus:ring-ring disabled:opacity-50 [appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
|
||||
/>
|
||||
{/if}
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex items-center gap-1.5 pt-0.5">
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
{disabled}
|
||||
class="h-6 px-2 text-[10px] text-muted-foreground hover:text-foreground"
|
||||
onclick={setNow}
|
||||
>
|
||||
Now
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
{disabled}
|
||||
class="h-6 px-2 text-[10px] text-muted-foreground hover:text-foreground"
|
||||
onclick={setMidnight}
|
||||
>
|
||||
00:00
|
||||
</Button>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
{disabled}
|
||||
class="h-6 px-2 text-[10px] text-muted-foreground hover:text-foreground"
|
||||
onclick={setNoon}
|
||||
>
|
||||
12:00
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
Reference in new issue
Block a user