Skip to content

Commit b889d3f

Browse files
committed
fix focus
1 parent c119b2f commit b889d3f

4 files changed

Lines changed: 19 additions & 52 deletions

File tree

Source/Blazorise/Components/DatePicker/DatePicker.razor.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,11 @@ private async ValueTask OpenCalendarAsync( bool focusCalendar )
680680
{
681681
NotifyCalendarStateChanged();
682682
await InvokeAsync( StateHasChanged );
683+
684+
if ( focusCalendar )
685+
{
686+
await JSUtilitiesModule.Focus( default, CalendarGridId, scrollToElement: false );
687+
}
683688
}
684689

685690
await SynchronizeOutsidePointerSubscriptionAsync();
@@ -1715,6 +1720,8 @@ protected override bool IsSameAsInternalValue( TValue value )
17151720

17161721
internal string CalendarId => $"{ElementId}-calendar";
17171722

1723+
internal string CalendarGridId => $"{CalendarId}-grid";
1724+
17181725
internal string PickerContainerId => $"{ElementId}-container";
17191726

17201727
internal DatePickerCalendarContext<TValue> CalendarContext

Source/Blazorise/Components/DatePicker/_DatePickerCalendar.razor

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127

128128
@if ( Parent.InputMode == DateInputMode.Month && Parent.CalendarView == DatePickerCalendarView.Month )
129129
{
130-
<div @ref="@CalendarGridElementRef"
130+
<div id="@Parent.CalendarGridId"
131131
class="@Calendar.MonthsClassNames"
132132
role="grid"
133133
aria-label="@Calendar.Label"
@@ -156,7 +156,7 @@
156156
}
157157
else if ( Parent.InputMode == DateInputMode.Month )
158158
{
159-
<div @ref="@CalendarGridElementRef"
159+
<div id="@Parent.CalendarGridId"
160160
class="@Calendar.MonthsClassNames"
161161
role="grid"
162162
aria-label="@Calendar.Label"
@@ -186,7 +186,7 @@
186186
}
187187
else
188188
{
189-
<div @ref="@CalendarGridElementRef"
189+
<div id="@Parent.CalendarGridId"
190190
class="@Calendar.GridClassNames"
191191
role="grid"
192192
aria-label="@Calendar.Label"
@@ -324,10 +324,6 @@
324324
}
325325

326326
@code {
327-
private bool focusApplied;
328-
329-
private ElementReference CalendarGridElementRef;
330-
331327
[CascadingParameter]
332328
public DatePicker<TValue> Parent { get; set; }
333329

@@ -342,26 +338,6 @@
342338
private void OnCalendarStateChanged()
343339
=> _ = InvokeAsync( StateHasChanged );
344340

345-
protected override async Task OnAfterRenderAsync( bool firstRender )
346-
{
347-
if ( Parent.CalendarVisible && !Parent.Inline )
348-
{
349-
if ( Parent.FocusCalendarOnOpen && !focusApplied )
350-
{
351-
focusApplied = true;
352-
await CalendarGridElementRef.FocusAsync( preventScroll: true );
353-
}
354-
else if ( !Parent.FocusCalendarOnOpen )
355-
{
356-
focusApplied = false;
357-
}
358-
}
359-
else
360-
{
361-
focusApplied = false;
362-
}
363-
}
364-
365341
public void Dispose()
366342
=> Parent.CalendarStateChanged -= OnCalendarStateChanged;
367343
}

Source/Blazorise/Components/TimePicker/TimePicker.razor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,12 @@ private async ValueTask OpenMenuAsync( bool focusMenu )
487487

488488
NotifyMenuStateChanged();
489489
await InvokeAsync( StateHasChanged );
490+
491+
if ( focusMenu )
492+
{
493+
await JSUtilitiesModule.Focus( default, MenuControlsId, scrollToElement: false );
494+
}
495+
490496
await SynchronizeOutsidePointerSubscriptionAsync();
491497
}
492498

@@ -1004,6 +1010,8 @@ protected string PickerContainerClassNames
10041010
/// </summary>
10051011
protected internal string MenuId => $"{ElementId}-menu";
10061012

1013+
internal string MenuControlsId => $"{MenuId}-controls";
1014+
10071015
internal string PickerContainerId => $"{ElementId}-container";
10081016

10091017
internal TimePickerMenuContext<TValue> MenuContext

Source/Blazorise/Components/TimePicker/_TimePickerMenu.razor

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
role="dialog"
1818
aria-modal="false"
1919
aria-label="@Menu.TimeText">
20-
<div @ref="@MenuControlsElementRef"
20+
<div id="@Parent.MenuControlsId"
2121
class="@Menu.ControlsClassNames"
2222
role="group"
2323
aria-label="@Menu.TimeText"
@@ -105,10 +105,6 @@
105105
}
106106

107107
@code {
108-
private bool focusApplied;
109-
110-
private ElementReference MenuControlsElementRef;
111-
112108
[CascadingParameter]
113109
public TimePicker<TValue> Parent { get; set; }
114110

@@ -123,26 +119,6 @@
123119
private void OnMenuStateChanged()
124120
=> _ = InvokeAsync( StateHasChanged );
125121

126-
protected override async Task OnAfterRenderAsync( bool firstRender )
127-
{
128-
if ( Parent.MenuVisible && !Parent.Inline )
129-
{
130-
if ( Parent.FocusMenuOnOpen && !focusApplied )
131-
{
132-
focusApplied = true;
133-
await MenuControlsElementRef.FocusAsync( preventScroll: true );
134-
}
135-
else if ( !Parent.FocusMenuOnOpen )
136-
{
137-
focusApplied = false;
138-
}
139-
}
140-
else
141-
{
142-
focusApplied = false;
143-
}
144-
}
145-
146122
public void Dispose()
147123
=> Parent.MenuStateChanged -= OnMenuStateChanged;
148124
}

0 commit comments

Comments
 (0)