Skip to content

Commit cf44465

Browse files
docs(calendar): selection examples
1 parent 552f3af commit cf44465

File tree

2 files changed

+107
-49
lines changed

2 files changed

+107
-49
lines changed

components/calendar/events.md

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ This article explains the events available in the Telerik Calendar for Blazor:
1515
* [ValueChanged](#valuechanged)
1616
* [DateChanged](#datechanged)
1717
* [ViewChanged](#viewchanged)
18-
* [RangeStartChanged](#rangestartchanged)
19-
* [RangeEndChanged](#rangeendchanged)
18+
* [RangeStartChanged and RangeEndChanged](#rangestartchanged-and-rangeendchanged)
2019

2120
## ValueChanged
2221

@@ -78,31 +77,26 @@ When handling the `ViewChanged` event, you cannot use two-way binding for the `V
7877

7978
>tip You are not required to provide an initial value to the `View` parameter. It will default to `CalendarView.Month`.
8079
81-
## RangeStartChanged
80+
## RangeStartChanged and RangeEndChanged
8281

83-
The `RangeStartChanged` fires every time the user selects a new starting date for the range of dates when the Calendar is in `Range` selection mode. The first click on a date in the Calendar will always be the starting date and the range can be consisted of dates only onward.
82+
The two RangeChanged events (`RangeStartChanged` and `RangeEndChanged`) fire when the user selects a new range.
8483

85-
## RangeEndChanged
86-
87-
The `RangeEndChanged` fires after the `RangeStartChanged` with the `default` value of the model field, and every time when the selection of dates is finished.
84+
When the user selects a range from the calendar, the first click always fires the start change with the selected date, and then clears the end of the range, so the end change event fires as well, with the default value for `DateTime` - this indicates that the end of the range is undefined. If the second click is on a date before the range start - it will become the new start.
8885

8986
>caption Example of `Range` Selection with `RangeStartChanged` and `RangeEndChanged` events
9087
9188
````CSHTML
9289
@* Observe the behavior of the RangeStartChanged and RangeEndChanged events and adding the selected dates to a List *@
9390
94-
<div class="example-wrapper">
95-
<h4>Range Selection Initial Selection</h4>
91+
<TelerikCalendar Views="2"
92+
Date="@Date"
93+
SelectionMode="@CalendarSelectionMode.Range"
94+
RangeStart="@RangeStart"
95+
RangeEnd="@RangeEnd"
96+
RangeStartChanged="@StartChangeHandler"
97+
RangeEndChanged="@EndChangeHandler">
98+
</TelerikCalendar>
9699
97-
<TelerikCalendar Views="3"
98-
Date="@Date"
99-
RangeStart="@RangeStart"
100-
RangeEnd="@RangeEnd"
101-
SelectionMode="@CalendarSelectionMode.Range"
102-
RangeStartChanged="@StartChangeHandler"
103-
RangeEndChanged="@EndChangeHandler">
104-
</TelerikCalendar>
105-
</div>
106100
107101
@if (SelectedDates.Any())
108102
{
@@ -116,28 +110,40 @@ The `RangeEndChanged` fires after the `RangeStartChanged` with the `default` val
116110
}
117111
118112
@code {
119-
public DateTime Date { get; set; } = new DateTime(2020, 3, 1);
120-
public DateTime RangeStart { get; set; } = new DateTime(2020, 3, 14);
121-
public DateTime RangeEnd { get; set; } = new DateTime(2020, 3, 18);
122-
public List<DateTime> SelectedDates { get; set; } = new List<DateTime>();
113+
DateTime Date { get; set; } = DateTime.Now.AddDays(-5);
114+
DateTime RangeStart { get; set; } = DateTime.Now;
115+
DateTime RangeEnd { get; set; } = DateTime.Now.AddDays(6);
116+
List<DateTime> SelectedDates { get; set; } = new List<DateTime>();
123117
124-
public void StartChangeHandler(DateTime startDate)
118+
void StartChangeHandler(DateTime startDate)
125119
{
126120
// you have to update the model manually because handling the <Parameter>Changed event does not let you use @bind-<Parameter>
127121
// not updating the model will effectively cancel the event
128122
RangeStart = startDate;
129-
GetDates();
123+
124+
Console.WriteLine($"The user started making a new selection from {startDate}");
125+
126+
RenderDateRange();
130127
}
131128
132-
public void EndChangeHandler(DateTime endDate)
129+
void EndChangeHandler(DateTime endDate)
133130
{
134131
// you have to update the model manually because handling the <Parameter>Changed event does not let you use @bind-<Parameter>
135132
// not updating the model will effectively cancel the event
136133
RangeEnd = endDate;
137-
GetDates();
134+
135+
136+
// sample check to execute logic only after the user has selected both ends of the range
137+
// if this does not pass, the user has only clicked once in the calendar popup
138+
if (endDate != default(DateTime))
139+
{
140+
Console.WriteLine($"The user finished making a new selection from {RangeStart} to {endDate}");
141+
}
142+
143+
RenderDateRange();
138144
}
139145
140-
public void GetDates()
146+
void RenderDateRange()
141147
{
142148
var datesInBetween = Enumerable.Range(0, 1 + RangeEnd.Subtract(RangeStart).Days)
143149
.Select(offset => RangeStart.AddDays(offset))

components/calendar/selection.md

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,71 @@ position: 2
1212

1313
The user can select one or mode dates depending on the Calendar configuration set by the developer. They may also be forbidden selection of certain disabled dates.
1414

15+
This article contains the following sections:
16+
17+
* [Selection Mode](#selection-mode)
18+
* [Receive User Selection](#receive-user-selection)
19+
* [Single Selection Mode](#single-selection-mode)
20+
* [Multiple Selection Mode](#multiple-selection-mode)
21+
* [Range Selection Mode](#range-selection-mode)
22+
* [Disabled Dates](#disabled-dates)
23+
1524
## Selection Mode
1625

1726
To control how many dates the user can select, use the `SelectionMode` property. It takes a member of the `Telerik.Blazor.CalendarSelectionMode` enum and can be:
1827
* `Single`
1928
* `Multiple`
2029
* `Range`
2130

22-
You can pre-select a date in Single selection mode by setting the `Value` property of the calendar to the desired date.
31+
You can pre-select a date in `Single` selection mode by setting the `Value` property of the calendar to the desired date.
2332

24-
To pre-select dates in the Multiple selection mode, use the `SelectedDates` property which is of type `List<DateTime>`.
33+
To pre-select dates in the `Multiple` selection mode, use the `SelectedDates` property which is of type `List<DateTime>`.
2534

26-
In `Range` selection mode you can get the start and end dates of a selected range, by the user, through the `RangeStart` and `RangeEnd` parameters of type `DateTime`. You also get events `RangeStartChanged` and `RangeEndChanged`. You can read more about them and see an example in the [Events]({%slug components/calendar/events%}) article.
35+
In `Range` selection mode you can get the start and end dates of the range the user selected through the `RangeStart` and `RangeEnd` parameters of type `DateTime`. You also get events `RangeStartChanged` and `RangeEndChanged`. You can read more about them and see an example in the [Events]({%slug components/calendar/events%}) article.
2736

2837
## Receive User Selection
2938

39+
The way you can get the user selection depends on the selection mode you use:
40+
41+
* [Single Selection Mode](#single-selection-mode)
42+
* [Multiple Selection Mode](#multiple-selection-mode)
43+
* [Range Selection Mode](#range-selection-mode)
44+
3045
### Single Selection Mode
3146

32-
Use the `ValueChanged` event. The handler receives a `DateTime` object as parameter which represents the new selected date.
47+
When using single selection mode, you can get the selected date through:
3348

34-
>caption Handle Single selection in the Calendar
49+
* two-way binding of the `Value` parameter
50+
* the `ValueChanged` event - the event handler receives a `DateTime` object as parameter which represents the new selected date.
51+
52+
You can find examples of both below.
53+
54+
>caption Two-way binding for the selected date
55+
56+
````CSHTML
57+
@* With single selection, you can use two-way binding of the selected date *@
58+
59+
<TelerikCalendar Date="@startDate"
60+
@bind-Value="@SelectedDate"
61+
SelectionMode="@CalendarSelectionMode.Single">
62+
</TelerikCalendar>
63+
64+
<p>
65+
Selected Date: @SelectedDate
66+
</p>
67+
68+
@code {
69+
private DateTime SelectedDate { get; set; } = DateTime.Now.Date;
70+
private DateTime startDate = DateTime.Now.Date;
71+
}
72+
````
73+
74+
>caption Handle Single selection in the Calendar through an event
3575
3676
````CSHTML
3777
@* This example shows how to handle Single selection *@
3878
3979
<TelerikCalendar Date="@startDate"
40-
View="CalendarView.Month"
4180
SelectionMode="@CalendarSelectionMode.Single"
4281
ValueChanged="SelectionHandler">
4382
</TelerikCalendar>
@@ -49,7 +88,6 @@ Use the `ValueChanged` event. The handler receives a `DateTime` object as parame
4988
@code {
5089
private DateTime SelectedDate { get; set; }
5190
private DateTime startDate = new DateTime(2019, 5, 2);
52-
private TelerikCalendar CalendarRef { get; set; }
5391
5492
public void SelectionHandler(DateTime selectedDate)
5593
{
@@ -58,17 +96,17 @@ Use the `ValueChanged` event. The handler receives a `DateTime` object as parame
5896
}
5997
````
6098

99+
61100
### Multiple Selection Mode
62101

63-
Use the `SelectedDates` property of the component reference in the `ValueChanged` handler.
102+
With multiple selection mode, to get the user selection, use the `SelectedDates` property of the component reference in the `ValueChanged` handler.
64103

65104
>caption Handle Multiple selection in the Calendar
66105
67106
````CSHTML
68107
@* This example shows how to handle Multiple selection *@
69108
70109
<TelerikCalendar Date="@startDate"
71-
View="@CalendarView.Month"
72110
SelectionMode="@CalendarSelectionMode.Multiple"
73111
ValueChanged="@SelectionHandler"
74112
@ref="@CalendarRef">
@@ -99,36 +137,51 @@ Use the `SelectedDates` property of the component reference in the `ValueChanged
99137
````
100138
### Range Selection Mode
101139

102-
Use the `RangeStart`, representing the first date of the selection, and the `RangeEnd` - the last date of the selection.
140+
With range selection mode, you have two options to get the user choice:
141+
142+
* two-way binding for the `RangeStart` (representing the first date of the selection) and the `RangeEnd` (the last date of the selection) parameters.
143+
* Handling the [RangeStartChanged and RangeEndChanged events]({%slug components/calendar/events%}#rangestartchanged-and-rangeendchanged)
144+
103145

104-
>caption Handle Range selection
146+
>caption Range selection with two-way binding
105147
106148
````CSHTML
107-
@* This example shows how to handle Range selection *@
149+
@* This example shows how to handle Range selection through two-way binding *@
108150
109151
<TelerikCalendar Views="2"
110152
Date="@Date"
111-
RangeStart="@RangeStart"
112-
RangeEnd="@RangeEnd"
153+
@bind-RangeStart="@RangeStart"
154+
@bind-RangeEnd="@RangeEnd"
113155
SelectionMode="@CalendarSelectionMode.Range">
114156
</TelerikCalendar>
115157
158+
<p>
159+
Start: @RangeStart
160+
<br />
161+
End: @RangeEnd
162+
</p>
163+
116164
@code {
117-
public static DateTime dateTimeNow { get; set; } = DateTime.Now;
118-
public DateTime Date { get; set; } = dateTimeNow.AddDays(-5);
119-
public DateTime RangeStart { get; set; } = dateTimeNow;
120-
public DateTime RangeEnd { get; set; } = dateTimeNow.AddDays(15);
165+
public DateTime Date { get; set; } = DateTime.Now.AddDays(-5);
166+
public DateTime RangeStart { get; set; } = DateTime.Now.Date;
167+
public DateTime RangeEnd { get; set; } = DateTime.Now.AddDays(15).Date;
168+
169+
// the RangeEnd value will be the default(DateTime) while the user is selecting a range
170+
// that is, while they have clicked only once in the calendar
121171
}
122172
````
123173

124-
### Disabled Dates
174+
175+
## Disabled Dates
125176

126177
To prevent the user from selecting certain dates (for example, holidays), add those dates to the `DisabledDates` collection.
127178

128-
>caption Handle Disabled dates with Calendar Multiple selection
179+
With `Single` and `Multiple` selection, the user can't select these dates. With `Range` selection, these dates cannot be the start or end of a range, but can be included in the range.
180+
181+
>caption Add Disabled dates to a Calendar with Multiple selection
129182
130183
````CSHTML
131-
The user will not be able to select the first and second of April 2019.
184+
@* The user will not be able to select the first and second of April 2019. *@
132185
133186
<TelerikCalendar SelectionMode="@CalendarSelectionMode.Multiple"
134187
ValueChanged="@MultipleSelectionChangeHandler"
@@ -164,7 +217,6 @@ The user will not be able to select the first and second of April 2019.
164217
chosenDates = multipleSelCalendar.SelectedDates;
165218
}
166219
}
167-
168220
````
169221

170222
## See Also

0 commit comments

Comments
 (0)