text input: make name optional, custom class support
This commit is contained in:
@@ -2,18 +2,12 @@
|
|||||||
import Label from './Label.svelte';
|
import Label from './Label.svelte';
|
||||||
import StyledRawInput from './StyledRawInput.svelte';
|
import StyledRawInput from './StyledRawInput.svelte';
|
||||||
import { type ValidatorOptions } from '@svelte-toolkit/validate';
|
import { type ValidatorOptions } from '@svelte-toolkit/validate';
|
||||||
|
import { generateIdentifier } from './util.js';
|
||||||
|
import type { ClassValue } from 'svelte/elements';
|
||||||
|
|
||||||
let {
|
interface Props {
|
||||||
name,
|
id?: string;
|
||||||
label,
|
name?: string;
|
||||||
value = $bindable(''),
|
|
||||||
placeholder,
|
|
||||||
type,
|
|
||||||
validate: validateOpts,
|
|
||||||
invalidMessage = 'Field is required',
|
|
||||||
ref = $bindable<HTMLInputElement | null>(null)
|
|
||||||
}: {
|
|
||||||
name: string;
|
|
||||||
label?: string;
|
label?: string;
|
||||||
value?: string;
|
value?: string;
|
||||||
placeholder?: string;
|
placeholder?: string;
|
||||||
@@ -21,18 +15,33 @@
|
|||||||
validate?: ValidatorOptions;
|
validate?: ValidatorOptions;
|
||||||
invalidMessage?: string;
|
invalidMessage?: string;
|
||||||
ref?: HTMLInputElement | null;
|
ref?: HTMLInputElement | null;
|
||||||
} = $props();
|
class?: ClassValue | null | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
let {
|
||||||
|
id = generateIdentifier('text-input'),
|
||||||
|
name,
|
||||||
|
label,
|
||||||
|
value = $bindable(''),
|
||||||
|
placeholder,
|
||||||
|
type,
|
||||||
|
validate: validateOpts,
|
||||||
|
invalidMessage = 'Field is required',
|
||||||
|
ref = $bindable<HTMLInputElement | null>(null),
|
||||||
|
class: classList
|
||||||
|
}: Props = $props();
|
||||||
|
|
||||||
let valid: boolean = $state(true);
|
let valid: boolean = $state(true);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
{#if label}
|
{#if label}
|
||||||
<Label for={name}>{label}</Label>
|
<Label for={id}>{label}</Label>
|
||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<StyledRawInput
|
<StyledRawInput
|
||||||
{placeholder}
|
{placeholder}
|
||||||
|
{id}
|
||||||
{name}
|
{name}
|
||||||
{type}
|
{type}
|
||||||
bind:value
|
bind:value
|
||||||
@@ -41,11 +50,12 @@
|
|||||||
onvalidate={(e) => {
|
onvalidate={(e) => {
|
||||||
valid = e.detail.valid;
|
valid = e.detail.valid;
|
||||||
}}
|
}}
|
||||||
|
class={classList}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{#if validateOpts}
|
{#if validateOpts}
|
||||||
<div class={['opacity-0 transition-opacity', !valid && 'opacity-100']}>
|
<div class={['opacity-0 transition-opacity', !valid && 'opacity-100']}>
|
||||||
<Label for={name} error>
|
<Label for={id} error>
|
||||||
{invalidMessage}
|
{invalidMessage}
|
||||||
</Label>
|
</Label>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -117,8 +117,8 @@
|
|||||||
<div class="component">
|
<div class="component">
|
||||||
<p class="title">Styled Raw Input, Text Input</p>
|
<p class="title">Styled Raw Input, Text Input</p>
|
||||||
|
|
||||||
<StyledRawInput name="example-raw-input" placeholder="Type here..." />
|
<StyledRawInput placeholder="Type here..." />
|
||||||
<TextInput name="example-text-input" placeholder="Enter text..." />
|
<TextInput label="Write something here" placeholder="Enter text..." />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="component">
|
<div class="component">
|
||||||
|
|||||||
Reference in New Issue
Block a user