Repository navigation
Expand file tree
/
Copy pathforms.html
More file actions
212 lines (179 loc) · 8.77 KB
/
Copy pathforms.html
File metadata and controls
212 lines (179 loc) · 8.77 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Form validation - rnxJS</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Archivo:wght@600;700&family=JetBrains+Mono:wght@400;500&family=Public+Sans:wght@400;500;600&display=swap" rel="stylesheet">
<link rel="stylesheet" href="index.css">
<link rel="stylesheet" href="demo.css">
</head>
<body>
<div class="topbar">
<div class="shell topbar-in mono">
<a class="wordmark" href="./" style="color:inherit;text-decoration:none">rnxJS</a>
<span class="sep">/</span>
<span>forms</span>
<span class="spacer"></span>
<a class="chan" href="./"><i class="bi bi-arrow-left"></i> All demos</a>
</div>
</div>
<div class="shell">
<header class="page-head">
<div class="titles">
<h1>Onboard a carrier</h1>
<p class="sub">
Validation runs against the same reactive state the inputs write to.
Errors appear on blur, not on every keystroke.
</p>
</div>
</header>
<section class="grid-split">
<div class="panel">
<div class="panel-head">
<h2>Carrier details</h2>
<p class="hint">All fields required unless marked optional</p>
</div>
<div class="panel-body">
<div class="form-grid" data-ref="form"></div>
<div class="form-actions" data-ref="actions"></div>
</div>
</div>
<div class="stack">
<div class="panel">
<div class="panel-head"><h2>Validation state</h2></div>
<div class="panel-body">
<ul class="feed" data-ref="state"></ul>
</div>
</div>
<div class="panel">
<div class="panel-head"><h2>What this shows</h2></div>
<div class="panel-body">
<p style="margin:0;font-size:0.9rem;color:var(--muted);line-height:1.6">
Every field is an rnxJS <code>Input</code> or <code>Select</code>
bound to one state object. The panel above reads that same
object, so it cannot drift from what the form holds.
</p>
</div>
</div>
</div>
</section>
</div>
<footer style="margin-top:2.5rem">
<div class="shell" style="display:flex;gap:1.25rem;flex-wrap:wrap;align-items:center;">
<span class="mono" data-ref="footVersion">rnxJS</span>
<a href="./">All demos</a>
<a href="https://github.com/BaryoDev/rnxJS_samples">Source</a>
<span style="margin-left:auto" class="mono">MPL-2.0</span>
</div>
</footer>
<script src="https://cdn.jsdelivr.net/npm/@arnelirobles/rnxjs@2.3.1/dist/rnx.global.js"></script>
<script>
const { Input, Select, Textarea, Button, createReactiveState } = rnx;
const $ = (ref) => document.querySelector(`[data-ref="${ref}"]`);
const state = createReactiveState({
company: '', vat: '', contact: '', email: '', phone: '',
country: 'nl', fleet: '', notes: '',
});
const RULES = {
company: (v) => !v.trim() ? 'Enter the registered company name'
: v.trim().length < 2 ? 'That looks too short' : '',
vat: (v) => !v.trim() ? 'Enter the VAT number'
: !/^[A-Z]{2}[0-9A-Z]{6,12}$/i.test(v.trim()) ? 'Two letters then 6 to 12 characters, like NL812345678' : '',
contact: (v) => !v.trim() ? 'Who should we contact' : '',
email: (v) => !v.trim() ? 'Enter an email address'
: !/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(v.trim()) ? 'That is not a valid email address' : '',
phone: (v) => v.trim() && !/^[+0-9 ()-]{7,}$/.test(v.trim()) ? 'Digits, spaces and + only' : '',
fleet: (v) => !v.trim() ? 'How many vehicles'
: !/^\d+$/.test(v.trim()) ? 'Whole numbers only'
: Number(v) < 1 ? 'Must be at least one' : '',
};
const FIELDS = [
['company', 'Company name', 'Meridian Haulage BV'],
['vat', 'VAT number', 'NL812345678'],
['contact', 'Main contact', 'Full name'],
['email', 'Email', 'name@company.com'],
['phone', 'Phone (optional)', '+31 20 123 4567'],
['fleet', 'Fleet size', 'Number of vehicles'],
];
const touched = {};
// ---- build ----------------------------------------------------------
for (const [key, label, placeholder] of FIELDS) {
const wrap = document.createElement('div');
wrap.className = 'field';
wrap.dataset.field = key;
const l = document.createElement('label');
l.textContent = label;
const control = Input({ placeholder });
const err = document.createElement('span');
err.className = 'field-error';
wrap.append(l, control, err);
$('form').append(wrap);
const el = control.querySelector('input') || control;
el.addEventListener('input', () => { state[key] = el.value; paint(); });
el.addEventListener('blur', () => { touched[key] = true; paint(); });
}
const country = document.createElement('div');
country.className = 'field';
const cl = document.createElement('label');
cl.textContent = 'Country of registration';
country.append(cl, Select({
value: 'nl',
options: [
{ value: 'nl', label: 'Netherlands' }, { value: 'de', label: 'Germany' },
{ value: 'be', label: 'Belgium' }, { value: 'pl', label: 'Poland' },
],
}));
$('form').append(country);
const notes = document.createElement('div');
notes.className = 'field wide';
const nl = document.createElement('label');
nl.textContent = 'Notes (optional)';
const ta = Textarea({ rows: 3, placeholder: 'Anything the ops team should know' });
notes.append(nl, ta);
$('form').append(notes);
const submit = Button({ label: 'Submit application', icon: 'check-lg', variant: 'primary' });
$('actions').append(submit);
// ---- validate -------------------------------------------------------
function errors() {
const out = {};
for (const key of Object.keys(RULES)) {
const msg = RULES[key](state[key] || '');
if (msg) out[key] = msg;
}
return out;
}
function paint() {
const errs = errors();
for (const [key] of FIELDS) {
const wrap = document.querySelector(`[data-field="${key}"]`);
const show = touched[key] && errs[key];
wrap.classList.toggle('is-invalid', !!show);
wrap.classList.toggle('is-valid', touched[key] && !errs[key] && !!(state[key] || '').trim());
wrap.querySelector('.field-error').textContent = show ? errs[key] : '';
}
const remaining = Object.keys(errs).length;
$('state').innerHTML = FIELDS.map(([key, label]) => {
const bad = errs[key];
const filled = !!(state[key] || '').trim();
const tone = bad ? (touched[key] ? 'bad' : '') : 'ok';
return `<li>
<span class="dot ${tone}"></span>
<span class="what">${label}</span>
<span class="when">${bad ? (touched[key] ? 'invalid' : filled ? 'checking' : 'empty') : 'ok'}</span>
</li>`;
}).join('');
const btn = submit.querySelector('button') || submit;
btn.disabled = remaining > 0;
btn.textContent = remaining > 0 ? `${remaining} field${remaining > 1 ? 's' : ''} to go` : 'Submit application';
}
paint();
const build = (document.documentElement.dataset.rnxVersion || '').trim();
if (build) $('footVersion').textContent = 'rnxJS ' + build;
</script>
</body>
</html>