dioxus-element-plug 0.1.4

Element UI components for Dioxus applications with pure Rust styling system
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
use dioxus::prelude::*;
use crate::components::input::{Input, InputProps, InputType};

// Form CSS class constants
pub const FORM: &str = "el-form";
pub const FORM_HORIZONTAL: &str = "el-form--horizontal";
pub const FORM_INLINE: &str = "el-form--inline";
pub const FORM_ITEM: &str = "el-form-item";
pub const FORM_ITEM_LABEL: &str = "el-form-item__label";
pub const FORM_ITEM_CONTENT: &str = "el-form-item__content";
pub const FORM_ITEM_ERROR: &str = "el-form-item__error";
pub const FORM_ITEM_HELP: &str = "el-form-item__help";
pub const FORM_ITEM_REQUIRED: &str = "is-required";
pub const FORM_ITEM_ERROR_STATE: &str = "is-error";

// Form controls CSS class constants
pub const SELECT: &str = "el-select";
pub const CHECKBOX: &str = "el-checkbox";
pub const RADIO: &str = "el-radio";
pub const SLIDER: &str = "el-slider";
pub const IS_DISABLED: &str = "is-disabled";

/// Form item validation status
#[derive(Clone, PartialEq)]
pub enum FormItemStatus {
    Valid,
    Invalid,
    Validating,
    None,
}

impl FormItemStatus {
    pub fn as_class(&self) -> &'static str {
        match self {
            FormItemStatus::Valid => "is-success",
            FormItemStatus::Invalid => "is-error",
            FormItemStatus::Validating => "is-validating",
            FormItemStatus::None => "",
        }
    }
}

/// Form item props
#[derive(Props, Clone, PartialEq)]
pub struct FormItemProps {
    /// Form item label
    pub label: String,

    /// Form item content
    pub children: Element,

    /// Whether the form item is required
    #[props(default = false)]
    pub required: bool,

    /// Form item validation status
    #[props(default = FormItemStatus::None)]
    pub status: FormItemStatus,

    /// Form item error message
    #[props(default)]
    pub error_message: Option<String>,

    /// Form item help text
    #[props(default)]
    pub help_text: Option<String>,

    /// Label width in pixels
    #[props(default = 120)]
    pub label_width: u32,

    /// Additional CSS classes for the form item
    #[props(default)]
    pub class: Option<String>,

    /// Inline styles for the form item
    #[props(default)]
    pub style: Option<String>,
}

/// A form item component with validation support
///
/// This component provides a labeled container for form controls with built-in
/// validation state display and error messaging.
///
/// ## Example
///
/// ```rust,ignore
/// use dioxus_element_plug::components::form::{FormItem, FormItemStatus};
/// use dioxus_element_plug::components::input::{Input, InputType};
///
/// rsx! {
///     FormItem {
///         label: "Email",
///         required: true,
///         status: FormItemStatus::Invalid,
///         error_message: Some("Please enter a valid email address".to_string()),
///         Input {
///             input_type: InputType::Email,
///             placeholder: "Enter your email",
///         }
///     }
/// }
/// ```
#[component]
pub fn FormItem(props: FormItemProps) -> Element {
    let mut class_names = vec![FORM_ITEM.to_string()];
    
    if !props.status.as_class().is_empty() {
        class_names.push(props.status.as_class().to_string());
    }
    
    if props.required {
        class_names.push("is-required".to_string());
    }
    
    if let Some(ref custom_class) = props.class {
        class_names.push(custom_class.to_string());
    }
    
    let class_string = class_names.join(" ");
    let style_string = props.style.as_ref().cloned().unwrap_or_default();
    
    let label_style = format!("width: {}px; display: inline-block;", props.label_width);
    
    rsx! {
        div {
            class: "{class_string}",
            style: "{style_string}",
            
            if !props.label.is_empty() {
                label {
                    class: "el-form-item__label",
                    style: "{label_style}",
                    "{props.label}"
                }
            }
            
            div {
                class: "el-form-item__content",
                
                {props.children}
                
                if let Some(ref error_msg) = props.error_message {
                    div {
                        class: "el-form-item__error",
                        "{error_msg}"
                    }
                }
                
                if let Some(ref help_text) = props.help_text {
                    div {
                        class: "el-form-item__help",
                        "{help_text}"
                    }
                }
            }
        }
    }
}

