time input: custom class support, name optional

This commit is contained in:
Elijah Duffy
2025-07-03 14:21:08 -07:00
parent 723fc4ee48
commit c54002f5fa

View File

@@ -4,6 +4,19 @@
import StyledRawInput from './StyledRawInput.svelte'; import StyledRawInput from './StyledRawInput.svelte';
import { onMount } from 'svelte'; import { onMount } from 'svelte';
import moment from 'moment'; import moment from 'moment';
import type { ClassValue } from 'svelte/elements';
import { generateIdentifier } from './util.js';
interface Props {
name?: string;
label?: string;
value?: string;
formattedValue?: string;
required?: boolean;
invalidMessage?: string;
showConfirm?: boolean;
class?: ClassValue | null | undefined;
}
let { let {
name, name,
@@ -12,17 +25,11 @@
formattedValue = $bindable(''), formattedValue = $bindable(''),
required, required,
invalidMessage = 'Please select a time', invalidMessage = 'Please select a time',
showConfirm = false showConfirm = false,
}: { class: classValue
name: string; }: Props = $props();
label?: string;
value?: string;
formattedValue?: string;
required?: boolean;
invalidMessage?: string;
showConfirm?: boolean;
} = $props();
const id = $derived(generateIdentifier('time-input', name));
let ampm: 'AM' | 'PM' = $state('AM'); let ampm: 'AM' | 'PM' = $state('AM');
let type: HTMLInputElement['type'] = $state('number'); let type: HTMLInputElement['type'] = $state('number');
let valid: boolean = $state(true); let valid: boolean = $state(true);
@@ -190,14 +197,15 @@
}); });
</script> </script>
<div> <div class={classValue}>
{#if label} {#if label}
<Label for={name}>{label}</Label> <Label for={id}>{label}</Label>
{/if} {/if}
<!-- Hidden input stores the selected time in 24-hour format --> <!-- Hidden input stores the selected time in 24-hour format -->
<input <input
type="hidden" type="hidden"
{id}
{name} {name}
use:validate={{ required, autovalOnInvalid: true }} use:validate={{ required, autovalOnInvalid: true }}
onvalidate={(e) => (valid = e.detail.valid)} onvalidate={(e) => (valid = e.detail.valid)}
@@ -344,7 +352,7 @@
</div> </div>
<div class={['opacity-0 transition-opacity', (!valid || showConfirm) && 'opacity-100']}> <div class={['opacity-0 transition-opacity', (!valid || showConfirm) && 'opacity-100']}>
<Label for={name} error={!valid}> <Label for={id} error={!valid}>
{#if !valid} {#if !valid}
{invalidMessage} {invalidMessage}
{:else if showConfirm} {:else if showConfirm}