From bfdfd7ad53be4f53fc94fd0a31d5cebf52a908aa Mon Sep 17 00:00:00 2001
From: NamelessGod <47334467+NamelessG0d@users.noreply.github.com>
Date: Fri, 10 Nov 2023 10:16:39 +0100
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20Reset=20method?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* Reset turnstile without reloading razor component
---
samples/BlazorWebAssembly/Pages/Index.razor | 15 ++++++++++++++-
src/BlazorTurnstile/Interop.cs | 6 ++++++
src/BlazorTurnstile/Turnstile.razor | 11 +++++++++++
src/BlazorTurnstile/wwwroot/interop.js | 4 ++++
4 files changed, 35 insertions(+), 1 deletion(-)
diff --git a/samples/BlazorWebAssembly/Pages/Index.razor b/samples/BlazorWebAssembly/Pages/Index.razor
index 623d9ba..bb6f4ae 100644
--- a/samples/BlazorWebAssembly/Pages/Index.razor
+++ b/samples/BlazorWebAssembly/Pages/Index.razor
@@ -10,7 +10,12 @@
Theme="@TurnstileTheme.light"
OnCallback="@TurnstileCallback"
OnErrorCallback="@TurnstileErrorCallback"
- ResponseField="@false" />
+ ResponseField="@false"
+ @ref="turnstile"/>
+
+
Token
@if (_turnstileToken != null)
@@ -23,6 +28,8 @@ else
}
@code {
+ private Turnstile turnstile = default!;
+
private string? _turnstileToken;
private void TurnstileCallback(string token)
@@ -34,4 +41,10 @@ else
{
Console.WriteLine($"Turnstile Error: {message}");
}
+
+ private async void ResetTurnstile()
+ {
+ await turnstile.ResetAsync();
+ }
+
}
diff --git a/src/BlazorTurnstile/Interop.cs b/src/BlazorTurnstile/Interop.cs
index e46c77b..7da51e6 100644
--- a/src/BlazorTurnstile/Interop.cs
+++ b/src/BlazorTurnstile/Interop.cs
@@ -22,6 +22,12 @@ public async ValueTask RenderAsync(
await module.InvokeVoidAsync("render", component, widgetElement, parameters);
}
+ public async ValueTask ResetAsync()
+ {
+ var module = await moduleTask.Value;
+ await module.InvokeVoidAsync("reset");
+ }
+
public async ValueTask DisposeAsync()
{
if (moduleTask.IsValueCreated)
diff --git a/src/BlazorTurnstile/Turnstile.razor b/src/BlazorTurnstile/Turnstile.razor
index f9c9b0c..f6cd44f 100644
--- a/src/BlazorTurnstile/Turnstile.razor
+++ b/src/BlazorTurnstile/Turnstile.razor
@@ -109,6 +109,17 @@
await OnErrorCallback.InvokeAsync(message);
}
+ public async Task ResetAsync()
+ {
+ if (_interop is not null)
+ {
+ Token = null;
+ await TokenChanged.InvokeAsync(Token);
+
+ await _interop.ResetAsync();
+ }
+ }
+
public async ValueTask DisposeAsync()
{
if (_interop is not null)
diff --git a/src/BlazorTurnstile/wwwroot/interop.js b/src/BlazorTurnstile/wwwroot/interop.js
index 57a5cdf..8894b9f 100644
--- a/src/BlazorTurnstile/wwwroot/interop.js
+++ b/src/BlazorTurnstile/wwwroot/interop.js
@@ -5,3 +5,7 @@ export function render(obj, element, parameters) {
turnstile.render(element, parameters);
}
+
+export function reset() {
+ turnstile.reset();
+}