Skip to content

Commit

Permalink
✨ Add Reset method
Browse files Browse the repository at this point in the history
* Reset turnstile without reloading razor component
  • Loading branch information
NamelessG0d authored Nov 10, 2023
1 parent 242fa5b commit bfdfd7a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
15 changes: 14 additions & 1 deletion samples/BlazorWebAssembly/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
Theme="@TurnstileTheme.light"
OnCallback="@TurnstileCallback"
OnErrorCallback="@TurnstileErrorCallback"
ResponseField="@false" />
ResponseField="@false"
@ref="turnstile"/>

<button @onclick="ResetTurnstile">
Reset Turnstile
</button>

<h3 class="mt-3">Token</h3>
@if (_turnstileToken != null)
Expand All @@ -23,6 +28,8 @@ else
}

@code {
private Turnstile turnstile = default!;

private string? _turnstileToken;

private void TurnstileCallback(string token)
Expand All @@ -34,4 +41,10 @@ else
{
Console.WriteLine($"Turnstile Error: {message}");
}

private async void ResetTurnstile()
{
await turnstile.ResetAsync();
}

}
6 changes: 6 additions & 0 deletions src/BlazorTurnstile/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions src/BlazorTurnstile/Turnstile.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/BlazorTurnstile/wwwroot/interop.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ export function render(obj, element, parameters) {

turnstile.render(element, parameters);
}

export function reset() {
turnstile.reset();
}

0 comments on commit bfdfd7a

Please sign in to comment.