/// Form props
#[derive(Props, Clone, PartialEq)]
pub struct FormProps {
    /// Form content
    pub children: Element,

    /// Form layout direction
    #[props(default = "horizontal".to_string())]
    pub layout: String,

    /// Label position
    #[props(default = "right".to_string())]
    pub label_position: String,

    /// Label width in pixels
    #[props(default = 120)]
    pub label_width: u32,

    /// Form size
    #[props(default)]
    pub size: Option<String>,

    /// Whether to show required asterisk
    #[props(default = true)]
    pub show_asterisk: bool,

    /// Additional CSS classes
    #[props(default)]
    pub class: Option<String>,

    /// Inline styles
    #[props(default)]
    pub style: Option<String>,

    /// Form submit handler
    #[props(default)]
    pub on_submit: Option<EventHandler<FormEvent>>,
}

/// A form component wrapper
///
/// This component provides a styled container for form items with consistent
/// layout and validation state management.
///
/// ## Example
///
/// ```rust,ignore
/// use dioxus_element_plug::components::form::{Form, FormItem, FormItemStatus};
/// use dioxus_element_plug::components::input::{Input, InputType};
///
/// rsx! {
///     Form {
///         layout: "horizontal".to_string(),
///         label_position: "right".to_string(),
///         on_submit: move |_| println!("Form submitted"),
///         
///         FormItem {
///             label: "Name",
///             Input {
///                 input_type: InputType::Text,
///                 placeholder: "Enter your name",
///             }
///         }
///         
///         FormItem {
///             label: "Email",
///             Input {
///                 input_type: InputType::Email,
///                 placeholder: "Enter your email",
///             }
///         }
///     }
/// }
/// ```
#[component]
pub fn Form(props: FormProps) -> Element {
    let mut class_names = vec![FORM.to_string()];
    
    class_names.push(format!("el-form--{}", props.layout));
    
    if let Some(ref size) = props.size {
        class_names.push(format!("el-form--{}", size));
    }
    
    if props.show_asterisk {
        class_names.push("el-form--show-asterisk".to_string());
    }
    
    if let Some(ref custom_class) = props.class {
        class_names.push(custom_class.to_string());
    }
    
    let class_string = class_names.join(" ");
    let style_string = props.style.as_ref().cloned().unwrap_or_default();
    
    let form_style = format!(
        "--el-form-label-width: {}px; {}", 
        props.label_width,
        style_string
    );
    
    rsx! {
        form {
            class: "{class_string}",
            style: "{form_style}",
            onsubmit: move |event| {
                event.prevent_default();
                if let Some(handler) = props.on_submit {
                    handler.call(event);
                }
            },
            {props.children}
        }
    }
}

/// Select option for dropdown
#[derive(Clone, PartialEq)]
pub struct SelectOption {
    pub value: String,
    pub label: String,
    pub disabled: bool,
}

/// Select input component
#[derive(Props, Clone, PartialEq)]
pub struct SelectProps {
    /// Selected value
    #[props(default)]
    pub value: Option<String>,

    /// Select options
    pub options: Vec<SelectOption>,

    /// Placeholder text
    #[props(default)]
    pub placeholder: Option<String>,

    /// Whether the select is disabled
    #[props(default = false)]
    pub disabled: bool,

    /// Whether to allow clearing selection
    #[props(default = false)]
    pub clearable: bool,

    /// Change event handler
    #[props(default)]
    pub on_change: Option<EventHandler<FormEvent>>,

    /// Additional CSS classes
    #[props(default)]
    pub class: Option<String>,

    /// Inline styles
    #[props(default)]
    pub style: Option<String>,
}

