use std::{cell::RefCell, rc::Rc};
use gpui::{prelude::*, App, Entity, SharedString, Window};
use super::{anchored, errors};
use crate::form::{
path::FormValues,
state::{blur, set_value, submit, Binding, FieldApi},
};
use crate::{
components::{
Field, FieldDescription, FieldLabel, Input, InputEvent, InputState, Spinner, Textarea,
},
IconName,
};
#[must_use = "components do nothing unless rendered as a child"]
pub struct BoundInput<V> {
field: FieldApi<V, String>,
placeholder: SharedString,
masked: bool,
leading_icon: Option<IconName>,
disabled: bool,
multiline: bool,
}
impl<V: FormValues> BoundInput<V> {
pub fn new(field: &FieldApi<V, String>) -> Self {
Self {
field: field.clone(),
placeholder: SharedString::default(),
masked: false,
leading_icon: None,
disabled: false,
multiline: false,
}
}
pub fn placeholder(mut self, placeholder: impl Into<SharedString>) -> Self {
self.placeholder = placeholder.into();
self
}
pub fn masked(mut self, masked: bool) -> Self {
self.masked = masked;
self
}
pub fn leading_icon(mut self, icon: IconName) -> Self {
self.leading_icon = Some(icon);
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
fn bind<V: FormValues>(
field: &FieldApi<V, String>,
placeholder: &SharedString,
masked: bool,
multiline: bool,
window: &mut Window,
cx: &mut App,
) -> Entity<InputState> {
let form = field.form.clone();
let key = field.name().clone();
let existing = form
.read(cx)
.bindings
.get(&key)
.map(|binding| (binding.input.clone(), binding.path.clone()));
if let Some((input, path)) = existing {
*path.borrow_mut() = field.path.clone();
return input;
}
let text = field.value().cloned().unwrap_or_default();
let placeholder = placeholder.clone();
let input = cx.new(|cx| {
InputState::new(cx)
.with_text(text)
.with_placeholder(placeholder)
.with_masked_text(masked)
.with_multiline(multiline)
});
let path = Rc::new(RefCell::new(field.path.clone()));
let focus_handle = input.read(cx).focus_handle_ref().clone();
let changes = {
let (form, path) = (form.downgrade(), path.clone());
window.subscribe(&input, cx, move |_, event: &InputEvent, window, cx| {
let Some(form) = form.upgrade() else {
return;
};
let path = path.borrow().clone();
match event {
InputEvent::Changed(text) => {
let current = path.get(&form.read(cx).values).cloned();
if current.as_deref() != Some(text.as_ref()) {
set_value(&form, cx, &path, text.to_string());
}
}
InputEvent::Submitted(_) => submit(&form, Some(window), cx),
}
})
};
let blurs = {
let (form, path) = (form.downgrade(), path.clone());
window.on_focus_out(&focus_handle, cx, move |_, _, cx| {
let Some(form) = form.upgrade() else {
return;
};
let key = path.borrow().key().clone();
blur(&form, cx, &key);
})
};
form.update(cx, |inner, _| {
inner.focus.insert(key.clone(), focus_handle);
inner.bindings.insert(
key,
Binding {
input: input.clone(),
path,
_subscriptions: vec![changes, blurs],
},
);
});
input
}
impl<V: FormValues> RenderOnce for BoundInput<V> {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let input = bind(
&self.field,
&self.placeholder,
self.masked,
self.multiline,
window,
cx,
);
let value = self.field.value().cloned().unwrap_or_default();
if input.read(cx).text().as_ref() != value {
input.update(cx, |state, cx| state.set_text(value, cx));
}
if self.multiline {
return Textarea::new(&input)
.invalid(self.field.should_show_errors())
.disabled(self.disabled)
.into_any_element();
}
let mut element = Input::new(&input)
.invalid(self.field.should_show_errors())
.disabled(self.disabled);
if let Some(icon) = self.leading_icon {
element = element.leading_icon(icon);
}
element.into_any_element()
}
}
impl<V: FormValues> IntoElement for BoundInput<V> {
type Element = gpui::Component<Self>;
fn into_element(self) -> Self::Element {
gpui::Component::new(self)
}
}
#[must_use = "components do nothing unless rendered as a child"]
pub struct TextField<V> {
input: BoundInput<V>,
label: SharedString,
description: Option<SharedString>,
}
impl<V: FormValues> TextField<V> {
pub fn new(field: &FieldApi<V, String>, label: impl Into<SharedString>) -> Self {
Self {
input: BoundInput::new(field),
label: label.into(),
description: None,
}
}
pub fn placeholder(mut self, placeholder: impl Into<SharedString>) -> Self {
self.input = self.input.placeholder(placeholder);
self
}
pub fn password(mut self) -> Self {
self.input = self.input.masked(true);
self
}
pub fn leading_icon(mut self, icon: IconName) -> Self {
self.input = self.input.leading_icon(icon);
self
}
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.description = Some(description.into());
self
}
}
impl<V: FormValues> RenderOnce for TextField<V> {
fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
let field = self.input.field.clone();
let invalid = field.should_show_errors();
let element = Field::new()
.invalid(invalid)
.child(FieldLabel::new(self.label))
.child(self.input)
.children(self.description.map(FieldDescription::new))
.children(errors(&field))
.children(field.meta().is_validating.then(Spinner::new));
anchored(&field, element, cx)
}
}
impl<V: FormValues> IntoElement for TextField<V> {
type Element = gpui::Component<Self>;
fn into_element(self) -> Self::Element {
gpui::Component::new(self)
}
}
#[must_use = "components do nothing unless rendered as a child"]
pub struct TextareaField<V> {
field: TextField<V>,
}
impl<V: FormValues> TextareaField<V> {
pub fn new(field: &FieldApi<V, String>, label: impl Into<SharedString>) -> Self {
let mut text = TextField::new(field, label);
text.input.multiline = true;
Self { field: text }
}
pub fn placeholder(mut self, placeholder: impl Into<SharedString>) -> Self {
self.field = self.field.placeholder(placeholder);
self
}
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.field = self.field.description(description);
self
}
}
impl<V: FormValues> RenderOnce for TextareaField<V> {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
self.field
}
}
impl<V: FormValues> IntoElement for TextareaField<V> {
type Element = gpui::Component<Self>;
fn into_element(self) -> Self::Element {
gpui::Component::new(self)
}
}