gpui-component/crates/story/src/calendar_story.rs
obito 7487662343
chrore: Refactor the disabled API for NumberInput, DatePicker, OtpInput, Calandar. (#1102)
- Added `disabled` method to OtpInput.
- Added `disabled` method to DatePicker.
- Added `disabled_matcher` method to CalendarState.

## Break Changes

- The `InputState` has been removed `disabled` and `set_disabled`
method, the disabled state should assign from TextInput element.

```diff
- let state = InputState::new("input1").disabled(true)
+ let state = InputState::new("input1");
+ TextInput::new(&state).disabled(true)
```

- Renamed `set_disabled` method to `set_disabled_matcher` from
`CalendarState`.
- Removed `set_disabled` method from `DatePickerState`, use
`disabled_matcher` method in `DatePicker` element.

---------

Co-authored-by: Jason Lee <huacnlee@gmail.com>
2025-07-29 17:31:52 +08:00

79 lines
2.2 KiB
Rust

use gpui::{
App, AppContext, Context, Entity, FocusHandle, Focusable, IntoElement, ParentElement as _,
Render, Styled as _, Window,
};
use gpui_component::{
calendar::{Calendar, CalendarState},
v_flex,
};
use crate::section;
pub struct CalendarStory {
focus_handle: FocusHandle,
calendar: Entity<CalendarState>,
calendar_wide: Entity<CalendarState>,
calendar_with_disabled_matcher: Entity<CalendarState>,
}
impl super::Story for CalendarStory {
fn title() -> &'static str {
"Calendar"
}
fn description() -> &'static str {
"A calendar to select a date or date range."
}
fn new_view(window: &mut Window, cx: &mut App) -> Entity<impl Render + Focusable> {
Self::view(window, cx)
}
}
impl CalendarStory {
pub fn view(window: &mut Window, cx: &mut App) -> Entity<Self> {
cx.new(|cx| Self::new(window, cx))
}
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let calendar = cx.new(|cx| CalendarState::new(window, cx));
let calendar_wide = cx.new(|cx| CalendarState::new(window, cx));
let calendar_with_disabled_matcher =
cx.new(|cx| CalendarState::new(window, cx).disabled_matcher(vec![0, 3, 6]));
Self {
calendar,
calendar_wide,
calendar_with_disabled_matcher,
focus_handle: cx.focus_handle(),
}
}
}
impl Focusable for CalendarStory {
fn focus_handle(&self, _: &App) -> FocusHandle {
self.focus_handle.clone()
}
}
impl Render for CalendarStory {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
v_flex()
.gap_3()
.child(
section("Normal")
.max_w_md()
.child(Calendar::new(&self.calendar)),
)
.child(
section("With 3 Months")
.max_w_md()
.child(Calendar::new(&self.calendar_wide).number_of_months(3)),
)
.child(
section("With Disabled matcher (Sundays, Wednesdays, Saturdays)")
.max_w_md()
.child(Calendar::new(&self.calendar_with_disabled_matcher)),
)
}
}