feat: InputNumber adds ComponentRef (#70)

This commit is contained in:
luoxiaozero 2024-01-06 23:51:23 +08:00 committed by GitHub
parent 5cce9afcb9
commit 74e5491d3d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 2 deletions

View file

@ -47,3 +47,10 @@ view! {
#### T impl
`T: Add<Output = T> + Sub<Output = T> + Default + Clone + FromStr + ToString + 'static`
### InputNumber Ref
| Name | Type | Description |
| ----- | ----------- | ------------------------ |
| focus | `Fn(&self)` | Focus the input element. |
| blur | `Fn(&self)` | Blur the input element. |

View file

@ -1,5 +1,5 @@
use crate::utils::StoredMaybeSignal;
use crate::{AiIcon, Button, ButtonVariant, Icon, Input, InputSuffix};
use crate::{AiIcon, Button, ButtonVariant, ComponentRef, Icon, Input, InputRef, InputSuffix};
use leptos::*;
use std::ops::{Add, Sub};
use std::str::FromStr;
@ -12,6 +12,7 @@ pub fn InputNumber<T>(
#[prop(optional, into)] disabled: MaybeSignal<bool>,
#[prop(optional, into)] invalid: MaybeSignal<bool>,
#[prop(optional, into)] class: MaybeSignal<String>,
#[prop(optional)] comp_ref: ComponentRef<InputNumberRef>,
#[prop(attrs)] attrs: Vec<(&'static str, Attribute)>,
) -> impl IntoView
where
@ -47,8 +48,23 @@ where
e.prevent_default();
value.set(value.get_untracked() - step.get_untracked());
});
let input_ref = ComponentRef::<InputRef>::new();
input_ref.on_load(move |_| {
comp_ref.load(InputNumberRef { input_ref });
});
view! {
<Input attrs class value=input_value allow_value placeholder disabled invalid>
<Input
attrs
class
value=input_value
allow_value
placeholder
disabled
invalid
comp_ref=input_ref
>
<InputSuffix slot>
<Button disabled variant=ButtonVariant::Link on_click=sub>
<Icon icon=Icon::from(AiIcon::AiMinusOutlined) style="font-size: 18px"/>
@ -60,3 +76,22 @@ where
</Input>
}
}
#[derive(Clone)]
pub struct InputNumberRef {
input_ref: ComponentRef<InputRef>,
}
impl InputNumberRef {
pub fn focus(&self) {
if let Some(input_ref) = self.input_ref.get_untracked() {
input_ref.focus();
}
}
pub fn blur(&self) {
if let Some(input_ref) = self.input_ref.get_untracked() {
input_ref.blur();
}
}
}