From db0148258dc4484208d1ae013509da1dadb971ae Mon Sep 17 00:00:00 2001 From: luoxiao Date: Thu, 22 Aug 2024 14:44:23 +0800 Subject: [PATCH] feat: DatePicker adds rules prop --- thaw/src/date_picker/docs/mod.md | 9 ++-- thaw/src/date_picker/mod.rs | 84 +++++++++++++++++++++++++++++-- thaw/src/date_picker/panel/mod.rs | 1 + thaw/src/field/docs/mod.md | 7 ++- thaw/src/field/rule.rs | 23 +++++++-- thaw/src/input/mod.rs | 4 +- thaw/src/spin_button/mod.rs | 3 +- 7 files changed, 117 insertions(+), 14 deletions(-) diff --git a/thaw/src/date_picker/docs/mod.md b/thaw/src/date_picker/docs/mod.md index 4e3e6f7..da3de29 100644 --- a/thaw/src/date_picker/docs/mod.md +++ b/thaw/src/date_picker/docs/mod.md @@ -15,7 +15,10 @@ view! { ### DatePicker Props -| Name | Type | Default | Desciption | -| ----- | ------------------------ | -------------------- | -------------------------- | -| class | `MaybeProp` | `Default::default()` | | +| Name | Type | Default | Desciption | +| --- | --- | --- | --- | +| class | `MaybeProp` | `Default::default()` | | +| id | `MaybeProp` | `Default::default()` | | +| name | `MaybeProp` | `Default::default()` | 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. | +| rules | `Vec>` | `vec![]` | The rules to validate Field. | | value | `OptionModel` | `Default::default()` | Set the date picker value. | diff --git a/thaw/src/date_picker/mod.rs b/thaw/src/date_picker/mod.rs index d0b5825..fd1e267 100644 --- a/thaw/src/date_picker/mod.rs +++ b/thaw/src/date_picker/mod.rs @@ -1,21 +1,33 @@ mod panel; -use crate::{Icon, Input, InputSuffix, SignalWatch}; + +use crate::{FieldInjection, FieldValidationState, Icon, Input, InputSuffix, Rule}; use chrono::NaiveDate; use leptos::{html, prelude::*}; use panel::{Panel, PanelRef}; +use std::ops::Deref; use thaw_components::{Binder, Follower, FollowerPlacement}; use thaw_utils::{ - class_list, mount_style, now_date, ComponentRef, OptionModel, OptionModelWithValue, + class_list, mount_style, now_date, ComponentRef, OptionModel, OptionModelWithValue, SignalWatch, }; #[component] pub fn DatePicker( #[prop(optional, into)] class: MaybeProp, + #[prop(optional, into)] id: MaybeProp, + /// 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, + /// The rules to validate Field. + #[prop(optional, into)] + rules: Vec, /// Set the date picker value. #[prop(optional, into)] value: OptionModel, ) -> impl IntoView { mount_style("date-picker", include_str!("./date-picker.css")); + let (id, name) = FieldInjection::use_id_and_name(id, name); + let validate = Rule::validate(rules, value, name); let date_picker_ref = NodeRef::::new(); let is_show_panel = RwSignal::new(false); let show_date_text = RwSignal::new(String::new()); @@ -53,6 +65,7 @@ pub fn DatePicker( } else { update_show_date_text(); } + validate.run(Some(DatePickerRuleTrigger::Blur)); }; let close_panel = move |date: Option| { @@ -65,7 +78,10 @@ pub fn DatePicker( is_show_panel.set(false); }; - let open_panel = move |_| { + let open_panel = move || { + if is_show_panel.get() { + return; + } panel_selected_date.set(value.get_untracked()); if let Some(panel_ref) = panel_ref.get_untracked() { panel_ref.init_panel(value.get_untracked().unwrap_or(now_date())); @@ -75,8 +91,8 @@ pub fn DatePicker( view! { -
- +
+ @@ -94,3 +110,61 @@ pub fn DatePicker( } } + +#[derive(Debug, Default, PartialEq, Clone, Copy)] +pub enum DatePickerRuleTrigger { + #[default] + Blur, +} + +pub struct DatePickerRule(Rule, DatePickerRuleTrigger>); + +impl DatePickerRule { + pub fn required(required: MaybeSignal) -> 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, + message: MaybeSignal, + ) -> 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, Signal>) -> Result<(), FieldValidationState> + + Send + + Sync + + 'static, + ) -> Self { + Self(Rule::validator(f)) + } + + pub fn with_trigger(self, trigger: DatePickerRuleTrigger) -> Self { + Self(Rule::with_trigger(self.0, trigger)) + } +} + +impl Deref for DatePickerRule { + type Target = Rule, DatePickerRuleTrigger>; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} diff --git a/thaw/src/date_picker/panel/mod.rs b/thaw/src/date_picker/panel/mod.rs index 01697d1..1d5ba62 100644 --- a/thaw/src/date_picker/panel/mod.rs +++ b/thaw/src/date_picker/panel/mod.rs @@ -76,6 +76,7 @@ pub fn Panel( data-thaw-id=config_provider.id().clone() style=move || display.get().unwrap_or_default() node_ref=panel_ref + on:mousedown=|e| e.prevent_default() > {move || { diff --git a/thaw/src/field/docs/mod.md b/thaw/src/field/docs/mod.md index bcfdc11..d438997 100644 --- a/thaw/src/field/docs/mod.md +++ b/thaw/src/field/docs/mod.md @@ -59,8 +59,13 @@ view! { } else { Ok(()) } - })]/> + })] + /> + + + +