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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
use kael::{prelude::FluentBuilder, *};
use kael_ui::{
components::{
button::{Button, ButtonSize, ButtonVariant},
input::{Input, InputSize, InputType, InputVariant},
input_state::InputState,
scrollable::scrollable_vertical,
},
layout::VStack,
theme::{install_theme, Theme},
};
use std::path::PathBuf;
struct Assets {
base: PathBuf,
}
impl kael::AssetSource for Assets {
fn load(&self, path: &str) -> Result<Option<std::borrow::Cow<'static, [u8]>>> {
std::fs::read(self.base.join(path))
.map(|data| Some(std::borrow::Cow::Owned(data)))
.map_err(|err| err.into())
}
fn list(&self, path: &str) -> Result<Vec<kael::SharedString>> {
std::fs::read_dir(self.base.join(path))
.map(|entries| {
entries
.filter_map(|entry| {
entry
.ok()
.and_then(|entry| entry.file_name().into_string().ok())
.map(kael::SharedString::from)
})
.collect()
})
.map_err(|err| err.into())
}
}
struct ValidationDemoApp {
// Various input types with validation
email_input: Entity<InputState>,
password_input: Entity<InputState>,
confirm_password_input: Entity<InputState>,
phone_input: Entity<InputState>,
credit_card_input: Entity<InputState>,
url_input: Entity<InputState>,
number_input: Entity<InputState>,
date_input: Entity<InputState>,
time_input: Entity<InputState>,
username_input: Entity<InputState>,
bio_input: Entity<InputState>,
// Form validation state
form_is_valid: bool,
form_submitted: bool,
}
impl ValidationDemoApp {
fn new(cx: &mut Context<Self>) -> Self {
Self {
// Email with built-in validation
email_input: cx.new(|cx| {
InputState::new(cx)
.input_type(InputType::Email)
.required(true)
.placeholder("email@example.com")
}),
// Password with minimum length
password_input: cx.new(|cx| {
InputState::new(cx)
.input_type(InputType::Password)
.required(true)
.min_length(8)
.placeholder("Enter password")
}),
// Confirm password (will validate match)
confirm_password_input: cx.new(|cx| {
InputState::new(cx)
.input_type(InputType::Password)
.required(true)
.placeholder("Confirm password")
}),
// Phone with formatting
phone_input: cx.new(|cx| {
InputState::new(cx)
.input_type(InputType::Tel)
.required(true)
.placeholder("(555) 555-5555")
}),
// Credit card with validation
credit_card_input: cx.new(|cx| {
InputState::new(cx)
.input_type(InputType::CreditCard)
.placeholder("1234 5678 9012 3456")
}),
// URL validation
url_input: cx.new(|cx| {
InputState::new(cx)
.input_type(InputType::Url)
.placeholder("https://example.com")
}),
// Number with min/max
number_input: cx.new(|cx| {
let mut state = InputState::new(cx).input_type(InputType::Number);
state.validation_rules.min_value = Some(1.0);
state.validation_rules.max_value = Some(100.0);
state.placeholder = "1-100".into();
state
}),
// Date input
date_input: cx.new(|cx| {
InputState::new(cx)
.input_type(InputType::Date)
.placeholder("MM/DD/YYYY")
}),
// Time input
time_input: cx.new(|cx| {
InputState::new(cx)
.input_type(InputType::Time)
.placeholder("HH:MM")
}),
// Username with length constraints
username_input: cx.new(|cx| {
InputState::new(cx)
.required(true)
.min_length(3)
.max_length(20)
.placeholder("johndoe")
}),
// Bio with character count
bio_input: cx.new(|cx| {
InputState::new(cx)
.max_length(200)
.placeholder("Tell us about yourself...")
}),
form_is_valid: false,
form_submitted: false,
}
}
fn validate_form(&mut self, cx: &mut Context<Self>) {
// Check if passwords match
let password = self.password_input.read(cx).content.clone();
let confirm = self.confirm_password_input.read(cx).content.clone();
if !password.is_empty() && !confirm.is_empty() && password != confirm {
self.confirm_password_input.update(cx, |state, cx| {
state.validation_error = Some(kael_ui::components::input::ValidationError {
message: "Passwords do not match".into(),
field_name: None,
});
cx.notify();
});
}
// Check overall form validity
let email_valid = self.email_input.read(cx).validation_error.is_none();
let password_valid =
self.password_input.read(cx).validation_error.is_none() && password.len() >= 8;
let passwords_match = password == confirm;
let phone_valid = self.phone_input.read(cx).validation_error.is_none();
let username_valid = self.username_input.read(cx).validation_error.is_none();
self.form_is_valid =
email_valid && password_valid && passwords_match && phone_valid && username_valid;
}
fn submit_form(&mut self, cx: &mut Context<Self>) {
// Validate all fields
let _ = self.email_input.update(cx, |state, cx| state.validate(cx));
let _ = self
.password_input
.update(cx, |state, cx| state.validate(cx));
let _ = self
.confirm_password_input
.update(cx, |state, cx| state.validate(cx));
let _ = self.phone_input.update(cx, |state, cx| state.validate(cx));
let _ = self
.username_input
.update(cx, |state, cx| state.validate(cx));
self.validate_form(cx);
if self.form_is_valid {
self.form_submitted = true;
cx.notify();
}
}
}
impl Render for ValidationDemoApp {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let theme = kael_ui::theme::use_theme();
VStack::new()
.size_full()
.bg(theme.tokens.background)
// Header
.child(
VStack::new()
.w_full()
.p(px(24.0))
.bg(theme.tokens.card)
.border_b_1()
.border_color(theme.tokens.border)
.child(
div()
.text_size(px(28.0))
.font_weight(FontWeight::BOLD)
.text_color(theme.tokens.foreground)
.child("Industry-Standard Input Validation Demo")
)
.child(
div()
.text_size(px(14.0))
.text_color(theme.tokens.muted_foreground)
.child("Comprehensive validation with real-time feedback, masking, and accessibility")
)
)
// Content
.child(
div()
.flex_1()
.w_full()
.overflow_hidden()
.child(
scrollable_vertical(
div()
.flex()
.flex_col()
.w_full()
.p(px(32.0))
.gap(px(24.0))
// Registration Form Section
.child(
VStack::new()
.w_full()
.max_w(px(600.0))
.gap(px(20.0))
.p(px(24.0))
.bg(theme.tokens.card)
.border_1()
.border_color(theme.tokens.border)
.rounded(theme.tokens.radius_lg)
.child(
div()
.text_size(px(20.0))
.font_weight(FontWeight::SEMIBOLD)
.text_color(theme.tokens.foreground)
.child("User Registration Form")
)
// Username field
.child(
VStack::new()
.w_full()
.gap(px(4.0))
.child(
div()
.text_size(px(14.0))
.font_weight(FontWeight::MEDIUM)
.text_color(theme.tokens.foreground)
.child("Username *")
)
.child(
Input::new(&self.username_input)
.variant(InputVariant::Outline)
.helper_text("3-20 characters, letters and numbers only")
.show_character_count(true)
.aria_label("Username")
.on_change({
let entity = cx.entity();
move |_value, cx| {
entity.update(cx, |app, cx| {
app.validate_form(cx);
});
}
})
)
)
// Email field
.child(
VStack::new()
.w_full()
.gap(px(4.0))
.child(
div()
.text_size(px(14.0))
.font_weight(FontWeight::MEDIUM)
.text_color(theme.tokens.foreground)
.child("Email Address *")
)
.child(
Input::new(&self.email_input)
.input_type(InputType::Email)
.variant(InputVariant::Outline)
.required(true)
.helper_text("We'll never share your email")
.aria_label("Email address")
.autocomplete("email")
.on_blur({
let entity = cx.entity();
move |_value, cx| {
entity.update(cx, |app, cx| {
app.validate_form(cx);
});
}
})
)
)
// Phone field
.child(
VStack::new()
.w_full()
.gap(px(4.0))
.child(
div()
.text_size(px(14.0))
.font_weight(FontWeight::MEDIUM)
.text_color(theme.tokens.foreground)
.child("Phone Number *")
)
.child(
Input::new(&self.phone_input)
.input_type(InputType::Tel)
.variant(InputVariant::Outline)
.required(true)
.helper_text("US format: (555) 555-5555")
.aria_label("Phone number")
.autocomplete("tel")
.on_change({
let entity = cx.entity();
move |_value, cx| {
entity.update(cx, |app, cx| {
app.validate_form(cx);
});
}
})
)
)
// Password field
.child(
VStack::new()
.w_full()
.gap(px(4.0))
.child(
div()
.text_size(px(14.0))
.font_weight(FontWeight::MEDIUM)
.text_color(theme.tokens.foreground)
.child("Password *")
)
.child(
Input::new(&self.password_input)
.input_type(InputType::Password)
.password(true)
.variant(InputVariant::Outline)
.required(true)
.min_length(8)
.helper_text("At least 8 characters")
.aria_label("Password")
.autocomplete("new-password")
.on_change({
let entity = cx.entity();
move |_value, cx| {
entity.update(cx, |app, cx| {
app.validate_form(cx);
cx.notify();
});
}
})
)
)
// Confirm Password field
.child(
VStack::new()
.w_full()
.gap(px(4.0))
.child(
div()
.text_size(px(14.0))
.font_weight(FontWeight::MEDIUM)
.text_color(theme.tokens.foreground)
.child("Confirm Password *")
)
.child(
Input::new(&self.confirm_password_input)
.input_type(InputType::Password)
.password(true)
.variant(InputVariant::Outline)
.required(true)
.aria_label("Confirm password")
.autocomplete("new-password")
.on_change({
let entity = cx.entity();
move |_value, cx| {
entity.update(cx, |app, cx| {
app.validate_form(cx);
cx.notify();
});
}
})
.on_blur({
let entity = cx.entity();
move |_value, cx| {
entity.update(cx, |app, cx| {
app.validate_form(cx);
cx.notify();
});
}
})
)
)
// Bio field
.child(
VStack::new()
.w_full()
.gap(px(4.0))
.child(
div()
.text_size(px(14.0))
.font_weight(FontWeight::MEDIUM)
.text_color(theme.tokens.foreground)
.child("Bio (optional)")
)
.child(
Input::new(&self.bio_input)
.variant(InputVariant::Outline)
.size(InputSize::Lg)
.max_length(200)
.show_character_count(true)
.helper_text("Brief description about yourself")
.aria_label("Biography")
)
)
// Submit button
.child(
div()
.w_full()
.child(
Button::new("submit-registration-btn", "Submit Registration")
.variant(ButtonVariant::Default)
.size(ButtonSize::Lg)
.on_click({
let entity = cx.entity();
move |_event, _window, cx| {
entity.update(cx, |app, cx| {
app.submit_form(cx);
});
}
})
)
)
.when(self.form_submitted, |v| {
v.child(
div()
.w_full()
.p(px(12.0))
.bg(theme.tokens.primary.opacity(0.1))
.border_1()
.border_color(theme.tokens.primary)
.rounded(theme.tokens.radius_md)
.child(
div()
.text_color(theme.tokens.primary)
.text_size(px(14.0))
.child("✓ Form submitted successfully!")
)
)
})
)
// Other Input Types Section
.child(
VStack::new()
.w_full()
.max_w(px(600.0))
.gap(px(20.0))
.p(px(24.0))
.bg(theme.tokens.card)
.border_1()
.border_color(theme.tokens.border)
.rounded(theme.tokens.radius_lg)
.child(
div()
.text_size(px(20.0))
.font_weight(FontWeight::SEMIBOLD)
.text_color(theme.tokens.foreground)
.child("Specialized Input Types")
)
// Credit card
.child(
VStack::new()
.w_full()
.gap(px(4.0))
.child(
div()
.text_size(px(14.0))
.font_weight(FontWeight::MEDIUM)
.text_color(theme.tokens.foreground)
.child("Credit Card Number")
)
.child(
Input::new(&self.credit_card_input)
.input_type(InputType::CreditCard)
.variant(InputVariant::Outline)
.helper_text("Test with: 4111 1111 1111 1111")
.aria_label("Credit card number")
.autocomplete("cc-number")
)
)
// URL
.child(
VStack::new()
.w_full()
.gap(px(4.0))
.child(
div()
.text_size(px(14.0))
.font_weight(FontWeight::MEDIUM)
.text_color(theme.tokens.foreground)
.child("Website URL")
)
.child(
Input::new(&self.url_input)
.input_type(InputType::Url)
.variant(InputVariant::Outline)
.helper_text("Must start with http:// or https://")
.aria_label("Website URL")
.autocomplete("url")
)
)
// Number
.child(
VStack::new()
.w_full()
.gap(px(4.0))
.child(
div()
.text_size(px(14.0))
.font_weight(FontWeight::MEDIUM)
.text_color(theme.tokens.foreground)
.child("Quantity (1-100)")
)
.child(
Input::new(&self.number_input)
.input_type(InputType::Number)
.variant(InputVariant::Outline)
.helper_text("Enter a number between 1 and 100")
.aria_label("Quantity")
)
)
// Date
.child(
VStack::new()
.w_full()
.gap(px(4.0))
.child(
div()
.text_size(px(14.0))
.font_weight(FontWeight::MEDIUM)
.text_color(theme.tokens.foreground)
.child("Date of Birth")
)
.child(
Input::new(&self.date_input)
.input_type(InputType::Date)
.variant(InputVariant::Outline)
.helper_text("Format: MM/DD/YYYY")
.aria_label("Date of birth")
.autocomplete("bday")
)
)
// Time
.child(
VStack::new()
.w_full()
.gap(px(4.0))
.child(
div()
.text_size(px(14.0))
.font_weight(FontWeight::MEDIUM)
.text_color(theme.tokens.foreground)
.child("Appointment Time")
)
.child(
Input::new(&self.time_input)
.input_type(InputType::Time)
.variant(InputVariant::Outline)
.helper_text("24-hour format: HH:MM")
.aria_label("Appointment time")
)
)
)
// Features showcase
.child(
VStack::new()
.w_full()
.max_w(px(600.0))
.gap(px(12.0))
.p(px(20.0))
.bg(theme.tokens.muted.opacity(0.3))
.border_1()
.border_color(theme.tokens.border)
.rounded(theme.tokens.radius_lg)
.child(
div()
.text_size(px(16.0))
.font_weight(FontWeight::SEMIBOLD)
.text_color(theme.tokens.foreground)
.child("✨ Features Demonstrated")
)
.child(
div()
.text_size(px(13.0))
.text_color(theme.tokens.muted_foreground)
.line_height(relative(1.6))
.child("• Real-time validation with error messages\n• Character filtering (try typing letters in number fields)\n• Input masking (phone, credit card, date formatting)\n• Min/max length enforcement\n• Character counter\n• Helper text and error states\n• Password matching validation\n• Accessibility with ARIA labels\n• Autocomplete attributes\n• Click on filled input places cursor at end\n• Proper focus management\n• Industry-standard UX patterns")
)
)
),
),
)
}
}
fn main() {
Application::new()
.with_assets(Assets {
base: PathBuf::from(env!("CARGO_MANIFEST_DIR")),
})
.run(move |cx| {
// Install dark theme
install_theme(cx, Theme::dark());
// Initialize input system
kael_ui::init(cx);
kael_ui::set_icon_base_path("assets/icons");
cx.open_window(
WindowOptions {
window_bounds: Some(WindowBounds::Windowed(Bounds::centered(
None,
size(px(900.0), px(800.0)),
cx,
))),
titlebar: Some(TitlebarOptions {
title: Some("Input Validation Demo - Industry Standard".into()),
..Default::default()
}),
..Default::default()
},
|_window, cx| cx.new(|cx| ValidationDemoApp::new(cx)),
)
.unwrap();
});
}