Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix(Button): ignore the exceptions thrown druing pre-rendering #2116

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/Masa.Blazor/Extensions/ElementReferenceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ namespace Masa.Blazor.Extensions;

public static class ElementReferenceExtensions
{
public static string? GetSelector(this ElementReference? elementReference)
{
return elementReference?.GetSelector();
}

public static string GetSelector(this ElementReference elementReference)
{
if (elementReference.TryGetSelector(out var selector))
{
return selector;
}

throw new InvalidOperationException($"ElementReference {elementReference.Id} does not have a selector.");
throw new InvalidOperationException(
"ElementReference has not been configured correctly. This issue may occur during pre-rendering; it can be ignored, or you can disable pre-rendering.");
}

public static bool TryGetSelector(this ElementReference elementReference, [NotNullWhen(true)] out string? selector)
Expand Down
47 changes: 30 additions & 17 deletions src/Masa.Blazor/Extensions/JsRuntimeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ public static async Task AddHtmlElementEventListener(this IJSRuntime jsRuntime,
Func<Task> callback,
OneOf<EventListenerOptions, bool> options, EventListenerExtras? extras = null)
{
await jsRuntime.InvokeVoidAsync(JsInteropConstants.AddHtmlElementEventListener,
el.GetSelector(),
type,
DotNetObjectReference.Create(new Invoker(callback)),
options.Value,
extras
);
if (el.TryGetSelector(out var selector))
{
await jsRuntime.InvokeVoidAsync(JsInteropConstants.AddHtmlElementEventListener,
selector,
type,
DotNetObjectReference.Create(new Invoker(callback)),
options.Value,
extras
);
}
}

public static async Task AddHtmlElementEventListener<TEventArgs>(this IJSRuntime jsRuntime, string selector, string type,
Expand All @@ -57,17 +60,21 @@ await jsRuntime.InvokeVoidAsync(JsInteropConstants.AddHtmlElementEventListener,
);
}

public static async Task AddHtmlElementEventListener<TEventArgs>(this IJSRuntime jsRuntime, ElementReference el, string type,
public static async Task AddHtmlElementEventListener<TEventArgs>(this IJSRuntime jsRuntime, ElementReference el,
string type,
Func<TEventArgs, Task> callback,
OneOf<EventListenerOptions, bool> options, EventListenerExtras? extras = null)
{
await jsRuntime.InvokeVoidAsync(JsInteropConstants.AddHtmlElementEventListener,
el.GetSelector(),
type,
DotNetObjectReference.Create(new Invoker<TEventArgs>(callback)),
options.Value,
extras
);
if (el.TryGetSelector(out var selector))
{
await jsRuntime.InvokeVoidAsync(JsInteropConstants.AddHtmlElementEventListener,
selector,
type,
DotNetObjectReference.Create(new Invoker<TEventArgs>(callback)),
options.Value,
extras
);
}
}

public static async Task AddClickEventListener(this IJSRuntime jsRuntime, ElementReference elementReference, Func<MouseEventArgs, Task> callback, bool stopPropagation, bool preventDefault)
Expand All @@ -84,12 +91,18 @@ public static async Task RemoveHtmlElementEventListener(this IJSRuntime jsRuntim
public static async Task RemoveHtmlElementEventListener(this IJSRuntime jsRuntime, ElementReference el, string type,
string? key = null)
{
await jsRuntime.InvokeVoidAsync(JsInteropConstants.RemoveHtmlElementEventListener, el.GetSelector(), type, key);
if (el.TryGetSelector(out var selector))
{
await jsRuntime.InvokeVoidAsync(JsInteropConstants.RemoveHtmlElementEventListener, selector, type, key);
}
}

public static async Task RemoveClickEventListener(this IJSRuntime jSRuntime, ElementReference el, string? key = null)
{
await jSRuntime.RemoveHtmlElementEventListener(el.GetSelector(), "click", key);
if (el.TryGetSelector(out var selector))
{
await jSRuntime.RemoveHtmlElementEventListener(selector, "click", key);
}
}

public static async Task ScrollTo(this IJSRuntime jsRuntime, string selector, double? top, double? left = null,
Expand Down
Loading