Skip to content

Commit 9b88706

Browse files
Add Parameter to can cancel the click on the Inputfile with some code when you click on the Inputfile
1 parent 9bba8e8 commit 9b88706

6 files changed

Lines changed: 40 additions & 16 deletions

File tree

BlazorBasics.InputFileExtended/Events.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,14 @@ public partial class InputFileComponent
1111
/// <returns></returns>
1212
async Task LoadFileEventsScript()
1313
{
14-
if (Parameters.AllowPasteFiles)
14+
try
1515
{
16-
17-
try
18-
{
19-
FileEventScriptsReference = await GetJSObjectReference("file-dialog");
20-
}
21-
catch (Exception ex)
22-
{
23-
FileEventScriptsReference = null;
24-
Console.WriteLine(ex.Message);
25-
}
16+
FileEventScriptsReference = await GetJSObjectReference("file-events");
17+
}
18+
catch (Exception ex)
19+
{
20+
FileEventScriptsReference = null;
21+
Console.WriteLine(ex.Message);
2622
}
2723
}
2824

BlazorBasics.InputFileExtended/InputFileComponent.razor

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
for="@InputFileId"
99
@ondragenter="DragEnter"
1010
@ondrop="DragLeave"
11-
@ondragleave="DragLeave">
11+
@ondragleave="DragLeave"
12+
@onclick=OnClick
13+
>
1214
<InputFile multiple="@Parameters.MultiFile" class="@Parameters.InputFileCss"
1315
accept="@InputFileTypes"
1416
id="@InputFileId"

BlazorBasics.InputFileExtended/InputFileComponent.razor.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Microsoft.AspNetCore.Components.Web;
2+
13
namespace BlazorBasics.InputFileExtended;
24

35
public partial class InputFileComponent
@@ -55,4 +57,13 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
5557
await LoadDragAdnDropScripts();
5658
}
5759
}
60+
61+
async Task OnClick()
62+
{
63+
if (Parameters.OnShouldCancelClick is not null)
64+
{
65+
bool cancel = await Parameters.OnShouldCancelClick.Invoke();
66+
await FileEventScriptsReference.InvokeVoidAsync("PreventDefault", InputFileId, cancel);
67+
}
68+
}
5869
}

BlazorBasics.InputFileExtended/ValueObjects/InputFileParameters.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,10 @@ public class InputFileParameters
5353
/// </summary>
5454
public DragAndDropOptions DragAndDropOptions { get; set; } = new();
5555

56+
/// <summary>
57+
/// Delegate to execute some code before execute the click. Return true if want to cancel the click event
58+
/// </summary>
59+
public Func<Task<bool>> OnShouldCancelClick;
60+
5661

5762
}

BlazorBasics.InputFileExtended/wwwroot/js/file-dialog.js

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const OpenDialog = (elementId) => {
2+
document.getElementById(elementId).click();
3+
}
4+
5+
const PreventDefault = (elementId, result) => {
6+
const element = document.getElementById(elementId);
7+
if (element && result) {
8+
element.addEventListener('click', function (event) {
9+
event.preventDefault();
10+
event.stopPropagation();
11+
}, { once: true });
12+
}
13+
}
14+
export { OpenDialog, PreventDefault }

0 commit comments

Comments
 (0)