Skip to content

Modern Docker Management, Designed for Everyone

2.4 KB
Raw Blame
<!-- Originally From  https://github.com/pocket-id/pocket-id/blob/main/frontend/src/lib/components/form/form-input.svelte -->
<script lang="ts">
	import type { Snippet } from 'svelte';
	import type { HTMLAttributes } from 'svelte/elements';
	import type { Temporal } from 'temporal-polyfill';

	import DatePicker from '#lib/components/form/date-picker.svelte';
	import { Input } from '#lib/components/ui/input/index.js';
	import { Label } from '#lib/components/ui/label/index.js';
	import { Switch } from '#lib/components/ui/switch/index.js';
	import { Textarea } from '#lib/components/ui/textarea/index.js';
	import type { FormInput } from '#lib/types/form.js';

	let {
		input = $bindable(),
		label,
		description,
		helpText,
		warningText,
		placeholder,
		disabled = false,
		type = 'text',
		rows = 3,
		children,
		autocomplete = 'off',
		...restProps
	}: HTMLAttributes<HTMLDivElement> & {
		input?: FormInput<string | boolean | number | Temporal.PlainDate | undefined>;
		label?: string;
		description?: string;
		helpText?: string;
		warningText?: string;
		placeholder?: string;
		disabled?: boolean;
		type?: 'text' | 'password' | 'email' | 'number' | 'checkbox' | 'date' | 'switch' | 'textarea';
		rows?: number;
		children?: Snippet;
		autocomplete?: HTMLInputElement['autocomplete'];
	} = $props();

	const id = $derived(label?.toLowerCase().replace(/ /g, '-'));
</script>

<div {...restProps}>
	{#if label}
		<Label class="mb-0" for={id}>{label}</Label>
	{/if}
	{#if description}
		<p class="mt-1 text-xs text-muted-foreground">{description}</p>
	{/if}
	<div class={label || description ? 'mt-2' : ''}>
		{#if children}
			{@render children()}
		{:else if input}
			{#if type === 'switch'}
				<Switch {id} bind:checked={input.value as boolean} {disabled} />
			{:else if type === 'textarea'}
				<Textarea {id} {placeholder} {rows} bind:value={input.value as string} {disabled} />
			{:else if type === 'date'}
				<DatePicker {id} bind:value={input.value as Temporal.PlainDate} {disabled} />
			{:else}
				<Input {id} {placeholder} {type} bind:value={input.value} {disabled} {autocomplete} />
			{/if}
		{/if}
		{#if input?.error}
			<p class="mt-1 text-xs font-medium text-destructive">{input.error}</p>
		{/if}
		{#if helpText}
			<p class="mt-1 text-xs text-muted-foreground">{helpText}</p>
		{/if}
		{#if warningText}
			<p class="mt-1 text-xs font-semibold text-destructive">{warningText}</p>
		{/if}
	</div>
</div>