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
//! Field component for wrapping form inputs with labels, descriptions, and error states.
use crate::theme::{ActiveTheme, Themeable};
use crate::traits::disableable::Disableable;
use crate::traits::labelable::Labelable;
use gpui::{
div, prelude::FluentBuilder, rems, AnyElement, App, IntoElement, ParentElement, RenderOnce,
SharedString, Styled, Window,
};
/// Creates a new Field builder.
pub fn field() -> Field {
Field::new()
}
/// Label position relative to the input.
#[derive(Default, Clone, Copy, PartialEq, Eq)]
pub enum LabelPosition {
/// Label is displayed above the input (default).
#[default]
Above,
/// Label is displayed beside the input (horizontal layout).
Beside,
}
/// A field component for wrapping form inputs with labels, descriptions, and error states.
///
/// # Example
///
/// ```ignore
/// field()
/// .label("Username")
/// .description("Enter your username")
/// .required(true)
/// .child(input("username"))
/// ```
#[derive(IntoElement)]
pub struct Field {
label: Option<SharedString>,
description: Option<SharedString>,
error: Option<SharedString>,
required: bool,
label_position: LabelPosition,
child: Option<AnyElement>,
disabled: bool,
}
impl Field {
/// Create a new Field.
pub fn new() -> Self {
Field {
label: None,
description: None,
error: None,
required: false,
label_position: LabelPosition::default(),
child: None,
disabled: false,
}
}
/// Set the description/help text for this field.
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.description = Some(description.into());
self
}
/// Set an error message for this field.
/// When set, the field will display in an error state.
pub fn error(mut self, error: impl Into<SharedString>) -> Self {
self.error = Some(error.into());
self
}
/// Mark this field as required.
/// Displays a required indicator next to the label.
pub fn required(mut self, required: bool) -> Self {
self.required = required;
self
}
/// Set the label position (above or beside the input).
pub fn label_position(mut self, position: LabelPosition) -> Self {
self.label_position = position;
self
}
/// Set the child element (typically a form input).
pub fn child(mut self, child: impl IntoElement) -> Self {
self.child = Some(child.into_any_element());
self
}
}
impl Default for Field {
fn default() -> Self {
Self::new()
}
}
impl Labelable for Field {
fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
}
impl Disableable for Field {
fn is_disabled(&self) -> bool {
self.disabled
}
fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
impl RenderOnce for Field {
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.theme();
let has_error = self.error.is_some();
let disabled = self.disabled;
let label_color = if disabled {
theme.fg_disabled()
} else if has_error {
theme.danger()
} else {
theme.fg()
};
let label_element = self.label.map(|label_text| {
div()
.flex()
.gap(rems(0.25))
.child(
div()
.text_sm()
.font_weight(gpui::FontWeight::MEDIUM)
.text_color(label_color)
.child(label_text),
)
.when(self.required && !disabled, |this| {
this.child(
div()
.text_sm()
.text_color(theme.danger())
.child("*"),
)
})
});
let description_element = self.description.map(|desc| {
div()
.text_xs()
.text_color(if disabled {
theme.fg_disabled()
} else {
theme.fg_muted()
})
.child(desc)
});
let error_element = self.error.map(|err| {
div()
.text_xs()
.text_color(theme.danger())
.child(err)
});
match self.label_position {
LabelPosition::Above => {
// Vertical layout: label above input
div()
.flex()
.flex_col()
.gap(rems(0.375))
.when(disabled, |el| el.cursor_not_allowed())
.when_some(label_element, |container, label| {
container.child(label)
})
.when_some(description_element, |container, desc| {
container.child(desc)
})
.when_some(self.child, |container, child| {
container.child(child)
})
.when_some(error_element, |container, err| {
container.child(err)
})
}
LabelPosition::Beside => {
// Horizontal layout: label beside input
div()
.flex()
.flex_col()
.gap(rems(0.375))
.when(disabled, |el| el.cursor_not_allowed())
.child(
div()
.flex()
.items_start()
.gap(rems(0.75))
.when_some(label_element, |container, label| {
container.child(
div()
.flex()
.flex_col()
.gap(rems(0.25))
.pt(rems(0.5)) // Align with input
.min_w(rems(8.0))
.child(label)
.when_some(description_element, |this, desc| {
this.child(desc)
}),
)
})
.when_some(self.child, |container, child| {
container.child(div().flex_1().child(child))
}),
)
.when_some(error_element, |container, err| {
container.child(
div()
.pl(rems(8.75)) // Align with input (label width + gap)
.child(err),
)
})
}
}
}
}