Skip to content

Modern Docker Management, Designed for Everyone

1.8 KB
Raw Blame
<script lang="ts">
	import type { HTMLInputAttributes } from 'svelte/elements';

	import { Input } from '#lib/components/ui/input/index.js';
	import { Label } from '#lib/components/ui/label/index.js';

	let {
		id,
		name,
		value = $bindable(''),
		label,
		placeholder = '',
		description,
		helpText,
		reserveHelpTextSpace = false,
		error,
		disabled = false,
		type = 'text',
		min,
		step,
		autocomplete = 'off',
		required = false,
		onChange
	}: {
		id?: string;
		name?: string;
		value: string | number;
		label: string;
		placeholder?: string;
		description?: string;
		helpText?: string;
		reserveHelpTextSpace?: boolean;
		error?: string | null;
		disabled?: boolean;
		type?: 'text' | 'email' | 'password' | 'number' | 'url';
		min?: HTMLInputAttributes['min'];
		step?: HTMLInputAttributes['step'];
		autocomplete?: HTMLInputElement['autocomplete'];
		required?: boolean;
		onChange?: (value: string) => void;
	} = $props();

	function handleInput(e: Event) {
		const target = e.target as HTMLInputElement;
		if (type === 'number') {
			const numValue = target.value === '' ? '' : Number(target.value);
			value = numValue;
			onChange?.(target.value);
		} else {
			value = target.value;
			onChange?.(target.value);
		}
	}
</script>

<div class="space-y-2.5">
	<div>
		<Label for={id}>
			{label}{#if required}<span class="ml-0.5 text-destructive">*</span>{/if}
		</Label>
		{#if description}
			<p class="mt-0.5 text-xs text-muted-foreground">{description}</p>
		{/if}
	</div>

	<Input
		{id}
		{name}
		{value}
		{placeholder}
		{disabled}
		{type}
		{min}
		{step}
		{autocomplete}
		{required}
		oninput={handleInput}
		aria-invalid={!!error}
	/>

	{#if error}
		<p class="text-xs font-medium text-destructive">{error}</p>
	{/if}
	{#if helpText || reserveHelpTextSpace}
		<p class="min-h-4 text-xs text-muted-foreground">{helpText}</p>
	{/if}
</div>