/// A select dropdown component
///
/// This component provides a styled dropdown select with option support
/// and validation capabilities.
#[component]
pub fn Select(props: SelectProps) -> Element {
    let mut class_names = vec![SELECT.to_string()];
    
    if props.disabled {
        class_names.push(IS_DISABLED.to_string());
    }
    
    if props.clearable {
        class_names.push("is-clearable".to_string());
    }
    
    if let Some(ref custom_class) = props.class {
        class_names.push(custom_class.to_string());
    }
    
    let class_string = class_names.join(" ");
    let style_string = props.style.as_ref().cloned().unwrap_or_default();
    
    let selected_option = props.options.iter()
        .find(|opt| Some(opt.value.clone()) == props.value)
        .map(|opt| opt.label.clone())
        .unwrap_or_default();
    
    rsx! {
        div {
            class: "{class_string}",
            style: "{style_string}",
            
            div {
                class: "el-select__wrapper",
                
                div {
                    class: "el-select__selection",
                    
                    if selected_option.is_empty() {
                        if let Some(ref placeholder) = props.placeholder {
                            span {
                                class: "el-select__placeholder",
                                "{placeholder}"
                            }
                        }
                    } else {
                        span {
                            class: "el-select__selected-item",
                            "{selected_option}"
                        }
                    }
                    
                    if props.clearable && !selected_option.is_empty() {
                        span {
                            class: "el-select__clear",
                            i {
                                class: "el-icon-close"
                            }
                        }
                    }
                }
                
                i {
                    class: "el-select__caret el-icon-arrow-down"
                }
            }
            
            select {
                class: "el-select__input",
                disabled: props.disabled,
                value: props.value.unwrap_or_default(),
                onchange: move |event| {
                    if let Some(handler) = props.on_change {
                        handler.call(event);
                    }
                },
                
                for option in props.options.iter() {
                    option {
                        value: "{option.value}",
                        disabled: option.disabled,
                        "{option.label}"
                    }
                }
            }
        }
    }
}

/// Checkbox component
#[derive(Props, Clone, PartialEq)]
pub struct CheckboxProps {
    /// Checkbox label
    #[props(default)]
    pub label: Option<String>,

    /// Whether the checkbox is checked
    #[props(default = false)]
    pub checked: bool,

    /// Whether the checkbox is disabled
    #[props(default = false)]
    pub disabled: bool,

    /// Change event handler
    #[props(default)]
    pub on_change: Option<EventHandler<FormEvent>>,

    /// Additional CSS classes
    #[props(default)]
    pub class: Option<String>,

    /// Inline styles
    #[props(default)]
    pub style: Option<String>,
}

/// A checkbox component
///
/// This component provides a styled checkbox with label support.
#[component]
pub fn Checkbox(props: CheckboxProps) -> Element {
    let mut class_names = vec![CHECKBOX.to_string()];
    
    if props.checked {
        class_names.push("is-checked".to_string());
    }
    
    if props.disabled {
        class_names.push(IS_DISABLED.to_string());
    }
    
    if let Some(ref custom_class) = props.class {
        class_names.push(custom_class.to_string());
    }
    
    let class_string = class_names.join(" ");
    let style_string = props.style.as_ref().cloned().unwrap_or_default();
    
    rsx! {
        label {
            class: "{class_string}",
            style: "{style_string}",
            
            span {
                class: "el-checkbox__input",
                
                input {
                    r#type: "checkbox",
                    checked: props.checked,
                    disabled: props.disabled,
                    onchange: move |event| {
                        if let Some(handler) = props.on_change {
                            handler.call(event);
                        }
                    },
                }
                
                span {
                    class: "el-checkbox__inner"
                }
            }
            
            if let Some(ref label_text) = props.label {
                span {
                    class: "el-checkbox__label",
                    "{label_text}"
                }
            }
        }
    }
}

/// Radio button component
#[derive(Props, Clone, PartialEq)]
pub struct RadioProps {
    /// Radio button label
    pub label: String,

    /// Radio value
    pub value: String,

    /// Whether the radio is checked
    #[props(default = false)]
    pub checked: bool,

    /// Whether the radio is disabled
    #[props(default = false)]
    pub disabled: bool,

    /// Group name
    #[props(default)]
    pub name: Option<String>,

    /// Change event handler
    #[props(default)]
    pub on_change: Option<EventHandler<FormEvent>>,

