thaw/src/input/mod.rs

61 lines
1.6 KiB
Rust
Raw Normal View History

2023-04-06 17:27:54 +08:00
mod theme;
2023-10-07 21:41:03 +08:00
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::*;
2023-05-30 23:00:30 +08:00
pub use theme::InputTheme;
2023-04-03 17:31:50 +08:00
2023-06-19 21:53:39 +08:00
#[derive(Default, Clone)]
2023-10-08 14:01:24 +08:00
pub enum InputVariant {
2023-06-19 21:53:39 +08:00
#[default]
TEXT,
PASSWORD,
}
2023-10-08 14:01:24 +08:00
impl InputVariant {
2023-06-19 21:53:39 +08:00
pub fn as_str(&self) -> &'static str {
match self {
2023-10-08 14:01:24 +08:00
InputVariant::TEXT => "text",
InputVariant::PASSWORD => "password",
2023-06-19 21:53:39 +08:00
}
}
}
2023-04-03 17:31:50 +08:00
#[component]
pub fn Input(
2023-05-30 23:00:30 +08:00
#[prop(into)] value: RwSignal<String>,
2023-10-08 14:01:24 +08:00
#[prop(optional, into)] variant: MaybeSignal<InputVariant>,
2023-04-03 17:31:50 +08:00
) -> impl IntoView {
2023-08-29 09:11:22 +08:00
let theme = use_theme(Theme::light);
2023-10-07 21:41:03 +08:00
mount_style("input", include_str!("./input.css"));
2023-05-30 23:00:30 +08:00
2023-08-29 09:11:22 +08:00
let input_ref = create_node_ref::<html::Input>();
input_ref.on_load(move |input| {
2023-05-30 23:00:30 +08:00
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-08-29 09:11:22 +08:00
let css_vars = create_memo(move |_| {
2023-04-06 17:27:54 +08:00
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! {
2023-04-06 17:27:54 +08:00
<div class:melt-input=true style=move || css_vars.get()>
2023-10-08 09:28:13 +08:00
<input
2023-10-08 14:01:24 +08:00
type=move || variant.get().as_str()
2023-10-08 09:28:13 +08:00
prop:value=move || value.get()
ref=input_ref
class="melt-input__input-el"
/>
2023-04-03 17:31:50 +08:00
</div>
}
}