text input: change default asterisk behaviour

asterisk now shown by default if field is required.
This commit is contained in:
Elijah Duffy
2026-02-04 14:53:14 -08:00
parent 5e5f133763
commit 90ff836061

View File

@@ -11,7 +11,7 @@
value?: string; value?: string;
invalidMessage?: string | null; invalidMessage?: string | null;
ref?: HTMLInputElement | null; ref?: HTMLInputElement | null;
asterisk?: boolean; asterisk?: boolean | null;
class?: ClassValue | null | undefined; class?: ClassValue | null | undefined;
} }
@@ -21,12 +21,16 @@
value = $bindable(''), value = $bindable(''),
invalidMessage = 'Field is required', invalidMessage = 'Field is required',
ref = $bindable<HTMLInputElement | null>(null), ref = $bindable<HTMLInputElement | null>(null),
asterisk = false, asterisk = null,
class: classValue, class: classValue,
forceInvalid = false,
...others ...others
}: Props = $props(); }: Props = $props();
let valid: boolean = $state(true); let valid: boolean = $state(true);
let displayAsterisk = $derived(
asterisk === true || (asterisk !== false && others.validate && others.validate.required)
);
export const focus = () => { export const focus = () => {
if (ref) ref.focus(); if (ref) ref.focus();
@@ -37,7 +41,7 @@
{#if label} {#if label}
<Label for={id}> <Label for={id}>
{label} {label}
{#if asterisk} {#if displayAsterisk}
<span class="text-red-500">*</span> <span class="text-red-500">*</span>
{/if} {/if}
</Label> </Label>
@@ -50,11 +54,12 @@
onvalidate={(e) => { onvalidate={(e) => {
valid = e.detail.valid; valid = e.detail.valid;
}} }}
{forceInvalid}
{...others} {...others}
/> />
{#if others.validate && invalidMessage !== null} {#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> <Label for={id} error>
{invalidMessage} {invalidMessage}
</Label> </Label>