use leptos::html::*;
use leptos::prelude::*;
#[component]
pub fn Textarea(
#[prop(into, optional)] initial_value: Signal<String>,
#[prop(into, optional)] label: String,
#[prop(into, optional)] name: String,
#[prop(optional)] input_node_ref: NodeRef<Textarea>,
#[prop(default = false, optional)] readonly: bool,
#[prop(default = false, optional)] required: bool,
#[prop(into, optional)] placeholder: String,
#[prop(into, optional)] ext_input_styles: String,
#[prop(into, optional)] id_attr: String,
) -> impl IntoView {
view! {
<div class="box-border">
{
if label.is_empty() {
None
} else {
Some(
view! {
<label
class={format!("block text-sm font-bold")}
for=id_attr.clone()
>
{label}
{move || required.then_some(view! {
<span class="text-danger ml-1">*</span>
})}
</label>
}
)
}
}
<textarea
class=move || format!(
"form-input ring-0 shadow-sm appearance-none border border-mid-gray rounded w-full py-2 px-3 leading-tight focus:outline-none focus:ring-2 focus:ring-secondary focus:border-transparent flex-grow bg-transparent {}",
ext_input_styles
)
name=name
node_ref=input_node_ref
readonly=readonly
placeholder=placeholder
id=id_attr.clone()
required=required
>
{move || initial_value.get()}
</textarea>
</div>
}
}
#[cfg(test)]
mod tests {
use super::*;
fn label_visible(label: &str) -> bool {
!label.is_empty()
}
#[test]
fn empty_label_is_hidden() {
assert!(!label_visible(""));
}
#[test]
fn non_empty_label_is_shown() {
assert!(label_visible("Description"));
}
fn shows_required_asterisk(required: bool) -> bool {
required
}
#[test]
fn required_shows_asterisk() {
assert!(shows_required_asterisk(true));
}
#[test]
fn not_required_hides_asterisk() {
assert!(!shows_required_asterisk(false));
}
#[test]
fn initial_value_updates_reactively() {
let owner = Owner::new();
owner.with(|| {
let content = RwSignal::new("hello".to_string());
let initial_value = Signal::derive(move || content.get());
assert_eq!(initial_value.get(), "hello");
content.set("updated".to_string());
assert_eq!(initial_value.get(), "updated");
});
}
#[test]
fn empty_initial_value_is_valid() {
let owner = Owner::new();
owner.with(|| {
let initial_value = Signal::derive(move || String::new());
assert_eq!(initial_value.get(), "");
});
}
}