feat: input value

This commit is contained in:
luoxiao 2023-05-30 23:00:30 +08:00
parent 25e3b81036
commit f2405604d2
6 changed files with 34 additions and 19 deletions

View file

@ -28,6 +28,9 @@ pub fn App(cx: Scope) -> impl IntoView {
<Route path="/tabbar" view=move |cx| view! {cx, <Route path="/tabbar" view=move |cx| view! {cx,
<MobilePage path="/mobile/tabbar" /> <MobilePage path="/mobile/tabbar" />
} /> } />
<Route path="/input" view=move |cx| view! {cx,
<InputPage />
} />
</Route> </Route>
</Routes> </Routes>
<Routes base="/mobile".to_string()> <Routes base="/mobile".to_string()>

View file

@ -36,6 +36,7 @@ pub fn ComponentsPage(cx: Scope) -> impl IntoView {
<MenuItem key="menu" label="menu" /> <MenuItem key="menu" label="menu" />
<MenuItem key="slider" label="slider" /> <MenuItem key="slider" label="slider" />
<MenuItem key="tabbar" label="tabbar" /> <MenuItem key="tabbar" label="tabbar" />
<MenuItem key="input" label="input" />
</Menu> </Menu>
</aside> </aside>
<main> <main>

View file

@ -12,13 +12,6 @@ pub fn Home(cx: Scope) -> impl IntoView {
provide_context(cx, theme); provide_context(cx, theme);
let (count, set_count) = create_signal(cx, 0.0); let (count, set_count) = create_signal(cx, 0.0);
let (button_type, set_button_type) = create_signal(cx, ButtonType::TEXT); let (button_type, set_button_type) = create_signal(cx, ButtonType::TEXT);
let count_string = create_memo(cx, move |_| {
count.get().to_string()
});
let on_input = SignalSetter::map(cx, move |value: String| {
set_count.set(value.parse().unwrap());
});
view! { cx, view! { cx,
<Button on:click=move |_| { <Button on:click=move |_| {
let navigate = use_navigate(cx); let navigate = use_navigate(cx);
@ -28,7 +21,6 @@ pub fn Home(cx: Scope) -> impl IntoView {
</Button> </Button>
<hr /> <hr />
<Space> <Space>
<Input value=count_string on_input=on_input/>
<Button <Button
on:click=move |_| set_theme.update(move |value| *value = Theme::dark()) on:click=move |_| set_theme.update(move |value| *value = Theme::dark())
type_=button_type type_=button_type

View file

@ -0,0 +1,14 @@
use leptos::*;
use melt_ui::*;
#[component]
pub fn InputPage(cx: Scope) -> impl IntoView {
let value = create_rw_signal(cx, String::from("o"));
view! { cx,
<>
{move || value.get()}
<Input value/>
<Input value type_="password" />
</>
}
}

View file

@ -1,5 +1,6 @@
mod components; mod components;
mod home; mod home;
mod input;
mod menu; mod menu;
mod mobile; mod mobile;
mod slider; mod slider;
@ -7,6 +8,7 @@ mod tabbar;
pub use components::*; pub use components::*;
pub use home::*; pub use home::*;
pub use input::*;
pub use menu::*; pub use menu::*;
pub use mobile::*; pub use mobile::*;
pub use slider::*; pub use slider::*;

View file

@ -1,25 +1,28 @@
mod theme; mod theme;
pub use theme::InputTheme; use crate::{
use crate::{utils::mount_style::mount_style, theme::{use_theme, Theme}}; theme::{use_theme, Theme},
utils::mount_style::mount_style,
};
use leptos::*; use leptos::*;
use stylers::style_sheet_str; use stylers::style_sheet_str;
pub use theme::InputTheme;
#[component] #[component]
pub fn Input( pub fn Input(
cx: Scope, cx: Scope,
#[prop(optional, into)] value: MaybeSignal<String>, #[prop(into)] value: RwSignal<String>,
#[prop(optional)] on_input: Option<SignalSetter<String>>, #[prop(default = MaybeSignal::Static(String::from("text")), into)] type_: MaybeSignal<String>,
) -> impl IntoView { ) -> impl IntoView {
let theme = use_theme(cx, Theme::light); let theme = use_theme(cx, Theme::light);
let class_name = mount_style("input", || style_sheet_str!("./src/input/input.css")); let class_name = mount_style("input", || style_sheet_str!("./src/input/input.css"));
let input_ref = create_node_ref::<html::Input>(cx); let input_ref = create_node_ref::<html::Input>(cx);
if let Some(on_input) = on_input {
input_ref.on_load(cx, move |input| { input_ref.on_load(cx, move |input| {
input.on(ev::input, move |ev| { input.on(ev::input, move |ev| {
on_input.set(event_target_value(&ev)); value.set(event_target_value(&ev));
}); });
}); });
}
let css_vars = create_memo(cx, move |_| { let css_vars = create_memo(cx, move |_| {
let mut css_vars = String::new(); let mut css_vars = String::new();
let theme = theme.get(); let theme = theme.get();
@ -32,7 +35,7 @@ pub fn Input(
view! { view! {
cx, class=class_name, cx, class=class_name,
<div class:melt-input=true style=move || css_vars.get()> <div class:melt-input=true style=move || css_vars.get()>
<input type="text" prop:value=move || value.get() ref=input_ref class="melt-input__input-el"/> <input type=move || type_.get() prop:value=move || value.get() ref=input_ref class="melt-input__input-el"/>
</div> </div>
} }
} }