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
use ;
use ;
use component_doc;
use inject_style;
use field_styles;
use FieldValidationMessage;
use crateLabel;
/// Field wraps a single form control with a label, required indicator, and validation
/// message area.
///
/// Child controls like [`Input`](crate::Input) and [`Select`](crate::Select) inherit `id` and `name` through context, while validation rules live on the control — not on Field — so Leptos signals drive error state reactively. Compose Field inside Leptos ActionForm or router Form roots; Orbital does not ship a separate Form component.
///
/// ## Leptos form architecture (no Orbital `Form` wrapper)
///
/// Pick a **Leptos form root**, then compose Orbital inside it:
///
/// | Root | When |
/// |---|---|
/// | `leptos_router::components::Form` | GET navigation — filters/search in URL (`?q=`) |
/// | `leptos::form::ActionForm` | Server action POST with progressive enhancement |
/// | `<form on:submit>` + `Action::dispatch` | SPA controlled submit from `Store` |
///
/// Typical stack: **form root** → [`FieldContextProvider`] → [`Field`] → [`Input`] / [`Select`] → [`Button`]. Form values live in a Leptos [`Store`](https://book.leptos.dev/15_global_state.html) (`store.field()`) or standalone `RwSignal`s. Native Orbital inputs use [`FormBind`](orbital_base_components::FormBind) (`RwSignal` \| store field \| plain initial value for previews).
///
/// # When to use
///
/// - Any labeled control: [`Input`](crate::Input), [`Select`](crate::Select), checkbox groups
/// - Forms where validation errors should appear below the control
/// - Horizontal label layouts in compact settings rows
///
/// # Usage
///
/// 1. Wrap each control in `<Field label="…">` (set `name` when posting native forms).
/// 2. Place the control as the child — e.g. [`Input`](crate::Input) or [`Select`](crate::Select).
/// 3. Add `rules` on the child control ([`InputRule`](crate::InputRule), [`SelectRule`](crate::SelectRule), …) to drive validation messages — for example `Input bind=InputBind { value: signal.into(), rules: vec![InputRule::required(required)], ..Default::default() }`.
/// 4. Set `required=true` when the label should show a required indicator.
/// 5. For multi-control forms, use [`FieldContextProvider`](crate::FieldContextProvider) at the form root.
///
/// # Best Practices
///
/// ## Do's
///
/// * Set `required` when the control is mandatory * Use vertical orientation for stacked forms (default) * Put validation `rules` on the inner control, not on `Field` itself * Share `name` between `Field` and the control when submitting HTML forms
///
/// ## Don'ts
///
/// * Do not nest `Field` inside `Field` * Do not skip `Field` when a visible label or validation message is required * Do not put `data-testid` on `Field` — wrap with a native element
///
/// # Examples
///
/// ## Labeled input
/// Vertical layout (default): the label stacks above the control and validation messages appear below the input.
/// <!-- preview -->
/// ```rust
/// use crate::{Field, Input, InputAppearance};
/// let value = RwSignal::new(String::new());
/// view! {
/// <div data-testid="field-preview">
/// <Field label="Display name">
/// <Input bind=value appearance=InputAppearance::with_placeholder("Jane Doe") />
/// </Field>
/// </div>
/// }
/// ```
///
/// ## Required field
/// When `required=true`, the label shows a required indicator. Add matching `InputRule::required` on the child so empty values produce validation text below the control.
/// <!-- preview -->
/// ```rust
/// use crate::{Field, Input, InputAppearance, InputBind, InputRule};
/// let value = RwSignal::new(String::new());
/// let required = Signal::from(true);
/// view! {
/// <div data-testid="field-required">
/// <Field label="Email" name="email" required=true>
/// <Input
/// bind=InputBind {
/// value: value.into(),
/// rules: vec![InputRule::required(required)],
/// ..Default::default()
/// }
/// appearance=InputAppearance::email("you@example.com")
/// />
/// </Field>
/// </div>
/// }
/// ```
///
/// ## Horizontal layout
/// `orientation=Horizontal` places the label beside the control. Validation and hint text still render below the input; label width is fixed (~33%) so stacked horizontal fields align.
/// <!-- preview -->
/// ```rust
/// use crate::{Field, FieldOrientation, Input, InputAppearance};
/// let value = RwSignal::new(String::new());
/// view! {
/// <div data-testid="field-horizontal">
/// <Field label="Code" orientation=FieldOrientation::Horizontal>
/// <Input bind=value appearance=InputAppearance::default() />
/// </Field>
/// </div>
/// }
/// ```
///
/// ## Select with validation
/// Field can wrap any control — here a Select with `SelectRule::required`. The field shows the label, required marker, and validation message from the child rules.
/// <!-- preview -->
/// ```rust
/// use crate::{Field, Select, SelectBind, SelectRule};
/// let value = RwSignal::new(String::new());
/// let required = Signal::from(true);
/// view! {
/// <div data-testid="field-select">
/// <Field label="Color" name="color" required=true>
/// <Select
/// bind=SelectBind {
/// value: value.into(),
/// rules: vec![SelectRule::required(required)],
/// ..Default::default()
/// }
/// >
/// <option value="">"Choose a color"</option>
/// <option value="red">"Red"</option>
/// <option value="green">"Green"</option>
/// <option value="blue">"Blue"</option>
/// </Select>
/// </Field>
/// </div>
/// }
/// ```
///
/// ## Store-backed ActionForm
/// Server action submit with progressive enhancement — values from a `Store`, validation via [`FieldContextProvider`].
/// <!-- code-only -->
/// ```rust,ignore
/// use leptos::form::ActionForm;
/// use leptos::prelude::*;
/// use reactive_stores::Store;
/// use crate::{Button, Field, FieldContextProvider, Input};
///
/// #[derive(Store, Clone)]
/// struct ProfileForm {
/// email: String,
/// }
///
/// #[server]
/// async fn save_profile(form: ProfileForm) -> Result<(), ServerFnError> {
/// Ok(())
/// }
///
/// let save = ServerAction::<SaveProfile>::new();
/// let form = Store::new(ProfileForm { email: String::new() });
/// view! {
/// <FieldContextProvider>
/// <ActionForm action=save>
/// <Field label="Email" name="email" required=true>
/// <Input value=form.email() />
/// </Field>
/// <Button type="submit" loading=save.pending()>"Save"</Button>
/// </ActionForm>
/// </FieldContextProvider>
/// }
/// ```
///
/// ## Router Form search
/// URL-synced filters via [`leptos_router::Form`](https://book.leptos.dev/router/20_form.html) — set `name=` on inputs.
/// <!-- code-only -->
/// ```rust,ignore
/// use leptos_router::{components::Form, hooks::use_query_map};
/// use leptos::prelude::*;
/// use crate::{Field, Input};
///
/// let query = use_query_map();
/// let search = move || query.read().get("q").unwrap_or_default();
/// view! {
/// <Form method="GET" action="">
/// <Field label="Search" name="q">
/// <Input value=search placeholder="Filter…" />
/// </Field>
/// </Form>
/// }
/// ```
///
/// ## SPA submit from Store
/// Controlled submit without a router form — dispatch an `Action` from `<form on:submit>`.
/// <!-- code-only -->
/// ```rust,ignore
/// use leptos::prelude::*;
/// use reactive_stores::Store;
/// use crate::{Button, Field, FieldContextProvider, Input};
///
/// #[derive(Store, Clone)]
/// struct Draft { title: String }
///
/// let draft = Store::new(Draft { title: String::new() });
/// let save = Action::new(/* … */);
/// view! {
/// <FieldContextProvider>
/// <form on:submit=move |ev| { ev.prevent_default(); save.dispatch(draft.get()); }>
/// <Field label="Title" name="title">
/// <Input value=draft.title() />
/// </Field>
/// <Button type="submit">"Save"</Button>
/// </form>
/// </FieldContextProvider>
/// }
/// ```