Skip to main content

orbital_base_components/form/field/
base.rs

1use leptos::{context::Provider, prelude::*};
2
3use crate::form::field_injection::{new_field_id, FieldInjection};
4use crate::form::field_validation::FieldValidationState;
5use crate::form::types::FieldOrientation;
6
7/// Headless field shell — provides [`FieldInjection`] to child controls.
8#[component(transparent)]
9pub fn BaseField(
10    #[prop(optional, into)] class: MaybeProp<String>,
11    #[prop(optional, into)] orientation: MaybeProp<String>,
12    #[prop(optional, into)] label: MaybeProp<String>,
13    #[prop(optional, into)] name: MaybeProp<String>,
14    #[prop(optional, into)] field_orientation: Signal<FieldOrientation>,
15    #[prop(optional, into)] required: Signal<bool>,
16    children: Children,
17) -> impl IntoView {
18    let _ = (field_orientation, required);
19    let id = StoredValue::new(new_field_id());
20    let validation_state = RwSignal::new(None::<FieldValidationState>);
21
22    view! {
23        <div
24            class=move || {
25                let mut parts = Vec::new();
26                if let Some(c) = class.get() {
27                    if !c.is_empty() {
28                        parts.push(c);
29                    }
30                }
31                if let Some(c) = orientation.get() {
32                    if !c.is_empty() {
33                        parts.push(c);
34                    }
35                }
36                parts.join(" ")
37            }
38        >
39            <Provider value=FieldInjection {
40                id,
41                name,
42                label,
43                validation_state,
44            }>{children()}</Provider>
45        </div>
46    }
47}