// Copyright (c) MudBlazor 2021 // MudBlazor licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using Connected.Annotations; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms; using Microsoft.Extensions.Logging; using Connected.Utilities; namespace Connected.Components; public partial class FileUpload : FormComponent { public FileUpload() : base(new DefaultConverter()) { } private readonly string _id = $"mud_fileupload_{Guid.NewGuid()}"; protected string Classname => new CssBuilder("mud-file-upload") .AddClass(Class) .Build(); /// /// The value of the MudFileUpload component. /// If T is IBrowserFile, it represents a single file. /// If T is IReadOnlyList<IBrowserFile>, it represents multiple files /// [Parameter] [Category(CategoryTypes.FileUpload.Behavior)] public T Files { get => _value; set { if (_value != null && _value.Equals(value)) return; _value = value; } } /// /// Triggered when the internal OnChange event fires /// [Parameter] [Category(CategoryTypes.FileUpload.Behavior)] public EventCallback FilesChanged { get; set; } /// /// Called when the internal files are changed /// [Parameter] [Category(CategoryTypes.FileUpload.Behavior)] public EventCallback OnFilesChanged { get; set; } /// /// Renders the button that triggers the input. Required for functioning. /// [Parameter] [Category(CategoryTypes.FileUpload.Appearance)] public RenderFragment ButtonTemplate { get; set; } /// /// Renders the selected files, if desired. /// [Parameter] [Category(CategoryTypes.FileUpload.Appearance)] public RenderFragment SelectedTemplate { get; set; } /// /// If true, OnFilesChanged will not trigger if validation fails /// [Parameter] [Category(CategoryTypes.FileUpload.Behavior)] public bool SuppressOnChangeWhenInvalid { get; set; } /// /// Sets the file types this input will accept /// [Parameter] [Category(CategoryTypes.FileUpload.Behavior)] public string Accept { get; set; } /// /// If false, the inner FileInput will be visible /// [Parameter] [Category(CategoryTypes.FileUpload.Appearance)] public bool Hidden { get; set; } = true; /// /// Css classes to apply to the internal InputFile /// [Parameter] [Category(CategoryTypes.FileUpload.Appearance)] public string InputClass { get; set; } /// /// Style to apply to the internal InputFile /// [Parameter] [Category(CategoryTypes.FileUpload.Appearance)] public string InputStyle { get; set; } private async Task OnChange(InputFileChangeEventArgs args) { if (typeof(T) == typeof(IReadOnlyList)) { _value = (T)args.GetMultipleFiles(); } else if (typeof(T) == typeof(IBrowserFile)) { _value = (T)args.File; } else return; await FilesChanged.InvokeAsync(_value); BeginValidate(); FieldChanged(_value); if (!Error || !SuppressOnChangeWhenInvalid) //only trigger FilesChanged if validation passes or SuppressOnChangeWhenInvalid is false await OnFilesChanged.InvokeAsync(args); } protected override void OnInitialized() { if (!(typeof(T) == typeof(IReadOnlyList) || typeof(T) == typeof(IBrowserFile))) Logger.LogWarning("T must be of type {type1} or {type2}", typeof(IReadOnlyList), typeof(IBrowserFile)); base.OnInitialized(); } }