-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.svelte
87 lines (77 loc) · 2.65 KB
/
form.svelte
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
<script>
import Form from '../components/form/Form.svelte'
import Input from '../components/form/Input.svelte'
import SubmitButton from '../components/form/SubmitButton.svelte'
const submit = ($fields, $errors, clear_form) => {
console.log("Fields", $fields)
console.log("Errors", $errors)
alert("Thank you!")
}
const validate = ($fields, $errors) => {
if ($fields.name.length < 5) {
$errors.name = 'Your name is so short!'
return false
}
return true
}
</script>
<style type="text/scss">
.address_form {
margin-top: 1em;
max-width: 30em;
font-size: .8em;
:global(form) {
display: grid;
grid-gap: 10px;
grid-row-gap: 1.1em;
grid-template-columns: 60% 15% 25%;
grid-template-rows: auto;
grid-template-areas:
"full_name full_name full_name"
"address address address"
"city state zip"
"save save save";
:global(fieldset) { /* display: grid doesn't work on fieldsets in chrome */
display: contents;
}
:global(.field) {
display: block;
:global(label, input) {
display: block;
width: 100%; /* unsure why I need this?
100% is actually larger than the container
99% is perfect, but then the third line doesn't match up?!
*/
}
}
}
:global(.field.full_name) { grid-area: full_name; }
:global(.field.address) { grid-area: address; }
:global(.field.city) { grid-area: city; }
:global(.field.state) { grid-area: state; }
:global(.field.zip) { grid-area: zip; }
:global(.buttons) { grid-area: save; }
}
</style>
<svelte:head>
<title>Forms</title>
</svelte:head>
<div class="flow">
<h1>Checkout</h1>
<h2>Shipping</h2>
<p>Please enter your address</p>
</div>
<div class="address_form">
<Form submit={submit} validate={validate} fieldnames="{['name', 'address', 'city', 'state', 'zip']}">
<fieldset slot="fieldset">
<Input field_class="full_name" required id="name" label="Name" field="name" type="text" placeholder="John Doe"></Input>
<Input field_class="address" required id="address" label="Address" field="address" type="text" placeholder="5 Main St"></Input>
<Input field_class="city" required id="city" label="City" field="city" type="text" placeholder="Springfield"></Input>
<Input field_class="state" required id="state" label="State" maxlength="2" field="state" type="text" placeholder="ZZ"></Input>
<Input field_class="zip" required id="zip" label="Zip code" maxlength="5" field="zip" type="number" placeholder="00000"></Input>
<div class="button">
<SubmitButton text="Save"></SubmitButton>
</div>
</fieldset>
</Form>
</div>