    /// Additional CSS classes
    #[props(default)]
    pub class: Option<String>,

    /// Inline styles
    #[props(default)]
    pub style: Option<String>,
}

/// A radio button component
///
/// This component provides a styled radio button with label support.
#[component]
pub fn Radio(props: RadioProps) -> Element {
    let mut class_names = vec![RADIO.to_string()];
    
    if props.checked {
        class_names.push("is-checked".to_string());
    }
    
    if props.disabled {
        class_names.push(IS_DISABLED.to_string());
    }
    
    if let Some(ref custom_class) = props.class {
        class_names.push(custom_class.to_string());
    }
    
    let class_string = class_names.join(" ");
    let style_string = props.style.as_ref().cloned().unwrap_or_default();
    
    rsx! {
        label {
            class: "{class_string}",
            style: "{style_string}",
            
            span {
                class: "el-radio__input",
                
                input {
                    r#type: "radio",
                    name: props.name.unwrap_or_default(),
                    value: props.value,
                    checked: props.checked,
                    disabled: props.disabled,
                    onchange: move |event| {
                        if let Some(handler) = props.on_change {
                            handler.call(event);
                        }
                    },
                }
                
                span {
                    class: "el-radio__inner"
                }
            }
            
            span {
                class: "el-radio__label",
                "{props.label}"
            }
        }
    }
}

/// Slider component for range input
#[derive(Props, Clone, PartialEq)]
pub struct SliderProps {
    /// Current value
    #[props(default = 0.0)]
    pub value: f64,

    /// Minimum value
    #[props(default = 0.0)]
    pub min: f64,

    /// Maximum value
    #[props(default = 100.0)]
    pub max: f64,

    /// Step size
    #[props(default = 1.0)]
    pub step: f64,

    /// Whether the slider is disabled
    #[props(default = false)]
    pub disabled: bool,

    /// Whether to show tooltip
    #[props(default = true)]
    pub show_tooltip: bool,

    /// Change event handler
    #[props(default)]
    pub on_change: Option<EventHandler<FormEvent>>,

    /// Input event handler
    #[props(default)]
    pub on_input: Option<EventHandler<FormEvent>>,

    /// Additional CSS classes
    #[props(default)]
    pub class: Option<String>,

    /// Inline styles
    #[props(default)]
    pub style: Option<String>,
}

/// A slider component for range input
///
/// This component provides a styled range slider with tooltip support.
#[component]
pub fn Slider(props: SliderProps) -> Element {
    let mut class_names = vec![SLIDER.to_string()];
    
    if props.disabled {
        class_names.push(IS_DISABLED.to_string());
    }
    
    if props.show_tooltip {
        class_names.push("show-tooltip".to_string());
    }
    
    if let Some(ref custom_class) = props.class {
        class_names.push(custom_class.to_string());
    }
    
    let class_string = class_names.join(" ");
    let style_string = props.style.as_ref().cloned().unwrap_or_default();
    
    let percentage = if props.max > props.min {
        ((props.value - props.min) / (props.max - props.min) * 100.0).clamp(0.0, 100.0)
    } else {
        0.0
    };
    
    rsx! {
        div {
            class: "{class_string}",
            style: "{style_string}",
            
            div {
                class: "el-slider__runway",
                
                div {
                    class: "el-slider__bar",
                    style: "width: {percentage}%"
                }
                
                div {
                    class: "el-slider__button-wrapper",
                    style: "left: {percentage}%",
                    
                    div {
                        class: "el-slider__button"
                    }
                    
                    if props.show_tooltip {
                        div {
                            class: "el-tooltip__popper is-dark",
                            "{props.value as i32}"
                        }
                    }
                }
            }
            
            input {
                r#type: "range",
                min: props.min,
                max: props.max,
                step: props.step,
                value: props.value,
                disabled: props.disabled,
                onchange: move |event| {
                    if let Some(handler) = props.on_change {
                        handler.call(event);
                    }
                },
                oninput: move |event| {
                    if let Some(handler) = props.on_input {
                        handler.call(event);
                    }
                },
            }
        }
    }
}