feat: TimePicker adds rules prop

This commit is contained in:
luoxiao 2024-08-22 16:12:38 +08:00 committed by luoxiaozero
parent f773fada0c
commit aba7e8818a
3 changed files with 96 additions and 9 deletions

View file

@ -65,10 +65,12 @@ view! {
})] })]
/> />
</Field> </Field>
<Field name="date"> <Field label="Date" name="date">
<DatePicker rules=vec![DatePickerRule::required(true.into())]/> <DatePicker rules=vec![DatePickerRule::required(true.into())]/>
</Field> </Field>
<Field label="Time" name="time">
<TimePicker rules=vec![TimePickerRule::required(true.into())]/>
</Field>
<div style="margin-top: 8px"> <div style="margin-top: 8px">
<Button <Button
button_type=ButtonType::Submit button_type=ButtonType::Submit

View file

@ -1,5 +1,5 @@
use super::{FieldContextInjection, FieldInjection, FieldValidationState}; use super::{FieldContextInjection, FieldInjection, FieldValidationState};
use chrono::NaiveDate; use chrono::{NaiveDate, NaiveTime};
use leptos::prelude::*; use leptos::prelude::*;
use send_wrapper::SendWrapper; use send_wrapper::SendWrapper;
use std::ops::Deref; use std::ops::Deref;
@ -147,6 +147,18 @@ impl RuleValueWithUntracked<Option<NaiveDate>> for OptionModel<NaiveDate> {
} }
} }
impl RuleValueWithUntracked<Option<NaiveTime>> for OptionModel<NaiveTime> {
fn value_with_untracked(
&self,
f: impl FnOnce(&Option<NaiveTime>) -> Result<(), FieldValidationState>,
) -> Result<(), FieldValidationState> {
self.with_untracked(move |v| match v {
OptionModelWithValue::T(v) => f(&Some(v.clone())),
OptionModelWithValue::Option(v) => f(v),
})
}
}
impl RuleValueWithUntracked<Vec<String>> for VecModel<String> { impl RuleValueWithUntracked<Vec<String>> for VecModel<String> {
fn value_with_untracked( fn value_with_untracked(
&self, &self,

View file

@ -1,23 +1,33 @@
use crate::{ use crate::{
Button, ButtonSize, ConfigInjection, Icon, Input, InputSuffix, Scrollbar, ScrollbarRef, Button, ButtonSize, ConfigInjection, Icon, Input, InputSuffix, Scrollbar, ScrollbarRef, FieldInjection, FieldValidationState, Rule
SignalWatch,
}; };
use chrono::{Local, NaiveTime, Timelike}; use chrono::{Local, NaiveTime, Timelike};
use leptos::{html, prelude::*}; use leptos::{html, prelude::*};
use thaw_components::{Binder, CSSTransition, Follower, FollowerPlacement}; use thaw_components::{Binder, CSSTransition, Follower, FollowerPlacement};
use thaw_utils::{ use thaw_utils::{
class_list, mount_style, ArcOneCallback, ComponentRef, OptionModel, OptionModelWithValue, class_list, mount_style, ArcOneCallback, ComponentRef, OptionModel, OptionModelWithValue, SignalWatch
}; };
use std::ops::Deref;
#[component] #[component]
pub fn TimePicker( pub fn TimePicker(
#[prop(optional, into)] class: MaybeProp<String>, #[prop(optional, into)] class: MaybeProp<String>,
#[prop(optional, into)] id: MaybeProp<String>,
/// A string specifying a name for the input control.
/// This name is submitted along with the control's value when the form data is submitted.
#[prop(optional, into)]
name: MaybeProp<String>,
/// The rules to validate Field.
#[prop(optional, into)]
rules: Vec<TimePickerRule>,
/// Set the TimePicker value. /// Set the TimePicker value.
#[prop(optional, into)] #[prop(optional, into)]
value: OptionModel<NaiveTime>, value: OptionModel<NaiveTime>,
// #[prop(attrs)] attrs: Vec<(&'static str, Attribute)>, // #[prop(attrs)] attrs: Vec<(&'static str, Attribute)>,
) -> impl IntoView { ) -> impl IntoView {
mount_style("time-picker", include_str!("./time-picker.css")); mount_style("time-picker", include_str!("./time-picker.css"));
let (id, name) = FieldInjection::use_id_and_name(id, name);
let validate = Rule::validate(rules, value, name);
let time_picker_ref = NodeRef::<html::Div>::new(); let time_picker_ref = NodeRef::<html::Div>::new();
let panel_ref = ComponentRef::<PanelRef>::default(); let panel_ref = ComponentRef::<PanelRef>::default();
let is_show_panel = RwSignal::new(false); let is_show_panel = RwSignal::new(false);
@ -55,6 +65,7 @@ pub fn TimePicker(
} else { } else {
update_show_time_text(); update_show_time_text();
} }
validate.run(Some(TimePickerRuleTrigger::Blur));
}; };
let close_panel = move |time: Option<NaiveTime>| { let close_panel = move |time: Option<NaiveTime>| {
if value.get_untracked() != time { if value.get_untracked() != time {
@ -66,7 +77,10 @@ pub fn TimePicker(
is_show_panel.set(false); is_show_panel.set(false);
}; };
let open_panel = move |_| { let open_panel = move || {
if is_show_panel.get() {
return;
}
panel_selected_time.set(value.get_untracked()); panel_selected_time.set(value.get_untracked());
is_show_panel.set(true); is_show_panel.set(true);
request_animation_frame(move || { request_animation_frame(move || {
@ -78,8 +92,8 @@ pub fn TimePicker(
view! { view! {
<Binder target_ref=time_picker_ref> <Binder target_ref=time_picker_ref>
<div node_ref=time_picker_ref class=class_list!["thaw-time-picker", class]> <div node_ref=time_picker_ref class=class_list!["thaw-time-picker", class] on:click=move |_| open_panel()>
<Input value=show_time_text on_focus=open_panel on_blur=on_input_blur> <Input id name value=show_time_text on_focus=move |_| open_panel() on_blur=on_input_blur>
<InputSuffix slot> <InputSuffix slot>
<Icon icon=icondata_ai::AiClockCircleOutlined style="font-size: 18px" /> <Icon icon=icondata_ai::AiClockCircleOutlined style="font-size: 18px" />
</InputSuffix> </InputSuffix>
@ -98,6 +112,64 @@ pub fn TimePicker(
} }
} }
#[derive(Debug, Default, PartialEq, Clone, Copy)]
pub enum TimePickerRuleTrigger {
#[default]
Blur,
}
pub struct TimePickerRule(Rule<Option<NaiveTime>, TimePickerRuleTrigger>);
impl TimePickerRule {
pub fn required(required: MaybeSignal<bool>) -> Self {
Self::validator(move |value, name| {
if required.get_untracked() && value.is_none() {
let message = name.get_untracked().map_or_else(
|| String::from("Please select!"),
|name| format!("Please select {name}!"),
);
Err(FieldValidationState::Error(message))
} else {
Ok(())
}
})
}
pub fn required_with_message(
required: MaybeSignal<bool>,
message: MaybeSignal<String>,
) -> Self {
Self::validator(move |value, _| {
if required.get_untracked() && value.is_none() {
Err(FieldValidationState::Error(message.get_untracked()))
} else {
Ok(())
}
})
}
pub fn validator(
f: impl Fn(&Option<NaiveTime>, Signal<Option<String>>) -> Result<(), FieldValidationState>
+ Send
+ Sync
+ 'static,
) -> Self {
Self(Rule::validator(f))
}
pub fn with_trigger(self, trigger: TimePickerRuleTrigger) -> Self {
Self(Rule::with_trigger(self.0, trigger))
}
}
impl Deref for TimePickerRule {
type Target = Rule<Option<NaiveTime>, TimePickerRuleTrigger>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[component] #[component]
fn Panel( fn Panel(
selected_time: RwSignal<Option<NaiveTime>>, selected_time: RwSignal<Option<NaiveTime>>,
@ -174,6 +246,7 @@ fn Panel(
data-thaw-id=config_provider.id().clone() data-thaw-id=config_provider.id().clone()
style=move || display.get().unwrap_or_default() style=move || display.get().unwrap_or_default()
node_ref=panel_ref node_ref=panel_ref
on:mousedown=|e| e.prevent_default()
> >
<div class="thaw-time-picker-panel__time"> <div class="thaw-time-picker-panel__time">
<div class="thaw-time-picker-panel__time-hour"> <div class="thaw-time-picker-panel__time-hour">