Skip to content

Commit

Permalink
#791 [HxInputDate] [HxInputDateRange] FocusAsync() not working
Browse files Browse the repository at this point in the history
  • Loading branch information
hakenr committed Apr 16, 2024
1 parent b205c19 commit 83ed753
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 1 deletion.
19 changes: 19 additions & 0 deletions BlazorAppTest/Pages/HxInputDateRange_Issue791_Test.razor
@@ -0,0 +1,19 @@
@page "/HxInputDateRange_Issue791_Test"
@using Havit

<h3>HxInputDateRange_Issue791_Test</h3>

<HxInputDateRange Label="HxInputDateRange" @bind-Value="_value" @ref="_component" />

@code {
private DateTimeRange _value;
private HxInputDateRange _component;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await _component.FocusAsync();
}
}
}
18 changes: 18 additions & 0 deletions BlazorAppTest/Pages/HxInputDate_Issue791_Test.razor
@@ -0,0 +1,18 @@
@page "/HxInputDate_Issue791_Test"

<h3>HxInputDate_Issue791_Test</h3>

<HxInputDate Label="HxInputDate" @bind-Value="_value" @ref="_component" />

@code {
private DateTime _value;
private HxInputDate<DateTime> _component;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await _component.FocusAsync();
}
}
}
2 changes: 1 addition & 1 deletion Havit.Blazor.Components.Web.Bootstrap/Forms/HxInputBase.cs
Expand Up @@ -433,7 +433,7 @@ public virtual async ValueTask FocusAsync()
{
if (EqualityComparer<ElementReference>.Default.Equals(InputElement, default))
{
throw new InvalidOperationException($"Cannot focus {GetType()}. The method must be called after first render.");
throw new InvalidOperationException($"Unable to focus {GetType().Name}, {nameof(InputElement)} reference not available (You are most likely calling the method too early, first render has to complete first.)");
}
await InputElement.FocusAsync();
}
Expand Down
16 changes: 16 additions & 0 deletions Havit.Blazor.Components.Web.Bootstrap/Forms/HxInputDate.cs
Expand Up @@ -143,6 +143,11 @@ public class HxInputDate<TValue> : HxInputBase<TValue>, IInputWithPlaceholder, I
[Parameter] public DateTime CalendarDisplayMonth { get; set; }

[Inject] private IStringLocalizer<HxInputDate> StringLocalizer { get; set; }


private HxInputDateInternal<TValue> _hxInputDateInternalComponent;


public HxInputDate()
{
Type underlyingType = Nullable.GetUnderlyingType(typeof(TValue)) ?? typeof(TValue);
Expand Down Expand Up @@ -193,10 +198,21 @@ protected virtual void BuildRenderInputCore(RenderTreeBuilder builder)
builder.AddAttribute(219, nameof(HxInputDateInternal<TValue>.CalendarDisplayMonth), CalendarDisplayMonth);

builder.AddMultipleAttributes(300, AdditionalAttributes);
builder.AddComponentReferenceCapture(301, (__value) => _hxInputDateInternalComponent = (HxInputDateInternal<TValue>)__value);

builder.CloseComponent();
}

public override ValueTask FocusAsync()
{
if (_hxInputDateInternalComponent is null)
{
throw new InvalidOperationException($"Unable to focus {nameof(HxInputDate)}, component reference not available (You are most likely calling the method too early, first render has to complete first.)");
}

return _hxInputDateInternalComponent.FocusAsync();
}

// For generating chips
/// <inheritdocs />
protected override string FormatValueAsString(TValue value) => FormatValue(value);
Expand Down
14 changes: 14 additions & 0 deletions Havit.Blazor.Components.Web.Bootstrap/Forms/HxInputDateRange.cs
Expand Up @@ -132,6 +132,9 @@ static HxInputDateRange()

[Inject] private IStringLocalizer<HxInputDateRange> StringLocalizer { get; set; }


private HxInputDateRangeInternal _hxInputDateRangeInternalComponent;

protected override void BuildRenderInput(RenderTreeBuilder builder)
{
RenderWithAutoCreatedEditContextAsCascadingValue(builder, 0, BuildRenderInputCore);
Expand Down Expand Up @@ -164,10 +167,21 @@ protected virtual void BuildRenderInputCore(RenderTreeBuilder builder)
builder.AddAttribute(221, nameof(HxInputDateRangeInternal.TimeProviderEffective), TimeProviderEffective);

builder.AddMultipleAttributes(300, AdditionalAttributes);
builder.AddComponentReferenceCapture(400, (__value) => _hxInputDateRangeInternalComponent = (HxInputDateRangeInternal)__value);

builder.CloseComponent();
}

public override ValueTask FocusAsync()
{
if (_hxInputDateRangeInternalComponent is null)
{
throw new InvalidOperationException($"Unable to focus {nameof(HxInputDateRange)}, component reference not available (You are most likely calling the method too early, first render has to complete first.)");
}

return _hxInputDateRangeInternalComponent.FocusAsync();
}

protected override void BuildRenderValidationMessage(RenderTreeBuilder builder)
{
// NOOP
Expand Down
Expand Up @@ -166,6 +166,11 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
}
}

public async ValueTask FocusAsync()
{
await _hxDropdownToggleElement.ElementReference.FocusAsync();
}

private CalendarDateCustomizationResult GetCalendarDateCustomization(CalendarDateCustomizationRequest request)
{
return CalendarDateCustomizationProviderEffective?.Invoke(request with { Target = CalendarDateCustomizationTarget.InputDate }) ?? null;
Expand Down
Expand Up @@ -120,6 +120,11 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
}
}

public async ValueTask FocusAsync()
{
await _fromDropdownToggleElement.ElementReference.FocusAsync();
}

protected override bool TryParseValueFromString(string value, out DateTimeRange result, out string validationErrorMessage)
{
throw new NotSupportedException();
Expand Down

0 comments on commit 83ed753

Please sign in to comment.