mirror of
https://github.com/adoyle0/thaw.git
synced 2025-01-22 22:09:22 -05:00
feat: DatePicker adds rules prop
This commit is contained in:
parent
e1e5c02e54
commit
db0148258d
7 changed files with 117 additions and 14 deletions
|
@ -16,6 +16,9 @@ view! {
|
||||||
### DatePicker Props
|
### DatePicker Props
|
||||||
|
|
||||||
| Name | Type | Default | Desciption |
|
| Name | Type | Default | Desciption |
|
||||||
| ----- | ------------------------ | -------------------- | -------------------------- |
|
| --- | --- | --- | --- |
|
||||||
| class | `MaybeProp<String>` | `Default::default()` | |
|
| class | `MaybeProp<String>` | `Default::default()` | |
|
||||||
|
| id | `MaybeProp<String>` | `Default::default()` | |
|
||||||
|
| name | `MaybeProp<String>` | `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<DatePickerRule<T>>` | `vec![]` | The rules to validate Field. |
|
||||||
| value | `OptionModel<NaiveDate>` | `Default::default()` | Set the date picker value. |
|
| value | `OptionModel<NaiveDate>` | `Default::default()` | Set the date picker value. |
|
||||||
|
|
|
@ -1,21 +1,33 @@
|
||||||
mod panel;
|
mod panel;
|
||||||
use crate::{Icon, Input, InputSuffix, SignalWatch};
|
|
||||||
|
use crate::{FieldInjection, FieldValidationState, Icon, Input, InputSuffix, Rule};
|
||||||
use chrono::NaiveDate;
|
use chrono::NaiveDate;
|
||||||
use leptos::{html, prelude::*};
|
use leptos::{html, prelude::*};
|
||||||
use panel::{Panel, PanelRef};
|
use panel::{Panel, PanelRef};
|
||||||
|
use std::ops::Deref;
|
||||||
use thaw_components::{Binder, Follower, FollowerPlacement};
|
use thaw_components::{Binder, Follower, FollowerPlacement};
|
||||||
use thaw_utils::{
|
use thaw_utils::{
|
||||||
class_list, mount_style, now_date, ComponentRef, OptionModel, OptionModelWithValue,
|
class_list, mount_style, now_date, ComponentRef, OptionModel, OptionModelWithValue, SignalWatch,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn DatePicker(
|
pub fn DatePicker(
|
||||||
#[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<DatePickerRule>,
|
||||||
/// Set the date picker value.
|
/// Set the date picker value.
|
||||||
#[prop(optional, into)]
|
#[prop(optional, into)]
|
||||||
value: OptionModel<NaiveDate>,
|
value: OptionModel<NaiveDate>,
|
||||||
) -> impl IntoView {
|
) -> impl IntoView {
|
||||||
mount_style("date-picker", include_str!("./date-picker.css"));
|
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::<html::Div>::new();
|
let date_picker_ref = NodeRef::<html::Div>::new();
|
||||||
let is_show_panel = RwSignal::new(false);
|
let is_show_panel = RwSignal::new(false);
|
||||||
let show_date_text = RwSignal::new(String::new());
|
let show_date_text = RwSignal::new(String::new());
|
||||||
|
@ -53,6 +65,7 @@ pub fn DatePicker(
|
||||||
} else {
|
} else {
|
||||||
update_show_date_text();
|
update_show_date_text();
|
||||||
}
|
}
|
||||||
|
validate.run(Some(DatePickerRuleTrigger::Blur));
|
||||||
};
|
};
|
||||||
|
|
||||||
let close_panel = move |date: Option<NaiveDate>| {
|
let close_panel = move |date: Option<NaiveDate>| {
|
||||||
|
@ -65,7 +78,10 @@ pub fn DatePicker(
|
||||||
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_date.set(value.get_untracked());
|
panel_selected_date.set(value.get_untracked());
|
||||||
if let Some(panel_ref) = panel_ref.get_untracked() {
|
if let Some(panel_ref) = panel_ref.get_untracked() {
|
||||||
panel_ref.init_panel(value.get_untracked().unwrap_or(now_date()));
|
panel_ref.init_panel(value.get_untracked().unwrap_or(now_date()));
|
||||||
|
@ -75,8 +91,8 @@ pub fn DatePicker(
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
<Binder target_ref=date_picker_ref>
|
<Binder target_ref=date_picker_ref>
|
||||||
<div node_ref=date_picker_ref class=class_list!["thaw-date-picker", class]>
|
<div node_ref=date_picker_ref class=class_list!["thaw-date-picker", class] on:click=move |_| open_panel()>
|
||||||
<Input value=show_date_text on_focus=open_panel on_blur=on_input_blur>
|
<Input id name value=show_date_text on_focus=move |_| open_panel() on_blur=on_input_blur>
|
||||||
<InputSuffix slot>
|
<InputSuffix slot>
|
||||||
<Icon icon=icondata_ai::AiCalendarOutlined style="font-size: 18px" />
|
<Icon icon=icondata_ai::AiCalendarOutlined style="font-size: 18px" />
|
||||||
</InputSuffix>
|
</InputSuffix>
|
||||||
|
@ -94,3 +110,61 @@ pub fn DatePicker(
|
||||||
</Binder>
|
</Binder>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default, PartialEq, Clone, Copy)]
|
||||||
|
pub enum DatePickerRuleTrigger {
|
||||||
|
#[default]
|
||||||
|
Blur,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct DatePickerRule(Rule<Option<NaiveDate>, DatePickerRuleTrigger>);
|
||||||
|
|
||||||
|
impl DatePickerRule {
|
||||||
|
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<NaiveDate>, Signal<Option<String>>) -> 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<Option<NaiveDate>, DatePickerRuleTrigger>;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -76,6 +76,7 @@ pub 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()
|
||||||
>
|
>
|
||||||
|
|
||||||
{move || {
|
{move || {
|
||||||
|
|
|
@ -59,8 +59,13 @@ view! {
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
})]/>
|
})]
|
||||||
|
/>
|
||||||
</Field>
|
</Field>
|
||||||
|
<Field name="date">
|
||||||
|
<DatePicker rules=vec![DatePickerRule::required(true.into())]/>
|
||||||
|
</Field>
|
||||||
|
|
||||||
<div style="margin-top: 8px">
|
<div style="margin-top: 8px">
|
||||||
<Button
|
<Button
|
||||||
button_type=ButtonType::Submit
|
button_type=ButtonType::Submit
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use super::{FieldContextInjection, FieldInjection, FieldValidationState};
|
use super::{FieldContextInjection, FieldInjection, FieldValidationState};
|
||||||
|
use chrono::NaiveDate;
|
||||||
use leptos::prelude::*;
|
use leptos::prelude::*;
|
||||||
use send_wrapper::SendWrapper;
|
use send_wrapper::SendWrapper;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
@ -46,11 +47,12 @@ impl<T, Trigger> Rule<T, Trigger> {
|
||||||
Trigger: PartialEq + 'static,
|
Trigger: PartialEq + 'static,
|
||||||
{
|
{
|
||||||
let field_injection = FieldInjection::use_context();
|
let field_injection = FieldInjection::use_context();
|
||||||
|
let rules_is_empty = rules.is_empty();
|
||||||
let rules = StoredValue::new(SendWrapper::new(rules));
|
let rules = StoredValue::new(SendWrapper::new(rules));
|
||||||
let validate = Callback::new(move |trigger: Option<Trigger>| {
|
let validate = Callback::new(move |trigger: Option<Trigger>| {
|
||||||
let state = rules.with_value(move |rules| {
|
let state = rules.with_value(move |rules| {
|
||||||
if rules.is_empty() {
|
if rules.is_empty() {
|
||||||
return Some(Ok(()));
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut rules_iter = rules.iter();
|
let mut rules_iter = rules.iter();
|
||||||
|
@ -88,9 +90,12 @@ impl<T, Trigger> Rule<T, Trigger> {
|
||||||
};
|
};
|
||||||
rt
|
rt
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if !rules_is_empty {
|
||||||
if let Some(field_context) = FieldContextInjection::use_context() {
|
if let Some(field_context) = FieldContextInjection::use_context() {
|
||||||
field_context.register_field(name, move || validate.run(None));
|
field_context.register_field(name, move || validate.run(None));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
validate
|
validate
|
||||||
}
|
}
|
||||||
|
@ -130,6 +135,18 @@ impl RuleValueWithUntracked<Option<String>> for OptionModel<String> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl RuleValueWithUntracked<Option<NaiveDate>> for OptionModel<NaiveDate> {
|
||||||
|
fn value_with_untracked(
|
||||||
|
&self,
|
||||||
|
f: impl FnOnce(&Option<NaiveDate>) -> 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,
|
||||||
|
|
|
@ -27,7 +27,9 @@ pub fn Input(
|
||||||
/// This name is submitted along with the control's value when the form data is submitted.
|
/// This name is submitted along with the control's value when the form data is submitted.
|
||||||
#[prop(optional, into)]
|
#[prop(optional, into)]
|
||||||
name: MaybeProp<String>,
|
name: MaybeProp<String>,
|
||||||
#[prop(optional, into)] rules: Vec<InputRule>,
|
/// The rules to validate Field.
|
||||||
|
#[prop(optional, into)]
|
||||||
|
rules: Vec<InputRule>,
|
||||||
/// Set the input value.
|
/// Set the input value.
|
||||||
#[prop(optional, into)]
|
#[prop(optional, into)]
|
||||||
value: Model<String>,
|
value: Model<String>,
|
||||||
|
|
|
@ -16,7 +16,8 @@ pub fn SpinButton<T>(
|
||||||
#[prop(optional, into)]
|
#[prop(optional, into)]
|
||||||
name: MaybeProp<String>,
|
name: MaybeProp<String>,
|
||||||
/// The rules to validate Field.
|
/// The rules to validate Field.
|
||||||
#[prop(optional, into)] rules: Vec<SpinButtonRule<T>>,
|
#[prop(optional, into)]
|
||||||
|
rules: Vec<SpinButtonRule<T>>,
|
||||||
/// Current value of the control.
|
/// Current value of the control.
|
||||||
#[prop(optional, into)]
|
#[prop(optional, into)]
|
||||||
value: Model<T>,
|
value: Model<T>,
|
||||||
|
|
Loading…
Add table
Reference in a new issue