Files
pocket-id-pocket-id/frontend/src/lib/components/form-input.svelte

50 lines
1.3 KiB
Svelte
Raw Normal View History

2024-08-12 11:00:25 +02:00
<script lang="ts">
import { Label } from '$lib/components/ui/label';
import type { FormInput } from '$lib/utils/form-util';
import type { Snippet } from 'svelte';
import type { HTMLAttributes } from 'svelte/elements';
2024-10-02 08:43:44 +02:00
import { Input, type FormInputEvent } from './ui/input';
2024-08-12 11:00:25 +02:00
let {
input = $bindable(),
label,
description,
placeholder,
disabled = false,
type = 'text',
children,
2024-10-02 08:43:44 +02:00
onInput,
...restProps
}: HTMLAttributes<HTMLDivElement> & {
input?: FormInput<string | boolean | number>;
2024-10-28 18:11:54 +01:00
label?: string;
description?: string;
placeholder?: string;
disabled?: boolean;
type?: 'text' | 'password' | 'email' | 'number' | 'checkbox';
2024-10-02 08:43:44 +02:00
onInput?: (e: FormInputEvent) => void;
2024-08-12 11:00:25 +02:00
children?: Snippet;
} = $props();
2024-10-28 18:11:54 +01:00
const id = label?.toLowerCase().replace(/ /g, '-');
2024-08-12 11:00:25 +02:00
</script>
<div {...restProps}>
2024-10-28 18:11:54 +01:00
{#if label}
<Label class="mb-0" for={id}>{label}</Label>
{/if}
{#if description}
<p class="text-muted-foreground mt-1 text-xs">{description}</p>
2024-08-12 11:00:25 +02:00
{/if}
2024-10-28 18:11:54 +01:00
<div class={label || description ? 'mt-2' : ''}>
{#if children}
{@render children()}
{:else if input}
<Input {id} {placeholder} {type} bind:value={input.value} {disabled} on:input={(e) => onInput?.(e)} />
{/if}
{#if input?.error}
<p class="mt-1 text-sm text-red-500">{input.error}</p>
{/if}
</div>
2024-08-12 11:00:25 +02:00
</div>