radio group: fix live validation after invalid
This commit is contained in:
@@ -23,8 +23,20 @@
|
|||||||
...props
|
...props
|
||||||
}: Props = $props();
|
}: Props = $props();
|
||||||
let valid = $state(true);
|
let valid = $state(true);
|
||||||
|
let hiddenInput: HTMLInputElement;
|
||||||
|
|
||||||
const group = new RadioGroup(props);
|
const group = new RadioGroup({
|
||||||
|
...props,
|
||||||
|
onValueChange(active) {
|
||||||
|
// call any passed in handler
|
||||||
|
props.onValueChange?.(active);
|
||||||
|
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
// send keyboard event to trigger validation
|
||||||
|
hiddenInput?.dispatchEvent(new KeyboardEvent('keyup'));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
const isVert = $derived(group.orientation === 'vertical');
|
const isVert = $derived(group.orientation === 'vertical');
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -69,8 +81,9 @@
|
|||||||
</div>
|
</div>
|
||||||
<input
|
<input
|
||||||
{...group.hiddenInput}
|
{...group.hiddenInput}
|
||||||
use:validate={{ required }}
|
use:validate={{ required, autovalOnInvalid: true }}
|
||||||
onvalidate={(e) => (valid = e.detail.valid)}
|
onvalidate={(e) => (valid = e.detail.valid)}
|
||||||
|
bind:this={hiddenInput}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{#if required}
|
{#if required}
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import PhoneInput from '$lib/PhoneInput.svelte';
|
import PhoneInput from '$lib/PhoneInput.svelte';
|
||||||
|
import RadioGroup from '$lib/RadioGroup.svelte';
|
||||||
import StateMachine, { type StateMachinePage } from '$lib/StateMachine.svelte';
|
import StateMachine, { type StateMachinePage } from '$lib/StateMachine.svelte';
|
||||||
import TextInput from '$lib/TextInput.svelte';
|
import TextInput from '$lib/TextInput.svelte';
|
||||||
import { ArrowRight, Check } from 'phosphor-svelte';
|
import { ArrowRight, Check } from 'phosphor-svelte';
|
||||||
|
|
||||||
let stateMachineRef: StateMachine;
|
let stateMachineRef: StateMachine;
|
||||||
let index = $state(1);
|
let index = $state(0);
|
||||||
|
|
||||||
const pages: StateMachinePage[] = [
|
const pages: StateMachinePage[] = [
|
||||||
{
|
{
|
||||||
@@ -79,6 +80,16 @@
|
|||||||
|
|
||||||
{#snippet firstPage()}
|
{#snippet firstPage()}
|
||||||
<TextInput label="What's your name?" validate={{ required: true }} />
|
<TextInput label="What's your name?" validate={{ required: true }} />
|
||||||
|
|
||||||
|
<RadioGroup
|
||||||
|
label="What's your favorite color?"
|
||||||
|
options={[
|
||||||
|
{ label: 'Red', value: 'red' },
|
||||||
|
{ label: 'Blue', value: 'blue' },
|
||||||
|
{ label: 'Green', value: 'green' }
|
||||||
|
]}
|
||||||
|
required={true}
|
||||||
|
/>
|
||||||
{/snippet}
|
{/snippet}
|
||||||
|
|
||||||
{#snippet secondPage()}
|
{#snippet secondPage()}
|
||||||
|
|||||||
Reference in New Issue
Block a user