thaw/src/input/mod.rs

42 lines
1.4 KiB
Rust
Raw Normal View History

2023-04-06 17:27:54 +08:00
mod theme;
2023-05-30 23:00:30 +08:00
use crate::{
theme::{use_theme, Theme},
utils::mount_style::mount_style,
};
2023-04-03 17:31:50 +08:00
use leptos::*;
use stylers::style_sheet_str;
2023-05-30 23:00:30 +08:00
pub use theme::InputTheme;
2023-04-03 17:31:50 +08:00
#[component]
pub fn Input(
cx: Scope,
2023-05-30 23:00:30 +08:00
#[prop(into)] value: RwSignal<String>,
#[prop(default = MaybeSignal::Static(String::from("text")), into)] type_: MaybeSignal<String>,
2023-04-03 17:31:50 +08:00
) -> impl IntoView {
2023-04-06 17:27:54 +08:00
let theme = use_theme(cx, Theme::light);
let class_name = mount_style("input", || style_sheet_str!("./src/input/input.css"));
2023-05-30 23:00:30 +08:00
2023-04-03 17:31:50 +08:00
let input_ref = create_node_ref::<html::Input>(cx);
2023-05-30 23:00:30 +08:00
input_ref.on_load(cx, move |input| {
input.on(ev::input, move |ev| {
value.set(event_target_value(&ev));
2023-04-03 17:31:50 +08:00
});
2023-05-30 23:00:30 +08:00
});
2023-04-06 17:27:54 +08:00
let css_vars = create_memo(cx, move |_| {
let mut css_vars = String::new();
let theme = theme.get();
let border_color_hover = theme.common.color_primary.clone();
css_vars.push_str(&format!("--border-color-hover: {border_color_hover};"));
let border_radius = theme.common.border_radius.clone();
css_vars.push_str(&format!("--border-radius: {border_radius};"));
css_vars
});
2023-04-03 17:31:50 +08:00
view! {
cx, class=class_name,
2023-04-06 17:27:54 +08:00
<div class:melt-input=true style=move || css_vars.get()>
2023-05-30 23:00:30 +08:00
<input type=move || type_.get() prop:value=move || value.get() ref=input_ref class="melt-input__input-el"/>
2023-04-03 17:31:50 +08:00
</div>
}
}