use arc_tokens::{ActiveArcTheme as _, Radii, Spacing, Typography};
use gpui_kit::{
App, ElementId, Entity, Focusable as _, IntoElement, ParentElement, RenderOnce, SharedString,
Styled, Window,
base::StyledExt as _,
component::input::{Textarea as KitTextarea, TextareaState},
div,
prelude::*,
px,
};
use super::field_message::render_field_message;
#[derive(IntoElement)]
pub struct Textarea {
id: ElementId,
label: SharedString,
state: Entity<TextareaState>,
description: Option<SharedString>,
error: Option<SharedString>,
min_height: f32,
disabled: bool,
}
impl Textarea {
pub fn new(
id: impl Into<ElementId>,
label: impl Into<SharedString>,
state: &Entity<TextareaState>,
) -> Self {
Self {
id: id.into(),
label: label.into(),
state: state.clone(),
description: None,
error: None,
min_height: 84.0,
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 min_height(mut self, min_height: f32) -> Self {
self.min_height = min_height.max(64.0);
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
impl RenderOnce for Textarea {
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(px(self.min_height))
.p(Spacing::S3)
.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.6))
.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(
KitTextarea::new(&self.state)
.appearance(false)
.disabled(self.disabled)
.w_full()
.h_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 TextareaHost {
state: Entity<TextareaState>,
error: Option<SharedString>,
}
impl Render for TextareaHost {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().w(px(400.0)).p(Spacing::S4).child(
Textarea::new("notes", "Release notes", &self.state)
.description("Supports Markdown.")
.error_opt(self.error.clone()),
)
}
}
#[gpui_kit::test]
fn textarea_renders_multiline_value_and_error(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| {
TextareaState::new(window, cx)
.rows(4)
.soft_wrap(true)
.placeholder("Write a short summary...")
.default_value("Line 1\nLine 2")
});
TextareaHost { state, error: None }
});
cx.update(|window, cx| {
window.render_frame(cx);
let state = view.read(cx).state.clone();
assert_eq!(state.read(cx).value(), "Line 1\nLine 2");
view.update(cx, |v, cx| {
v.error = Some("Description is required.".into());
cx.notify();
});
window.render_frame(cx);
});
}
}