use arc_tokens::{ActiveArcTheme as _, ControlHeights, Radii, Spacing, Typography};
use gpui_kit::{
App, ElementId, Entity, Focusable as _, IntoElement, ParentElement, RenderOnce, SharedString,
Styled, Window,
base::StyledExt as _,
component::input::{Input as KitInput, InputState},
div,
prelude::*,
};
use super::field_message::render_field_message;
#[derive(IntoElement)]
pub struct Input {
id: ElementId,
label: SharedString,
state: Entity<InputState>,
description: Option<SharedString>,
error: Option<SharedString>,
disabled: bool,
}
impl Input {
pub fn new(
id: impl Into<ElementId>,
label: impl Into<SharedString>,
state: &Entity<InputState>,
) -> Self {
Self {
id: id.into(),
label: label.into(),
state: state.clone(),
description: None,
error: None,
disabled: false,
}
}
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.description = Some(description.into());
self
}
pub fn error(mut self, error: impl Into<SharedString>) -> Self {
self.error = Some(error.into());
self
}
pub fn error_opt(mut self, error: Option<SharedString>) -> Self {
self.error = error;
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
impl RenderOnce for Input {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let colors = cx.arc_theme().colors;
let has_error = self.error.as_ref().is_some_and(|e| !e.is_empty());
let is_focused = self.state.read(cx).focus_handle(cx).is_focused(window);
let border_color = if has_error {
colors.danger
} else if is_focused {
colors.foreground
} else {
colors.border_strong
};
let hover_border = colors.foreground;
let shell = div()
.w_full()
.min_h(ControlHeights::MD)
.px(Spacing::S3)
.flex()
.items_center()
.rounded(Radii::CONTROL)
.border_1()
.border_color(border_color)
.bg(if self.disabled {
colors.surface_muted
} else {
colors.surface
})
.when(self.disabled, |this| this.opacity(0.5))
.when(!self.disabled && !has_error && !is_focused, |this| {
this.hover(move |s| s.border_color(hover_border))
})
.text_size(Typography::TEXT_SM)
.text_color(colors.foreground)
.child(
KitInput::new(&self.state)
.appearance(false)
.disabled(self.disabled)
.w_full(),
);
let desc_id = ElementId::NamedChild(self.id.clone().into(), "description".into());
let err_id = ElementId::NamedChild(self.id.clone().into(), "error".into());
div()
.id(self.id)
.w_full()
.flex()
.flex_col()
.child(
div()
.mb(Spacing::S2)
.text_size(Typography::TEXT_SM)
.font_medium()
.text_color(colors.foreground)
.child(self.label),
)
.child(shell)
.child(render_field_message(
desc_id,
self.description,
false,
window,
cx,
))
.child(render_field_message(err_id, self.error, true, window, cx))
}
}
#[cfg(test)]
mod tests {
use arc_tokens::{Accent, ThemeMode, install_arc_theme};
use gpui_kit::{Context, Render, TestAppContext, div, px, test::TestWindowExt as _};
use super::*;
struct InputHost {
state: Entity<InputState>,
error: Option<SharedString>,
}
impl Render for InputHost {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().w(px(360.0)).p(Spacing::S4).child(
Input::new("email", "Work email", &self.state)
.description("We only send release summaries.")
.error_opt(self.error.clone()),
)
}
}
#[gpui_kit::test]
fn input_renders_updates_value_and_toggles_error_message(cx: &mut TestAppContext) {
cx.update(|cx| {
gpui_kit::init(cx);
install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
});
let (view, cx) = cx.add_window_view(|window, cx| {
let state = cx.new(|cx| {
InputState::new(window, cx)
.placeholder("name@company.com")
.default_value("alex@arc.dev")
});
InputHost { state, error: None }
});
cx.update(|window, cx| {
window.render_frame(cx);
let state = view.read(cx).state.clone();
assert_eq!(state.read(cx).value(), "alex@arc.dev");
state.update(cx, |s, cx| s.set_value("invalid", window, cx));
view.update(cx, |v, cx| {
v.error = Some("Enter a valid email address.".into());
cx.notify();
});
window.render_frame(cx);
assert_eq!(state.read(cx).value(), "invalid");
});
}
}