MudFileUpload: Improve validation in MudForm#7720
Conversation
Builds upon MudBlazor#7578 to flesh out form validation on `MudFileUpload` by allowing users to clear a `MudFileUpload` instance by setting `Files` to `null`. This would previously not trigger form validation or it would not mark the `MudFileUpload` instance as `Touched`.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## dev #7720 +/- ##
==========================================
+ Coverage 85.49% 87.17% +1.68%
==========================================
Files 427 389 -38
Lines 15267 11411 -3856
Branches 3329 2297 -1032
==========================================
- Hits 13052 9948 -3104
+ Misses 1536 966 -570
+ Partials 679 497 -182
☔ View full report in Codecov by Sentry. |
|
Adding @Mr-Technician, but I'm not really liking that we are adding an async call here in components parameter. |
|
@ScarletKuro I was looking at this last night and had the same thought. I don't want to add an async call in the setter. @igotinfected How about implementing a Clear button? This is how other Form components do it. |
|
@Mr-Technician @ScarletKuro Fully agree with both of you guys, this was the quick and dirty version :) I think for a clean <ButtonsTemplate>
<Progress Value="@filePickerCustom.GetProgressPercentage()" />
<Buttons>
<Button Clicked="@context.Clear" Color="Color.Warning"><Icon Name="IconName.Clear" /></Button>
<Button Clicked="@context.Upload" Color="Color.Primary"><Icon Name="IconName.FileUpload" /></Button>
</Buttons>
</ButtonsTemplate>We would then have something akin to: public RenderFragment<FileUploadButtonTemplateContext> ButtonTemplate { get; set; }Where public class FileUploadButtonTemplateContext
{
public string Id { get; set; }
public EventCallback ClearAsync { get; set; }
}Or potentially, based on the existing MudBlazor public class FileUploadButtonTemplateContext<T>
{
public string Id { get; }
public FileUploadButtonTemplateActions Actions { get; }
private readonly MudFileUpload<T> _fileUpload;
public FileUploadButtonTemplateContext(MudFileUpload<T> fileUpload, string id)
{
_fileUpload = fileUpload;
Id = id;
Actions = new FileUploadButtonTemplateActions
{
ClearAsync = async () => await _fileUpload.ClearAsync(),
};
}
public class FileUploadButtonTemplateActions
{
public Func<Task> ClearAsync { get; init; } = null!;
}
}Thoughts? |
|
I was thinking we would add a ClearAsync method to the file upload component and not modify the context. It could still be cleared with a button using a reference to the component. This is potentially less clean but isn't breaking. |
|
Having looked at this more closely on my computer, we might be able to get around the breaking change by add a I otherwise agree that using |
|
@Mr-Technician Thank you for the feedback! The I've gone ahead and implemented the After adapting the snippet in the original post to use the new context, this is the result: example.mp4Working as expected :) For regressions, there is already a test verifying the old I'm also fine with going for the |
|
lgtm pending the new tests and docs example. @ScarletKuro @henon Are both of you ok with merging this instead of waiting for v7? It's technically breaking but existing code will still work unless someone is doing something unusual. "Normal" usage of the button context will still work the same as it did before without modifying code due to the |
|
Updated all Added a render test, and two Updated documentation and added a second drag and drop example with a form using the new Let me know if you have different ideas for documentation, can also add a recording of the new example if you need :) |
Personally I'm ok with this. |
henon
left a comment
There was a problem hiding this comment.
LGTM, leaving final approval and merge to @Mr-Technician.
|
I'll do a final review and (hopefully) merge tomorrow afternoon. 👍 |
* MudFileUpload: Allow clearing FileUpload by setting `Files` to null Builds upon MudBlazor#7578 to flesh out form validation on `MudFileUpload` by allowing users to clear a `MudFileUpload` instance by setting `Files` to `null`. This would previously not trigger form validation or it would not mark the `MudFileUpload` instance as `Touched`. * WIP `ClearAsync` + `FileUploadButtonTemplateContext` implementations * Add/Update documentation and tests
Builds upon #7578 to flesh out form validation with
MudFileUpload, allowing users to clear aMudFileUploadinstance by settingFilestonull.Description
We have a form where users can upload images for a specific model. When editing an existing item, if all the user intends to do is clear the existing image on the form page, the
MudForminstance will not be marked asTouched.Visually we have a
Clearbutton that programmatically setsFilestonull, very similar to the drag and drop example in the docs.Here's a snippet -- pressing the clear button will remove the file but not trigger
IsTouched = true.Unsure if the proposed solution makes sense, though I somewhat based it on the
MudDatePickerimplementation.How Has This Been Tested?
bUnit tested
Types of changes
Checklist:
dev).