From b476835c114cbb4bb09ed1b91b2b76e6ffea79f2 Mon Sep 17 00:00:00 2001 From: luoxiaozero <48741584+luoxiaozero@users.noreply.github.com> Date: Sat, 24 Aug 2024 01:36:12 +0800 Subject: [PATCH] feat: Calendar adds children prop (#239) --- thaw/src/calendar/docs/mod.md | 13 ++++++++----- thaw/src/calendar/mod.rs | 36 +++++++++++++++++++++++++++++++++-- 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/thaw/src/calendar/docs/mod.md b/thaw/src/calendar/docs/mod.md index 358db27..a4e4dd9 100644 --- a/thaw/src/calendar/docs/mod.md +++ b/thaw/src/calendar/docs/mod.md @@ -8,14 +8,17 @@ let option_value = RwSignal::new(Some(Local::now().date_naive())); view! { - + + {date.year()}"-"{date.month()}"-"{date.day()} + } ``` ### Calendar Props -| Name | Type | Default | Desciption | -| ----- | ------------------------ | -------------------- | -------------- | -| class | `MaybeProp` | `Default::default()` | | -| value | `OptionModel` | `Default::default()` | selected date. | +| Name | Type | Default | Desciption | +| -------- | ----------------------------- | -------------------- | -------------- | +| class | `MaybeProp` | `Default::default()` | | +| value | `OptionModel` | `Default::default()` | selected date. | +| children | `Option>` | `None` | . | diff --git a/thaw/src/calendar/mod.rs b/thaw/src/calendar/mod.rs index c5005dc..fa6b494 100644 --- a/thaw/src/calendar/mod.rs +++ b/thaw/src/calendar/mod.rs @@ -1,7 +1,8 @@ use crate::{Button, ButtonGroup}; use chrono::{Datelike, Days, Local, Month, Months, NaiveDate}; use leptos::prelude::*; -use std::ops::Deref; +use std::{ops::Deref, sync::Arc}; +use tachys::view::any_view::AnyView; use thaw_utils::{class_list, mount_style, OptionModel, OptionModelWithValue}; #[component] @@ -10,6 +11,7 @@ pub fn Calendar( /// selected date. #[prop(optional, into)] value: OptionModel, + #[prop(optional, into)] children: Option, ) -> impl IntoView { mount_style("calendar", include_str!("./calendar.css")); let show_date = RwSignal::new(value.get_untracked().unwrap_or(now_date())); @@ -118,7 +120,14 @@ pub fn Calendar( .into_iter() .enumerate() .map(|(index, date)| { - view! { } + view! { + + } }) .collect_view() }} @@ -133,6 +142,7 @@ fn CalendarItem( value: OptionModel, index: usize, date: CalendarItemDate, + children: Option, ) -> impl IntoView { let is_selected = Memo::new({ let date = date.clone(); @@ -171,6 +181,7 @@ fn CalendarItem( }} + {children.map(|c| c(date.deref()))}
} @@ -213,3 +224,24 @@ impl Deref for CalendarItemDate { pub(crate) fn now_date() -> NaiveDate { Local::now().date_naive() } + +#[derive(Clone)] +pub struct CalendarChildrenFn(Arc AnyView + Send + Sync>); + +impl Deref for CalendarChildrenFn { + type Target = Arc AnyView + Send + Sync>; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl From for CalendarChildrenFn +where + F: Fn(&NaiveDate) -> C + Send + Sync + 'static, + C: RenderHtml + Send + 'static, +{ + fn from(f: F) -> Self { + Self(Arc::new(move |date| f(date).into_any())) + } +}