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';
|
2024-08-24 00:49:08 +02:00
|
|
|
import type { HTMLAttributes } from 'svelte/elements';
|
2024-08-12 11:00:25 +02:00
|
|
|
import { Input } from './ui/input';
|
|
|
|
|
|
|
|
|
|
let {
|
|
|
|
|
input = $bindable(),
|
|
|
|
|
label,
|
2024-08-13 20:51:10 +02:00
|
|
|
description,
|
2024-09-09 10:29:41 +02:00
|
|
|
disabled = false,
|
|
|
|
|
type = 'text',
|
2024-08-24 00:49:08 +02:00
|
|
|
children,
|
|
|
|
|
...restProps
|
|
|
|
|
}: HTMLAttributes<HTMLDivElement> & {
|
|
|
|
|
input?: FormInput<string | boolean | number>;
|
2024-08-12 11:00:25 +02:00
|
|
|
label: string;
|
2024-08-13 20:51:10 +02:00
|
|
|
description?: string;
|
2024-09-09 10:29:41 +02:00
|
|
|
disabled?: boolean;
|
|
|
|
|
type?: 'text' | 'password' | 'email' | 'number' | 'checkbox';
|
2024-08-12 11:00:25 +02:00
|
|
|
children?: Snippet;
|
|
|
|
|
} = $props();
|
|
|
|
|
|
|
|
|
|
const id = label.toLowerCase().replace(/ /g, '-');
|
|
|
|
|
</script>
|
|
|
|
|
|
2024-08-24 00:49:08 +02:00
|
|
|
<div {...restProps}>
|
2024-08-13 20:51:10 +02:00
|
|
|
<Label class="mb-0" for={id}>{label}</Label>
|
|
|
|
|
{#if description}
|
2024-08-24 00:49:08 +02:00
|
|
|
<p class="text-muted-foreground mt-1 text-xs">{description}</p>
|
2024-08-12 11:00:25 +02:00
|
|
|
{/if}
|
2024-08-13 20:51:10 +02:00
|
|
|
<div class="mt-2">
|
|
|
|
|
{#if children}
|
|
|
|
|
{@render children()}
|
2024-08-24 00:49:08 +02:00
|
|
|
{:else if input}
|
2024-09-09 10:29:41 +02:00
|
|
|
<Input {id} {type} bind:value={input.value} {disabled} />
|
2024-08-13 20:51:10 +02:00
|
|
|
{/if}
|
2024-08-24 00:49:08 +02:00
|
|
|
{#if input?.error}
|
|
|
|
|
<p class="mt-1 text-sm text-red-500">{input.error}</p>
|
2024-08-13 20:51:10 +02:00
|
|
|
{/if}
|
|
|
|
|
</div>
|
2024-08-12 11:00:25 +02:00
|
|
|
</div>
|