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

View File

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