1use arc_tokens::{ActiveArcTheme as _, ControlHeights, Radii, Spacing, Typography};
14use gpui_kit::{
15 App, ElementId, Entity, Focusable as _, IntoElement, ParentElement, RenderOnce, SharedString,
16 Styled, Window,
17 base::StyledExt as _,
18 component::input::{Input as KitInput, InputState},
19 div,
20 prelude::*,
21};
22
23use super::field_message::render_field_message;
24
25#[derive(IntoElement)]
27pub struct Input {
28 id: ElementId,
29 label: SharedString,
30 state: Entity<InputState>,
31 description: Option<SharedString>,
32 error: Option<SharedString>,
33 disabled: bool,
34}
35
36impl Input {
37 pub fn new(
38 id: impl Into<ElementId>,
39 label: impl Into<SharedString>,
40 state: &Entity<InputState>,
41 ) -> Self {
42 Self {
43 id: id.into(),
44 label: label.into(),
45 state: state.clone(),
46 description: None,
47 error: None,
48 disabled: false,
49 }
50 }
51
52 pub fn description(mut self, description: impl Into<SharedString>) -> Self {
53 self.description = Some(description.into());
54 self
55 }
56
57 pub fn error(mut self, error: impl Into<SharedString>) -> Self {
58 self.error = Some(error.into());
59 self
60 }
61
62 pub fn error_opt(mut self, error: Option<SharedString>) -> Self {
63 self.error = error;
64 self
65 }
66
67 pub fn disabled(mut self, disabled: bool) -> Self {
68 self.disabled = disabled;
69 self
70 }
71}
72
73impl RenderOnce for Input {
74 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
75 let colors = cx.arc_theme().colors;
76 let has_error = self.error.as_ref().is_some_and(|e| !e.is_empty());
77 let is_focused = self.state.read(cx).focus_handle(cx).is_focused(window);
78
79 let border_color = if has_error {
80 colors.danger
81 } else if is_focused {
82 colors.foreground
83 } else {
84 colors.border_strong
85 };
86 let hover_border = colors.foreground;
87
88 let shell = div()
89 .w_full()
90 .min_h(ControlHeights::MD)
91 .px(Spacing::S3)
92 .flex()
93 .items_center()
94 .rounded(Radii::CONTROL)
95 .border_1()
96 .border_color(border_color)
97 .bg(if self.disabled {
98 colors.surface_muted
99 } else {
100 colors.surface
101 })
102 .when(self.disabled, |this| this.opacity(0.5))
103 .when(!self.disabled && !has_error && !is_focused, |this| {
104 this.hover(move |s| s.border_color(hover_border))
105 })
106 .text_size(Typography::TEXT_SM)
107 .text_color(colors.foreground)
108 .child(
109 KitInput::new(&self.state)
110 .appearance(false)
111 .disabled(self.disabled)
112 .w_full(),
113 );
114
115 let desc_id = ElementId::NamedChild(self.id.clone().into(), "description".into());
116 let err_id = ElementId::NamedChild(self.id.clone().into(), "error".into());
117
118 div()
119 .id(self.id)
120 .w_full()
121 .flex()
122 .flex_col()
123 .child(
124 div()
125 .mb(Spacing::S2)
126 .text_size(Typography::TEXT_SM)
127 .font_medium()
128 .text_color(colors.foreground)
129 .child(self.label),
130 )
131 .child(shell)
132 .child(render_field_message(
133 desc_id,
134 self.description,
135 false,
136 window,
137 cx,
138 ))
139 .child(render_field_message(err_id, self.error, true, window, cx))
140 }
141}
142
143#[cfg(test)]
144mod tests {
145 use arc_tokens::{Accent, ThemeMode, install_arc_theme};
146 use gpui_kit::{Context, Render, TestAppContext, div, px, test::TestWindowExt as _};
147
148 use super::*;
149
150 struct InputHost {
151 state: Entity<InputState>,
152 error: Option<SharedString>,
153 }
154
155 impl Render for InputHost {
156 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
157 div().w(px(360.0)).p(Spacing::S4).child(
158 Input::new("email", "Work email", &self.state)
159 .description("We only send release summaries.")
160 .error_opt(self.error.clone()),
161 )
162 }
163 }
164
165 #[gpui_kit::test]
166 fn input_renders_updates_value_and_toggles_error_message(cx: &mut TestAppContext) {
167 cx.update(|cx| {
168 gpui_kit::init(cx);
169 install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
170 });
171
172 let (view, cx) = cx.add_window_view(|window, cx| {
173 let state = cx.new(|cx| {
174 InputState::new(window, cx)
175 .placeholder("name@company.com")
176 .default_value("alex@arc.dev")
177 });
178 InputHost { state, error: None }
179 });
180
181 cx.update(|window, cx| {
182 window.render_frame(cx);
183 let state = view.read(cx).state.clone();
184 assert_eq!(state.read(cx).value(), "alex@arc.dev");
185
186 state.update(cx, |s, cx| s.set_value("invalid", window, cx));
187 view.update(cx, |v, cx| {
188 v.error = Some("Enter a valid email address.".into());
189 cx.notify();
190 });
191 window.render_frame(cx);
192 assert_eq!(state.read(cx).value(), "invalid");
193 });
194 }
195}