fixed automatic enter button and add a summary state
This commit is contained in:
		@@ -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,6 +200,7 @@
 | 
				
			|||||||
			>
 | 
								>
 | 
				
			||||||
				Previous
 | 
									Previous
 | 
				
			||||||
			</button>
 | 
								</button>
 | 
				
			||||||
 | 
								{#if !$state.matches('submitting')}
 | 
				
			||||||
				<button
 | 
									<button
 | 
				
			||||||
					type="button"
 | 
										type="button"
 | 
				
			||||||
					on:click={() => send('NEXT')}
 | 
										on:click={() => send('NEXT')}
 | 
				
			||||||
@@ -170,12 +209,15 @@
 | 
				
			|||||||
				>
 | 
									>
 | 
				
			||||||
					Next
 | 
										Next
 | 
				
			||||||
				</button>
 | 
									</button>
 | 
				
			||||||
 | 
								{/if}
 | 
				
			||||||
 | 
								{#if $state.matches('submitting')}
 | 
				
			||||||
				<button
 | 
									<button
 | 
				
			||||||
					type="submit"
 | 
										type="submit"
 | 
				
			||||||
					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"
 | 
										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
 | 
										Submit
 | 
				
			||||||
				</button>
 | 
									</button>
 | 
				
			||||||
 | 
								{/if}
 | 
				
			||||||
		</div>
 | 
							</div>
 | 
				
			||||||
	</form>
 | 
						</form>
 | 
				
			||||||
</div>
 | 
					</div>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user