mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-14 08:51:27 +08:00
78abff551d
* feat(module: form): add form-context demo, support FormProvider * refactor: remove debug code Co-authored-by: James Yeung <shunjiey@hotmail.com>
75 lines
1.7 KiB
C#
75 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using AntDesign.Internal;
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
namespace AntDesign
|
|
{
|
|
public partial class FormProvider : AntComponentBase, IFormProvider
|
|
{
|
|
[Parameter]
|
|
public EventCallback<FormProviderFinishEventArgs> OnFormFinish { get; set; }
|
|
|
|
[Parameter]
|
|
public RenderFragment ChildContent { get; set; }
|
|
|
|
private Dictionary<string, IForm> _forms = new Dictionary<string, IForm>();
|
|
|
|
void IFormProvider.AddForm(IForm form)
|
|
{
|
|
_forms.Add(form.Name, form);
|
|
|
|
form.OnFinishEvent += OnFinish;
|
|
}
|
|
|
|
void IFormProvider.RemoveForm(IForm form)
|
|
{
|
|
_forms.Remove(form.Name);
|
|
|
|
form.OnFinishEvent += OnFinish;
|
|
}
|
|
|
|
private void OnFinish(IForm form)
|
|
{
|
|
var args = new FormProviderFinishEventArgs
|
|
{
|
|
Forms = _forms,
|
|
FinishForm = form
|
|
};
|
|
|
|
OnFormFinish.InvokeAsync(args);
|
|
}
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
base.Dispose(disposing);
|
|
|
|
_forms.Values.ForEach(form =>
|
|
{
|
|
form.OnFinishEvent -= OnFinish;
|
|
});
|
|
}
|
|
|
|
public IForm GetForm(string formName)
|
|
{
|
|
if (_forms.TryGetValue(formName, out IForm form))
|
|
{
|
|
return form;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public bool TryGetForm(string formName, out IForm form)
|
|
{
|
|
if (_forms.TryGetValue(formName, out form))
|
|
{
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|