pin input: make name optional, custom class support, fix hidden input

This commit is contained in:
Elijah Duffy
2025-07-03 11:54:53 -07:00
parent ef7bc6bbab
commit 37c3594d1a
2 changed files with 57 additions and 45 deletions

View File

@@ -1,6 +1,19 @@
<script lang="ts"> <script lang="ts">
import { isValueValid, validate, type ValidatorOptions } from '@svelte-toolkit/validate'; import { isValueValid, validate, type ValidatorOptions } from '@svelte-toolkit/validate';
import Label from './Label.svelte'; import Label from './Label.svelte';
import type { ClassValue } from 'svelte/elements';
import { generateIdentifier } from './util.js';
interface Props {
label?: string;
length: number;
required?: boolean;
name?: string;
value?: string;
oncomplete?: (value: string) => void;
onchange?: (value: string) => void;
class?: ClassValue | null | undefined;
}
let { let {
label, label,
@@ -9,16 +22,11 @@
name, name,
value = $bindable(''), value = $bindable(''),
oncomplete, oncomplete,
onchange onchange,
}: { class: classValue
label?: string; }: Props = $props();
length: number;
required?: boolean; const id = $derived(generateIdentifier('pin-input', name));
name: string;
value?: string;
oncomplete?: (value: string) => void;
onchange?: (value: string) => void;
} = $props();
let hiddenInput: HTMLInputElement; let hiddenInput: HTMLInputElement;
let valid: boolean = $state(true); let valid: boolean = $state(true);
@@ -148,20 +156,23 @@
}); });
</script> </script>
<input <div class={classValue}>
<input
{id}
{name}
class="hidden" class="hidden"
use:validate={validateOpts} use:validate={validateOpts}
onvalidate={(e) => { onvalidate={(e) => {
valid = e.detail.valid; valid = e.detail.valid;
}} }}
bind:this={hiddenInput} bind:this={hiddenInput}
/> />
{#if label} {#if label}
<Label bigError={!valid} for={name}>{label}</Label> <Label bigError={!valid} for={id}>{label}</Label>
{/if} {/if}
<div> <div>
<div class="flex gap-4"> <div class="flex gap-4">
{#each { length: length } as _, i} {#each { length: length } as _, i}
<input <input
@@ -185,4 +196,5 @@
/> />
{/each} {/each}
</div> </div>
</div>
</div> </div>

View File

@@ -98,7 +98,7 @@
<div class="component"> <div class="component">
<p class="title">Pin Input</p> <p class="title">Pin Input</p>
<PinInput name="pin" length={6} /> <PinInput label="Please enter your 6-digit PIN" length={6} />
</div> </div>
<div class="component"> <div class="component">