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-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,
|
2024-08-13 20:51:10 +02:00
|
|
|
description,
|
2025-01-19 06:02:07 -06:00
|
|
|
placeholder,
|
2024-09-09 10:29:41 +02:00
|
|
|
disabled = false,
|
|
|
|
|
type = 'text',
|
2024-08-24 00:49:08 +02:00
|
|
|
children,
|
2024-10-02 08:43:44 +02:00
|
|
|
onInput,
|
2024-08-24 00:49:08 +02:00
|
|
|
...restProps
|
|
|
|
|
}: HTMLAttributes<HTMLDivElement> & {
|
|
|
|
|
input?: FormInput<string | boolean | number>;
|
2024-10-28 18:11:54 +01:00
|
|
|
label?: string;
|
2024-08-13 20:51:10 +02:00
|
|
|
description?: string;
|
2025-01-19 06:02:07 -06:00
|
|
|
placeholder?: string;
|
2024-09-09 10:29:41 +02:00
|
|
|
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>
|
|
|
|
|
|
2024-08-24 00:49:08 +02:00
|
|
|
<div {...restProps}>
|
2024-10-28 18:11:54 +01:00
|
|
|
{#if label}
|
|
|
|
|
<Label class="mb-0" for={id}>{label}</Label>
|
|
|
|
|
{/if}
|
2024-08-13 20:51:10 +02:00
|
|
|
{#if description}
|
2025-01-19 15:41:16 +01:00
|
|
|
<p class="mt-1 text-xs text-muted-foreground">{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' : ''}>
|
2024-08-13 20:51:10 +02:00
|
|
|
{#if children}
|
|
|
|
|
{@render children()}
|
2024-08-24 00:49:08 +02:00
|
|
|
{:else if input}
|
2025-01-19 15:41:16 +01:00
|
|
|
<Input
|
|
|
|
|
{id}
|
|
|
|
|
{placeholder}
|
|
|
|
|
{type}
|
|
|
|
|
bind:value={input.value}
|
|
|
|
|
{disabled}
|
|
|
|
|
on:input={(e) => onInput?.(e)}
|
|
|
|
|
/>
|
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>
|