refactor: Calendar

This commit is contained in:
luoxiao 2024-06-26 23:24:43 +08:00
parent acf0d85539
commit e52741cf20
4 changed files with 26 additions and 88 deletions

View file

@ -23,7 +23,8 @@
grid-auto-rows: 1fr; grid-auto-rows: 1fr;
border-top: 1px solid; border-top: 1px solid;
border-left: 1px solid; border-left: 1px solid;
border-color: var(--thaw-border-color); border-color: var(--colorNeutralStroke2);
border-radius: var(--borderRadiusMedium);
} }
.thaw-calendar-item { .thaw-calendar-item {
@ -31,16 +32,16 @@
padding: 8px 12px; padding: 8px 12px;
border-right: 1px solid; border-right: 1px solid;
border-bottom: 1px solid; border-bottom: 1px solid;
border-color: var(--thaw-border-color); border-color: var(--colorNeutralStroke2);
cursor: pointer; cursor: pointer;
} }
.thaw-calendar-item:hover { .thaw-calendar-item:hover {
background-color: var(--thaw-background-color-hover); background-color: var(--colorNeutralBackground1Hover);
} }
.thaw-calendar-item--other-month { .thaw-calendar-item--other-month {
color: var(--thaw-font-color-other-month); color: var(--colorNeutralForegroundDisabled);
} }
.thaw-calendar-item__header { .thaw-calendar-item__header {
@ -53,7 +54,7 @@
justify-content: center; justify-content: center;
align-items: center; align-items: center;
color: white; color: white;
background-color: var(--thaw-background-color-today); background-color: var(--colorBrandBackground);
border-radius: 50%; border-radius: 50%;
margin-left: -0.4em; margin-left: -0.4em;
margin-top: -0.3em; margin-top: -0.3em;
@ -66,6 +67,6 @@
left: 0; left: 0;
right: 0; right: 0;
bottom: 0; bottom: 0;
background-color: var(--thaw-background-color-today); background-color: var(--colorBrandBackground);
height: 3px; height: 3px;
} }

View file

