Skip to content

Commit 6f5730f

Browse files
authored
kb(date-input):Select AM/PM by pressing A or P on keyboard (#265)
* kb(date-input):Select AM/PM by pressing A or P on keyboard * chore(dateinput - select-am-pm): minor fix Co-authored-by: Nadezhda Tacheva <[email protected]>
1 parent 82fa7da commit 6f5730f

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
title: Select AM/PM by pressing A or P on keyboard
3+
description: How to select AM/PM by pressing A or P on keyboard in TimePicker, DateTimePicker and DateInput
4+
type: how-to
5+
page_title: Select AM/PM by pressing A or P on keyboard
6+
slug: date-input-kb-select-am-pm-on-keypress
7+
position:
8+
tags:
9+
res_type: kb
10+
---
11+
12+
## Environment
13+
<table>
14+
<tbody>
15+
<tr>
16+
<td>Product</td>
17+
<td>TimePicker for Blazor, DateTimePicker for Blazor, DateInput for Blazor</td>
18+
</tr>
19+
</tbody>
20+
</table>
21+
22+
23+
## Description
24+
25+
When typing in time in the TimePicker/DateTimePicker (enter hours,enter mins, arrow up/down for AM/PM) it would be nice if I could select AM or PM by pressing A or P on the keyboard also.
26+
27+
When using the DateInput component and using format "hh:mm:ss tt" the UI part allows me to edit the hour minute seconds correctly, but the AM / PM is not editable by typing A or P. Only changes the AM/PM by using up arrow and down arrow on keyboard.
28+
29+
Any way to make the AM/PM change the way the hh mm ss does by directly entering A or P?
30+
31+
32+
## Solution
33+
34+
While A and P might be very common symbols, there are many other symbols that might be used in different countries and cultures. Thus, the component cannot track all such possible combinations, and it exposes the Up and Down Arrows as options to change the time segments, including the AM/PM indicator.
35+
36+
In order to change AM/PM by pressing A or P on keyboard you can get the input events (such as keydown) by capturing them as they bubble up the DOM in a parent element, as described in this article - [Capture input keyboard events]({%slug inputs-kb-handle-keyboard-events%}).
37+
38+
If you detect an A or P key you could change the Value as desired (e.g., add or subtract 12 hours depending on the current hours).
39+
40+
41+
>caption TimePicker - Select AM/PM by pressing A or P on keyboard
42+
43+
````CSHTML
44+
@* Press A or P on keyboard to select AM or PM*@
45+
46+
@TheTime
47+
<br />
48+
49+
<span @onkeydown="@OnKeyDownHandler">
50+
<TelerikTimePicker Format="hh:mm:ss tt" @bind-Value="@TheTime"></TelerikTimePicker>
51+
</span>
52+
@code{
53+
DateTime TheTime { get; set; } = DateTime.Now;
54+
55+
async Task OnKeyDownHandler(KeyboardEventArgs e)
56+
{
57+
if (e.Key.ToLowerInvariant() == "p" && TheTime.Hour < 12)
58+
{
59+
await Task.Delay(20);
60+
TheTime = TheTime.AddHours(12);
61+
}
62+
if (e.Key.ToLowerInvariant() == "a" && TheTime.Hour > 12)
63+
{
64+
await Task.Delay(20);
65+
TheTime = TheTime.AddHours(-12);
66+
}
67+
}
68+
}
69+
````
70+
71+
72+
73+
>caption DateTimePicker - Select AM/PM by pressing A or P on keyboard
74+
75+
````CSHTML
76+
@* Press A or P on keyboard to select AM or PM*@
77+
78+
@TheDateTime
79+
<br />
80+
81+
<span @onkeydown="@OnKeyDownHandler">
82+
<TelerikDateTimePicker Width="210px" Format="dd MMM yyyy hh:mm:ss tt" @bind-Value="@TheDateTime"></TelerikDateTimePicker>
83+
</span>
84+
@code{
85+
DateTime TheDateTime { get; set; } = DateTime.Now;
86+
87+
async Task OnKeyDownHandler(KeyboardEventArgs e)
88+
{
89+
if (e.Key.ToLowerInvariant() == "p" && TheDateTime.Hour < 12)
90+
{
91+
await Task.Delay(20);
92+
TheDateTime = TheDateTime.AddHours(12);
93+
}
94+
if (e.Key.ToLowerInvariant() == "a" && TheDateTime.Hour > 12)
95+
{
96+
await Task.Delay(20);
97+
TheDateTime = TheDateTime.AddHours(-12);
98+
}
99+
}
100+
}
101+
````
102+
103+
104+
105+
>caption DateInput - Select AM/PM by pressing A or P on keyboard
106+
107+
````CSHTML
108+
@* Press A or P on keyboard to select AM or PM*@
109+
110+
@TheTime
111+
<br />
112+
113+
<span @onkeydown="@OnKeyDownHandler">
114+
<TelerikDateInput Format="hh:mm:ss tt" @bind-Value="@TheTime"></TelerikDateInput>
115+
</span>
116+
@code{
117+
DateTime TheTime { get; set; } = DateTime.Now;
118+
119+
async Task OnKeyDownHandler(KeyboardEventArgs e)
120+
{
121+
if (e.Key.ToLowerInvariant() == "p" && TheTime.Hour < 12)
122+
{
123+
await Task.Delay(20);
124+
TheTime = TheTime.AddHours(12);
125+
}
126+
if (e.Key.ToLowerInvariant() == "a" && TheTime.Hour > 12)
127+
{
128+
await Task.Delay(20);
129+
TheTime = TheTime.AddHours(-12);
130+
}
131+
}
132+
}
133+
````
134+

0 commit comments

Comments
 (0)