Skip to content

Commit

Permalink
Merge pull request #23 from stavroskasidis/release/3.1.0
Browse files Browse the repository at this point in the history
Added `ForceAllowNavigation` and `ForcePreventNavigation` methods to …
  • Loading branch information
stavroskasidis authored Jun 25, 2024
2 parents 0f07475 + 4876eb4 commit ec927bd
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion BlazorDialog/BlazorDialog.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<Copyright />
<PackageTags>blazor blazor-component blazor-dialog dialog modal blazor-modal blazordialog blazormodaldialog blazormodal razor razor-components razorcomponents</PackageTags>
<VersionSuffix>$(VersionSuffix)</VersionSuffix>
<Version>3.0.0</Version>
<Version>3.1.0</Version>
<Version Condition=" '$(VersionSuffix)' != '' ">$(Version)-$(VersionSuffix)</Version>
<Product>BlazorDialog</Product>
</PropertyGroup>
Expand Down
22 changes: 22 additions & 0 deletions BlazorDialog/Components/Dialog.razor
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
/// </summary>
[Parameter] public bool PreventNavigation { get; set; } = true;

private bool? _forcedPreventNavigation;
protected TaskCompletionSource<object> taskCompletionSource;
internal Action<object> OnDialogHide;
protected void NotifyDialogHidden(object result) => OnDialogHide?.Invoke(result);
Expand Down Expand Up @@ -305,6 +306,27 @@
return this.isShowing;
}

internal bool GetPreventNavigation()
{
if (_forcedPreventNavigation.HasValue) return _forcedPreventNavigation.Value;
return this.PreventNavigation;
}

/// <summary>
/// Forces the dialog to allow navigation when shown, regardless of the <see cref="PreventNavigation" /> property.
/// </summary>
public void ForceAllowNavigation()
{
_forcedPreventNavigation = false;
}

/// <summary>
/// Forces the dialog to prevent navigation when shown, regardless of the <see cref="PreventNavigation" /> property.
/// </summary>
public void ForcePreventNavigation()
{
_forcedPreventNavigation = true;
}

protected override Task OnAfterRenderAsync(bool firstRender)
{
Expand Down
2 changes: 1 addition & 1 deletion BlazorDialog/LocationChangingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void RegisterLocationChangingHandler(Dialog dialog)

private ValueTask OnLocationChanging(LocationChangingContext context)
{
if (!context.IsNavigationIntercepted && _dialogsStack.Any(x => x.PreventNavigation))
if (!context.IsNavigationIntercepted && _dialogsStack.Any(x => x.GetPreventNavigation()))
{
context.PreventNavigation();
}
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,14 @@ Make sure that there is a call to `app.UseStaticFiles();` in your server project
</details>
## Release Notes
<details open="open"><summary>3.0</summary>

<details open="open"><summary>3.1</summary>

>- Added `ForceAllowNavigation` and `ForcePreventNavigation` methods to the `Dialog` component to allow/prevent navigation regardless of the `PreventNavigation` parameter.
</details>

<details><summary>3.0</summary>

>- Migrate to .NET 8.0
>- Add PreventNavigation option to prevent navigation when dialog is open.
Expand Down
9 changes: 8 additions & 1 deletion TestApps/BlazorDialog.TestAppsCommon/IndexCommon.razor
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<option value="@DialogAnimation.Zoom">@DialogAnimation.Zoom</option>
<option value="@DialogAnimation.FadeIn">@DialogAnimation.FadeIn</option>
</select>
<Dialog Id="simple-large-dialog" Size="size" Centered="isCentered" Animation="animation" CssClass="custom-class">
<Dialog Id="simple-large-dialog" Size="size" Centered="isCentered" Animation="animation" CssClass="custom-class" @ref="simpleLargeDialog">
<DialogInputProvider TInput="string">
<DialogHeader ShowClose="true">
<h4>@context.Input</h4>
Expand Down Expand Up @@ -128,6 +128,7 @@

<button @onclick="SimpleDialogOnClick">Simple Dialog</button>
<button @onclick="SimpleDialogBigOnClick">Simple Dialog Big</button>
<button @onclick="SimpleLargeDialogDisablePreventNavigation">Simple Dialog Big allow navigation</button>

@if (dialogResult != null)
{
Expand All @@ -139,6 +140,7 @@
bool isCentered;
DialogSize size;
DialogAnimation animation;
Dialog simpleLargeDialog;

async Task SimpleDialogOnClick()
{
Expand All @@ -149,4 +151,9 @@
{
dialogResult = await dialogService.ShowDialog<string>("simple-large-dialog", "(Simple Dialog Large) Are you sure?");
}

void SimpleLargeDialogDisablePreventNavigation()
{
simpleLargeDialog.ForceAllowNavigation();
}
}

0 comments on commit ec927bd

Please sign in to comment.