@ -1,8 +1,4 @@
mod theme; use crate::{Button, ButtonGroup};
pub use theme::CalendarTheme;
use crate::{use_theme, Button, ButtonGroup, Theme};
use chrono::{Datelike, Days, Local, Month, Months, NaiveDate}; use chrono::{Datelike, Days, Local, Month, Months, NaiveDate};
use leptos::*; use leptos::*;
use std::ops::Deref; use std::ops::Deref;
@ -14,31 +10,8 @@ pub fn Calendar(
#[prop(optional, into)] value: Model<Option<NaiveDate>>, #[prop(optional, into)] value: Model<Option<NaiveDate>>,
) -> impl IntoView { ) -> impl IntoView {
mount_style("calendar", include_str!("./calendar.css")); mount_style("calendar", include_str!("./calendar.css"));
let theme = use_theme(Theme::light); let show_date = RwSignal::new(value.get_untracked().unwrap_or(now_date()));
let css_vars = create_memo(move |_| { Effect::new(move |_| {
let mut css_vars = String::new();
theme.with(|theme| {
css_vars.push_str(&format!(
"--thaw-background-color-today: {};",
theme.common.color_primary
));
css_vars.push_str(&format!(
"--thaw-font-color-other-month: {};",
theme.calendar.other_month_font_color,
));
css_vars.push_str(&format!(
"--thaw-border-color: {};",
theme.calendar.border_color
));
css_vars.push_str(&format!(
"--thaw-background-color-hover: {};",
theme.calendar.background_color_hover
));
});
css_vars
});
let show_date = create_rw_signal(value.get_untracked().unwrap_or(now_date()));
create_effect(move |_| {
if let Some(selected_date) = value.get() { if let Some(selected_date) = value.get() {
let show_date_data = show_date.get_untracked(); let show_date_data = show_date.get_untracked();
if selected_date.year() != show_date_data.year() if selected_date.year() != show_date_data.year()
@ -49,7 +22,7 @@ pub fn Calendar(
} }
}); });
let dates = create_memo(move |_| { let dates = Memo::new(move |_| {
let show_date = show_date.get(); let show_date = show_date.get();
let show_date_month = show_date.month(); let show_date_month = show_date.month();
let mut dates = vec![]; let mut dates = vec![];
@ -115,7 +88,6 @@ pub fn Calendar(
view! { view! {
<div <div
class=class_list!["thaw-calendar", class.map(| c | move || c.get())] class=class_list!["thaw-calendar", class.map(| c | move || c.get())]
style=move || css_vars.get()
> >
<div class="thaw-calendar__header"> <div class="thaw-calendar__header">
<span class="thaw-calendar__header-title"> <span class="thaw-calendar__header-title">
@ -132,21 +104,19 @@ pub fn Calendar(
}} }}
</span> </span>
<span> <ButtonGroup>
<ButtonGroup> <Button
<Button icon=icondata_ai::AiLeftOutlined
icon=icondata_ai::AiLeftOutlined on_click=previous_month
on_click=previous_month />
/> <Button on_click=today>
<Button on_click=today> "Today"
"Today" </Button>
</Button> <Button
<Button icon=icondata_ai::AiRightOutlined
icon=icondata_ai::AiRightOutlined on_click=next_month
on_click=next_month />
/> </ButtonGroup>
</ButtonGroup>
</span>
</div> </div>
<div class="thaw-calendar__dates"> <div class="thaw-calendar__dates">
@ -172,7 +142,7 @@ fn CalendarItem(
index: usize, index: usize,
date: CalendarItemDate, date: CalendarItemDate,
) -> impl IntoView { ) -> impl IntoView {
let is_selected = create_memo({ let is_selected = Memo::new({
let date = date.clone(); let date = date.clone();
move |_| value.with(|value_date| value_date.as_ref() == Some(date.deref())) move |_| value.with(|value_date| value_date.as_ref() == Some(date.deref()))
}); });

View file

@ -1,26 +0,0 @@
use crate::theme::ThemeMethod;
#[derive(Clone)]
pub struct CalendarTheme {
pub border_color: String,
pub other_month_font_color: String,
pub background_color_hover: String,
}
impl ThemeMethod for CalendarTheme {
fn light() -> Self {
Self {
border_color: "#efeff5".into(),
other_month_font_color: "#c2c2c2".into(),
background_color_hover: "#f3f3f5".into(),
}
}
fn dark() -> Self {
Self {
border_color: "#2d2d30".into(),
other_month_font_color: "#ffffff61".into(),
background_color_hover: "#2d2d30".into(),
}
}
}

View file

@ -2,10 +2,7 @@ mod color;
mod common; mod common;
use self::common::CommonTheme; use self::common::CommonTheme;
use crate::{ use crate::{AlertTheme, AnchorTheme, MessageTheme, ProgressTheme, ScrollbarTheme, SelectTheme};
AlertTheme, AnchorTheme, CalendarTheme, MessageTheme, ProgressTheme, ScrollbarTheme,
SelectTheme,
};
pub use color::ColorTheme; pub use color::ColorTheme;
use leptos::*; use leptos::*;
@ -23,7 +20,6 @@ pub struct Theme {
pub message: MessageTheme, pub message: MessageTheme,
pub select: SelectTheme, pub select: SelectTheme,
pub progress: ProgressTheme, pub progress: ProgressTheme,
pub calendar: CalendarTheme,
pub scrollbar: ScrollbarTheme, pub scrollbar: ScrollbarTheme,
pub anchor: AnchorTheme, pub anchor: AnchorTheme,
} }
@ -37,9 +33,7 @@ impl Theme {
alert: AlertTheme::light(), alert: AlertTheme::light(),
message: MessageTheme::light(), message: MessageTheme::light(),
select: SelectTheme::light(), select: SelectTheme::light(),
progress: ProgressTheme::light(), progress: ProgressTheme::light(),
calendar: CalendarTheme::light(),
scrollbar: ScrollbarTheme::light(), scrollbar: ScrollbarTheme::light(),
anchor: AnchorTheme::light(), anchor: AnchorTheme::light(),
} }
@ -53,7 +47,6 @@ impl Theme {
message: MessageTheme::dark(), message: MessageTheme::dark(),
select: SelectTheme::dark(), select: SelectTheme::dark(),
progress: ProgressTheme::dark(), progress: ProgressTheme::dark(),
calendar: CalendarTheme::dark(),
scrollbar: ScrollbarTheme::dark(), scrollbar: ScrollbarTheme::dark(),
anchor: AnchorTheme::dark(), anchor: AnchorTheme::dark(),
} }