styled raw & text input: include all props & add disabled
This commit is contained in:
@@ -8,7 +8,7 @@
|
|||||||
import { generateIdentifier } from './util';
|
import { generateIdentifier } from './util';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
|
|
||||||
type $Props = Omit<HTMLInputAttributes, 'name' | 'value' | 'class'> & {
|
type $Props = Omit<HTMLInputAttributes, 'name' | 'value'> & {
|
||||||
name?: string;
|
name?: string;
|
||||||
value?: string;
|
value?: string;
|
||||||
validate?: ValidatorOptions;
|
validate?: ValidatorOptions;
|
||||||
@@ -16,7 +16,6 @@
|
|||||||
use?: (node: HTMLInputElement) => void;
|
use?: (node: HTMLInputElement) => void;
|
||||||
ref?: HTMLInputElement | null;
|
ref?: HTMLInputElement | null;
|
||||||
onvalidate?: (e: InputValidatorEvent) => void;
|
onvalidate?: (e: InputValidatorEvent) => void;
|
||||||
class?: ClassValue | null | undefined;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let {
|
let {
|
||||||
@@ -25,6 +24,7 @@
|
|||||||
value = $bindable(''),
|
value = $bindable(''),
|
||||||
placeholder,
|
placeholder,
|
||||||
validate: validateOpts,
|
validate: validateOpts,
|
||||||
|
disabled = false,
|
||||||
focus: focusOnMount = false,
|
focus: focusOnMount = false,
|
||||||
use,
|
use,
|
||||||
ref = $bindable<HTMLInputElement | null>(null),
|
ref = $bindable<HTMLInputElement | null>(null),
|
||||||
@@ -68,8 +68,11 @@
|
|||||||
'dark:text-sui-background dark:placeholder:text-sui-background/60 dark:sm:bg-slate-800',
|
'dark:text-sui-background dark:placeholder:text-sui-background/60 dark:sm:bg-slate-800',
|
||||||
'ring-sui-primary ring-offset-1 placeholder-shown:text-ellipsis focus:ring-2',
|
'ring-sui-primary ring-offset-1 placeholder-shown:text-ellipsis focus:ring-2',
|
||||||
!valid && 'border-red-500!',
|
!valid && 'border-red-500!',
|
||||||
|
disabled &&
|
||||||
|
'border-sui-accent/20 text-sui-text/60 dark:text-sui-background/60 cursor-not-allowed',
|
||||||
classValue
|
classValue
|
||||||
]}
|
]}
|
||||||
|
{disabled}
|
||||||
use:validate={validateOpts}
|
use:validate={validateOpts}
|
||||||
use:conditionalUse
|
use:conditionalUse
|
||||||
onvalidate={(e) => {
|
onvalidate={(e) => {
|
||||||
|
|||||||
@@ -1,38 +1,27 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
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 { generateIdentifier } from './util';
|
import { generateIdentifier } from './util';
|
||||||
import type { ClassValue } from 'svelte/elements';
|
import type { ClassValue } from 'svelte/elements';
|
||||||
|
import type { ComponentProps } from 'svelte';
|
||||||
|
|
||||||
interface Props {
|
interface Props extends ComponentProps<typeof StyledRawInput> {
|
||||||
id?: string;
|
id?: string;
|
||||||
name?: string;
|
|
||||||
label?: string;
|
label?: string;
|
||||||
value?: string;
|
value?: string;
|
||||||
placeholder?: string;
|
|
||||||
type?: HTMLInputElement['type'];
|
|
||||||
validate?: ValidatorOptions;
|
|
||||||
invalidMessage?: string;
|
invalidMessage?: string;
|
||||||
focus?: boolean;
|
|
||||||
use?: (node: HTMLInputElement) => void;
|
|
||||||
ref?: HTMLInputElement | null;
|
ref?: HTMLInputElement | null;
|
||||||
class?: ClassValue | null | undefined;
|
class?: ClassValue | null | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
let {
|
let {
|
||||||
id = generateIdentifier('text-input'),
|
id = generateIdentifier('text-input'),
|
||||||
name,
|
|
||||||
label,
|
label,
|
||||||
value = $bindable(''),
|
value = $bindable(''),
|
||||||
placeholder,
|
|
||||||
type,
|
|
||||||
validate: validateOpts,
|
|
||||||
invalidMessage = 'Field is required',
|
invalidMessage = 'Field is required',
|
||||||
focus: focusOnMount = false,
|
|
||||||
use,
|
|
||||||
ref = $bindable<HTMLInputElement | null>(null),
|
ref = $bindable<HTMLInputElement | null>(null),
|
||||||
class: classValue
|
class: classValue,
|
||||||
|
...others
|
||||||
}: Props = $props();
|
}: Props = $props();
|
||||||
|
|
||||||
let valid: boolean = $state(true);
|
let valid: boolean = $state(true);
|
||||||
@@ -48,21 +37,16 @@
|
|||||||
{/if}
|
{/if}
|
||||||
|
|
||||||
<StyledRawInput
|
<StyledRawInput
|
||||||
{placeholder}
|
|
||||||
{id}
|
{id}
|
||||||
{name}
|
|
||||||
{type}
|
|
||||||
bind:value
|
bind:value
|
||||||
bind:ref
|
bind:ref
|
||||||
validate={validateOpts}
|
|
||||||
onvalidate={(e) => {
|
onvalidate={(e) => {
|
||||||
valid = e.detail.valid;
|
valid = e.detail.valid;
|
||||||
}}
|
}}
|
||||||
focus={focusOnMount}
|
{...others}
|
||||||
{use}
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{#if validateOpts}
|
{#if others.validate}
|
||||||
<div class={['opacity-0 transition-opacity', !valid && 'opacity-100']}>
|
<div class={['opacity-0 transition-opacity', !valid && 'opacity-100']}>
|
||||||
<Label for={id} error>
|
<Label for={id} error>
|
||||||
{invalidMessage}
|
{invalidMessage}
|
||||||
|
|||||||
@@ -150,12 +150,16 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="component">
|
<div class="component">
|
||||||
<p class="title">Styled Raw Input, Text Input</p>
|
<p class="title">Styled Raw Input, Text Input, Disabled Input</p>
|
||||||
|
|
||||||
<InputGroup>
|
<InputGroup>
|
||||||
<StyledRawInput placeholder="Type here..." class="basis-1/2" />
|
<StyledRawInput placeholder="Type here..." class="basis-1/2" />
|
||||||
<TextInput label="Write something here" placeholder="Enter text..." class="basis-1/2" />
|
<TextInput label="Write something here" placeholder="Enter text..." class="basis-1/2" />
|
||||||
</InputGroup>
|
</InputGroup>
|
||||||
|
|
||||||
|
<InputGroup>
|
||||||
|
<TextInput label="Disabled input" placeholder="You can't enter text" disabled />
|
||||||
|
</InputGroup>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="component">
|
<div class="component">
|
||||||
|
|||||||
Reference in New Issue
Block a user