mirror of
https://github.com/adoyle0/thaw.git
synced 2025-02-02 08:34:15 -05:00
feat: InputNumber adds ComponentRef (#70)
This commit is contained in:
parent
5cce9afcb9
commit
74e5491d3d
2 changed files with 44 additions and 2 deletions
|
@ -47,3 +47,10 @@ view! {
|
||||||
#### T impl
|
#### T impl
|
||||||
|
|
||||||
`T: Add<Output = T> + Sub<Output = T> + Default + Clone + FromStr + ToString + 'static`
|
`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. |
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::utils::StoredMaybeSignal;
|
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 leptos::*;
|
||||||
use std::ops::{Add, Sub};
|
use std::ops::{Add, Sub};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
@ -12,6 +12,7 @@ pub fn InputNumber<T>(
|
||||||
#[prop(optional, into)] disabled: MaybeSignal<bool>,
|
#[prop(optional, into)] disabled: MaybeSignal<bool>,
|
||||||
#[prop(optional, into)] invalid: MaybeSignal<bool>,
|
#[prop(optional, into)] invalid: MaybeSignal<bool>,
|
||||||
#[prop(optional, into)] class: MaybeSignal<String>,
|
#[prop(optional, into)] class: MaybeSignal<String>,
|
||||||
|
#[prop(optional)] comp_ref: ComponentRef<InputNumberRef>,
|
||||||
#[prop(attrs)] attrs: Vec<(&'static str, Attribute)>,
|
#[prop(attrs)] attrs: Vec<(&'static str, Attribute)>,
|
||||||
) -> impl IntoView
|
) -> impl IntoView
|
||||||
where
|
where
|
||||||
|
@ -47,8 +48,23 @@ where
|
||||||
e.prevent_default();
|
e.prevent_default();
|
||||||
value.set(value.get_untracked() - step.get_untracked());
|
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! {
|
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>
|
<InputSuffix slot>
|
||||||
<Button disabled variant=ButtonVariant::Link on_click=sub>
|
<Button disabled variant=ButtonVariant::Link on_click=sub>
|
||||||
<Icon icon=Icon::from(AiIcon::AiMinusOutlined) style="font-size: 18px"/>
|
<Icon icon=Icon::from(AiIcon::AiMinusOutlined) style="font-size: 18px"/>
|
||||||
|
@ -60,3 +76,22 @@ where
|
||||||
</Input>
|
</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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue