text input: change default asterisk behaviour
asterisk now shown by default if field is required.
This commit is contained in:
@@ -11,7 +11,7 @@
|
||||
value?: string;
|
||||
invalidMessage?: string | null;
|
||||
ref?: HTMLInputElement | null;
|
||||
asterisk?: boolean;
|
||||
asterisk?: boolean | null;
|
||||
class?: ClassValue | null | undefined;
|
||||
}
|
||||
|
||||
@@ -21,12 +21,16 @@
|
||||
value = $bindable(''),
|
||||
invalidMessage = 'Field is required',
|
||||
ref = $bindable<HTMLInputElement | null>(null),
|
||||
asterisk = false,
|
||||
asterisk = null,
|
||||
class: classValue,
|
||||
forceInvalid = false,
|
||||
...others
|
||||
}: Props = $props();
|
||||
|
||||
let valid: boolean = $state(true);
|
||||
let displayAsterisk = $derived(
|
||||
asterisk === true || (asterisk !== false && others.validate && others.validate.required)
|
||||
);
|
||||
|
||||
export const focus = () => {
|
||||
if (ref) ref.focus();
|
||||
@@ -37,7 +41,7 @@
|
||||
{#if label}
|
||||
<Label for={id}>
|
||||
{label}
|
||||
{#if asterisk}
|
||||
{#if displayAsterisk}
|
||||
<span class="text-red-500">*</span>
|
||||
{/if}
|
||||
</Label>
|
||||
@@ -50,11 +54,12 @@
|
||||
onvalidate={(e) => {
|
||||
valid = e.detail.valid;
|
||||
}}
|
||||
{forceInvalid}
|
||||
{...others}
|
||||
/>
|
||||
|
||||
{#if others.validate && invalidMessage !== null}
|
||||
<div class={['opacity-0 transition-opacity', !valid && 'opacity-100']}>
|
||||
<div class={['opacity-0 transition-opacity', (!valid || forceInvalid) && 'opacity-100']}>
|
||||
<Label for={id} error>
|
||||
{invalidMessage}
|
||||
</Label>
|
||||
|
||||
Reference in New Issue
Block a user