-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
demo.vue
118 lines (106 loc) · 3.62 KB
/
demo.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<script setup lang="ts">
import { useStepper } from '@vueuse/core'
import { reactive } from 'vue'
const form = reactive({
firstName: 'Jon',
lastName: '',
billingAddress: '',
contractAccepted: false,
carbonOffsetting: false,
payment: 'credit-card' as 'paypal' | 'credit-card',
})
const stepper = useStepper({
'user-information': {
title: 'User information',
isValid: () => form.firstName && form.lastName,
},
'billing-address': {
title: 'Billing address',
isValid: () => form.billingAddress?.trim() !== '',
},
'terms': {
title: 'Terms',
isValid: () => form.contractAccepted === true,
},
'payment': {
title: 'Payment',
isValid: () => ['credit-card', 'paypal'].includes(form.payment),
},
})
function submit() {
if (stepper.current.value.isValid())
stepper.goToNext()
}
function allStepsBeforeAreValid(index: number): boolean {
return !Array(index)
.fill(null)
.some((_, i) => !stepper.at(i)?.isValid())
}
</script>
<template>
<div>
<div class="flex gap-2 justify-center">
<div v-for="(step, id, i) in stepper.steps.value" :key="id" class="">
<button
:disabled="!allStepsBeforeAreValid(i) && stepper.isBefore(id)"
@click="stepper.goTo(id)"
v-text="step.title"
/>
</div>
</div>
<form class="mt-10" @submit.prevent="submit">
<span class="text-lg font-bold" v-text="stepper.current.value.title" />
<div class="flex flex-col justify-center gap-2 mt-2">
<div>
<div v-if="stepper.isCurrent('user-information')">
<span>First name:</span>
<input v-model="form.firstName" class="!mt-0.5" type="text">
<span>Last name:</span>
<input v-model="form.lastName" class="!mt-0.5" type="text">
</div>
<div v-if="stepper.isCurrent('billing-address')">
<input v-model="form.billingAddress" type="text">
</div>
<div v-if="stepper.isCurrent('terms')">
<div>
<input id="carbon-offsetting" v-model="form.carbonOffsetting" type="checkbox" class="mr-2">
<label for="carbon-offsetting">I accept to deposit a carbon offsetting fee</label>
</div>
<div>
<input id="contract" v-model="form.contractAccepted" type="checkbox" class="mr-2">
<label for="contract">I accept the terms of the contract</label>
</div>
</div>
<div v-if="stepper.isCurrent('payment')">
<div>
<input id="credit-card" v-model="form.payment" type="radio" class="mr-2" value="credit-card">
<label for="credit-card">Credit card</label>
</div>
<div>
<input id="paypal" v-model="form.payment" type="radio" class="mr-2" value="paypal">
<label for="paypal">PayPal</label>
</div>
</div>
</div>
<div>
<button v-if="!stepper.isLast" :disabled="!stepper.current.value.isValid()">
Next
</button>
<button v-if="stepper.isLast" :disabled="!stepper.current.value.isValid()">
Submit
</button>
</div>
</div>
</form>
<div class="flex flex-col gap-4 mt-12">
<div class="w-full px-4 py-2 rounded border border-main space-y-2 overflow-auto h-full">
<span class="font-bold">Form</span>
<pre v-text="form" />
</div>
<div class="w-full px-4 py-2 rounded border border-main space-y-2 overflow-auto h-full">
<span class="font-bold">Wizard</span>
<pre v-text="stepper" />
</div>
</div>
</div>
</template>