fixed automatic enter button and add a summary state
This commit is contained in:
parent
20563628e3
commit
b5cd51e125
@ -42,24 +42,47 @@
|
|||||||
},
|
},
|
||||||
input: {
|
input: {
|
||||||
on: {
|
on: {
|
||||||
NEXT: {
|
NEXT: [
|
||||||
target: 'input',
|
{
|
||||||
actions: assign({
|
target: 'submitting',
|
||||||
currentField: (context) => context.currentField + 1
|
actions: [
|
||||||
}),
|
assign({
|
||||||
cond: (context) => context.currentField < fields.length - 1
|
formData: (context) => ({
|
||||||
},
|
...context.formData,
|
||||||
|
[fields[context.currentField].name]: form[fields[context.currentField].name]
|
||||||
|
})
|
||||||
|
})
|
||||||
|
],
|
||||||
|
cond: (context) => context.currentField === fields.length - 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
target: 'input',
|
||||||
|
actions: [
|
||||||
|
assign({
|
||||||
|
currentField: (context) => context.currentField + 1,
|
||||||
|
formData: (context) => ({
|
||||||
|
...context.formData,
|
||||||
|
[fields[context.currentField].name]: form[fields[context.currentField].name]
|
||||||
|
})
|
||||||
|
})
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
PREV: {
|
PREV: {
|
||||||
target: 'input',
|
target: 'input',
|
||||||
actions: assign({
|
actions: assign({
|
||||||
currentField: (context) => context.currentField - 1
|
currentField: (context) => context.currentField - 1
|
||||||
}),
|
}),
|
||||||
cond: (context) => context.currentField > 0
|
cond: (context) => context.currentField > 0
|
||||||
},
|
}
|
||||||
SUBMIT: 'submitting'
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
submitting: {
|
submitting: {
|
||||||
|
on: {
|
||||||
|
SUBMIT: 'submitted'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
submitted: {
|
||||||
// Add your submission logic here
|
// Add your submission logic here
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -67,20 +90,27 @@
|
|||||||
|
|
||||||
const { state, send } = useMachine(formMachine);
|
const { state, send } = useMachine(formMachine);
|
||||||
|
|
||||||
async function handleSubmit() {
|
async function handleNext() {
|
||||||
const validationResult = await validate();
|
const currentFieldName = fields[$state.context.currentField].name;
|
||||||
|
const validationResult = await validate(currentFieldName);
|
||||||
|
|
||||||
if (!validationResult.valid) {
|
if (validationResult && !validationResult.valid) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(form);
|
send('NEXT');
|
||||||
send('SUBMIT');
|
}
|
||||||
|
|
||||||
|
function handleKeyDown(event) {
|
||||||
|
if (event.key === 'Enter') {
|
||||||
|
event.preventDefault();
|
||||||
|
handleNext();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="p-6">
|
<div class="p-6">
|
||||||
<form on:submit|preventDefault={handleSubmit} class="w-full max-w-md">
|
<form on:submit|preventDefault={handleNext} on:keydown={handleKeyDown} class="w-full max-w-md">
|
||||||
<div class="mb-4">
|
<div class="mb-4">
|
||||||
{#if $errors[fields[$state.context.currentField].name]}
|
{#if $errors[fields[$state.context.currentField].name]}
|
||||||
<span class="block mb-2 font-semibold text-red-500">
|
<span class="block mb-2 font-semibold text-red-500">
|
||||||
@ -153,6 +183,14 @@
|
|||||||
/>
|
/>
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
{#if $state.matches('submitting')}
|
||||||
|
<div class="mb-4">
|
||||||
|
<h2 class="text-lg font-semibold text-white">Summary</h2>
|
||||||
|
{#each Object.entries($state.context.formData) as [key, value]}
|
||||||
|
<p class="mt-1 text-white">{key}: {value}</p>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
<div class="flex justify-between mt-4">
|
<div class="flex justify-between mt-4">
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
@ -162,20 +200,24 @@
|
|||||||
>
|
>
|
||||||
Previous
|
Previous
|
||||||
</button>
|
</button>
|
||||||
<button
|
{#if !$state.matches('submitting')}
|
||||||
type="button"
|
<button
|
||||||
on:click={() => send('NEXT')}
|
type="button"
|
||||||
class="px-4 py-2 text-white bg-blue-500 rounded-md hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500"
|
on:click={() => send('NEXT')}
|
||||||
disabled={$errors[fields[$state.context.currentField].name]}
|
class="px-4 py-2 text-white bg-blue-500 rounded-md hover:bg-blue-600 focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||||
>
|
disabled={$errors[fields[$state.context.currentField].name]}
|
||||||
Next
|
>
|
||||||
</button>
|
Next
|
||||||
<button
|
</button>
|
||||||
type="submit"
|
{/if}
|
||||||
class="px-4 py-2 text-white bg-green-500 rounded-md hover:bg-green-600 focus:outline-none focus:ring-2 focus:ring-green-500"
|
{#if $state.matches('submitting')}
|
||||||
>
|
<button
|
||||||
Submit
|
type="submit"
|
||||||
</button>
|
class="px-4 py-2 text-white bg-green-500 rounded-md hover:bg-green-600 focus:outline-none focus:ring-2 focus:ring-green-500"
|
||||||
|
>
|
||||||
|
Submit
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user