2020-06-01 14:09:28 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-05-28 17:08:42 +08:00
|
|
|
|
using System.Linq;
|
2021-05-11 11:13:14 +08:00
|
|
|
|
using System.Reflection;
|
2021-05-28 17:08:42 +08:00
|
|
|
|
using System.Text.Json;
|
2020-07-16 08:56:15 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
using AntDesign.Forms;
|
|
|
|
|
using AntDesign.Internal;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Forms;
|
2020-11-27 16:41:26 +08:00
|
|
|
|
using OneOf;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
|
|
|
|
public partial class Form<TModel> : AntDomComponentBase, IForm
|
|
|
|
|
{
|
|
|
|
|
private readonly string _prefixCls = "ant-form";
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public string Layout { get; set; } = FormLayout.Horizontal;
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment<TModel> ChildContent { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
2020-11-27 16:41:26 +08:00
|
|
|
|
public ColLayoutParam LabelCol { get; set; } = new ColLayoutParam();
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
2021-04-02 23:13:44 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public AntLabelAlignType? LabelAlign { get; set; }
|
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
[Parameter]
|
2020-11-27 16:41:26 +08:00
|
|
|
|
public OneOf<string, int> LabelColSpan
|
|
|
|
|
{
|
|
|
|
|
get { return LabelCol.Span; }
|
|
|
|
|
set { LabelCol.Span = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public OneOf<string, int> LabelColOffset
|
|
|
|
|
{
|
|
|
|
|
get { return LabelCol.Offset; }
|
|
|
|
|
set { LabelCol.Offset = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public ColLayoutParam WrapperCol { get; set; } = new ColLayoutParam();
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public OneOf<string, int> WrapperColSpan
|
|
|
|
|
{
|
|
|
|
|
get { return WrapperCol.Span; }
|
|
|
|
|
set { WrapperCol.Span = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public OneOf<string, int> WrapperColOffset
|
|
|
|
|
{
|
|
|
|
|
get { return WrapperCol.Offset; }
|
|
|
|
|
set { WrapperCol.Offset = value; }
|
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
2020-06-07 00:47:18 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string Size { get; set; }
|
|
|
|
|
|
2020-07-16 08:56:15 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
[Parameter]
|
2021-01-24 22:52:19 +08:00
|
|
|
|
public TModel Model
|
|
|
|
|
{
|
|
|
|
|
get { return _model; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (!(_model?.Equals(value) ?? false))
|
|
|
|
|
{
|
2021-05-11 11:13:14 +08:00
|
|
|
|
var wasNull = _model is null;
|
2021-01-24 22:52:19 +08:00
|
|
|
|
_model = value;
|
2021-05-11 11:13:14 +08:00
|
|
|
|
if (!wasNull)
|
|
|
|
|
BuildEditContext();
|
2021-01-24 22:52:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
2020-06-10 19:13:09 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public bool Loading { get; set; }
|
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<EditContext> OnFinish { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<EditContext> OnFinishFailed { get; set; }
|
|
|
|
|
|
2021-05-11 11:13:14 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<FieldChangedEventArgs> OnFieldChanged { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<ValidationRequestedEventArgs> OnValidationRequested { get; set; }
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<ValidationStateChangedEventArgs> OnValidationStateChanged { get; set; }
|
|
|
|
|
|
2020-12-01 19:07:14 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public RenderFragment Validator { get; set; } = _defaultValidator;
|
|
|
|
|
|
2020-12-25 12:51:55 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Enable validation when component values change
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public bool ValidateOnChange { get; set; }
|
|
|
|
|
|
2021-05-28 17:08:42 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public FormValidateMode ValidateMode { get; set; } = FormValidateMode.Default;
|
|
|
|
|
|
2020-12-01 19:07:14 +08:00
|
|
|
|
private static readonly RenderFragment _defaultValidator = builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.OpenComponent<ObjectGraphDataAnnotationsValidator>(0);
|
|
|
|
|
builder.CloseComponent();
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-28 17:08:42 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public FormValidateErrorMessages ValidateMessages { get; set; }
|
|
|
|
|
|
|
|
|
|
|
2020-07-16 08:56:15 +08:00
|
|
|
|
[CascadingParameter(Name = "FormProvider")]
|
|
|
|
|
private IFormProvider FormProvider { get; set; }
|
|
|
|
|
|
2020-07-12 00:17:02 +08:00
|
|
|
|
public bool IsModified => _editContext.IsModified();
|
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
private EditContext _editContext;
|
|
|
|
|
private IList<IFormItem> _formItems = new List<IFormItem>();
|
2020-06-02 07:28:06 +08:00
|
|
|
|
private IList<IControlValueAccessor> _controls = new List<IControlValueAccessor>();
|
2021-01-24 22:52:19 +08:00
|
|
|
|
private TModel _model;
|
2021-05-28 17:08:42 +08:00
|
|
|
|
private FormRulesValidator _rulesValidator;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
|
|
|
|
ColLayoutParam IForm.WrapperCol => WrapperCol;
|
|
|
|
|
|
|
|
|
|
ColLayoutParam IForm.LabelCol => LabelCol;
|
|
|
|
|
|
|
|
|
|
EditContext IForm.EditContext => _editContext;
|
2020-07-12 00:17:02 +08:00
|
|
|
|
|
2021-04-02 23:13:44 +08:00
|
|
|
|
AntLabelAlignType? IForm.LabelAlign => LabelAlign;
|
2020-06-07 00:47:18 +08:00
|
|
|
|
string IForm.Size => Size;
|
2020-07-16 08:56:15 +08:00
|
|
|
|
string IForm.Name => Name;
|
|
|
|
|
object IForm.Model => Model;
|
2020-12-25 12:51:55 +08:00
|
|
|
|
bool IForm.ValidateOnChange => ValidateOnChange;
|
2020-07-16 08:56:15 +08:00
|
|
|
|
|
2020-12-25 12:51:55 +08:00
|
|
|
|
bool IForm.IsModified => _editContext.IsModified();
|
2020-08-02 16:34:34 +08:00
|
|
|
|
|
2021-05-28 17:08:42 +08:00
|
|
|
|
FormValidateMode IForm.ValidateMode => ValidateMode;
|
|
|
|
|
FormValidateErrorMessages IForm.ValidateMessages => ValidateMessages;
|
|
|
|
|
|
2020-07-16 08:56:15 +08:00
|
|
|
|
public event Action<IForm> OnFinishEvent;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
|
2020-06-30 22:32:36 +08:00
|
|
|
|
_editContext = new EditContext(Model);
|
2020-07-16 08:56:15 +08:00
|
|
|
|
|
|
|
|
|
if (FormProvider != null)
|
|
|
|
|
{
|
|
|
|
|
FormProvider.AddForm(this);
|
|
|
|
|
}
|
2021-05-11 11:13:14 +08:00
|
|
|
|
|
|
|
|
|
if (OnFieldChanged.HasDelegate)
|
|
|
|
|
_editContext.OnFieldChanged += OnFieldChangedHandler;
|
|
|
|
|
if (OnValidationRequested.HasDelegate)
|
|
|
|
|
_editContext.OnValidationRequested += OnValidationRequestedHandler;
|
|
|
|
|
if (OnValidationStateChanged.HasDelegate)
|
|
|
|
|
_editContext.OnValidationStateChanged += OnValidationStateChangedHandler;
|
|
|
|
|
|
2021-05-28 17:08:42 +08:00
|
|
|
|
if (ValidateMode.IsIn(FormValidateMode.Rules, FormValidateMode.Complex))
|
|
|
|
|
{
|
|
|
|
|
_editContext.OnFieldChanged += RulesModeOnFieldChanged;
|
|
|
|
|
_editContext.OnValidationRequested += RulesModeOnValidationRequested;
|
|
|
|
|
}
|
2021-05-11 11:13:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnFieldChangedHandler(object sender, FieldChangedEventArgs e) => InvokeAsync(() => OnFieldChanged.InvokeAsync(e));
|
|
|
|
|
private void OnValidationRequestedHandler(object sender, ValidationRequestedEventArgs e) => InvokeAsync(() => OnValidationRequested.InvokeAsync(e));
|
|
|
|
|
private void OnValidationStateChangedHandler(object sender, ValidationStateChangedEventArgs e) => InvokeAsync(() => OnValidationStateChanged.InvokeAsync(e));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (OnFieldChanged.HasDelegate)
|
|
|
|
|
_editContext.OnFieldChanged -= OnFieldChangedHandler;
|
|
|
|
|
if (OnValidationRequested.HasDelegate)
|
|
|
|
|
_editContext.OnValidationRequested -= OnValidationRequestedHandler;
|
|
|
|
|
if (OnValidationStateChanged.HasDelegate)
|
|
|
|
|
_editContext.OnValidationStateChanged -= OnValidationStateChangedHandler;
|
2021-05-28 17:08:42 +08:00
|
|
|
|
|
|
|
|
|
if (ValidateMode.IsIn(FormValidateMode.Rules, FormValidateMode.Complex))
|
|
|
|
|
{
|
|
|
|
|
_editContext.OnFieldChanged -= RulesModeOnFieldChanged;
|
|
|
|
|
_editContext.OnValidationRequested -= RulesModeOnValidationRequested;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 11:13:14 +08:00
|
|
|
|
base.Dispose(disposing);
|
2020-07-16 08:56:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void OnParametersSet()
|
|
|
|
|
{
|
|
|
|
|
base.OnParametersSet();
|
|
|
|
|
|
|
|
|
|
SetClass();
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void SetClass()
|
|
|
|
|
{
|
|
|
|
|
this.ClassMapper.Clear()
|
|
|
|
|
.Add(_prefixCls)
|
2021-03-12 17:02:11 +08:00
|
|
|
|
.Get(() => $"{_prefixCls}-{Layout.ToLowerInvariant()}")
|
|
|
|
|
.If($"{_prefixCls}-rtl", () => RTL)
|
2020-06-01 14:09:28 +08:00
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 08:56:15 +08:00
|
|
|
|
private async Task OnValidSubmit(EditContext editContext)
|
|
|
|
|
{
|
|
|
|
|
await OnFinish.InvokeAsync(editContext);
|
|
|
|
|
|
2020-07-22 16:58:47 +08:00
|
|
|
|
OnFinishEvent?.Invoke(this);
|
2020-07-16 08:56:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task OnInvalidSubmit(EditContext editContext)
|
2020-06-01 14:09:28 +08:00
|
|
|
|
{
|
2020-07-16 08:56:15 +08:00
|
|
|
|
await OnFinishFailed.InvokeAsync(editContext);
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 17:08:42 +08:00
|
|
|
|
private void RulesModeOnFieldChanged(object sender, FieldChangedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (!ValidateMode.IsIn(FormValidateMode.Rules, FormValidateMode.Complex))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_rulesValidator.ClearError(args.FieldIdentifier);
|
|
|
|
|
|
|
|
|
|
var formItem = _formItems
|
|
|
|
|
.Single(t => t.GetFieldIdentifier().FieldName == args.FieldIdentifier.FieldName);
|
|
|
|
|
|
|
|
|
|
var result = formItem.ValidateField();
|
|
|
|
|
|
|
|
|
|
if (result.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
var errors = new Dictionary<FieldIdentifier, List<string>>();
|
|
|
|
|
errors[args.FieldIdentifier] = result.Select(r => r.ErrorMessage).ToList();
|
|
|
|
|
|
|
|
|
|
_rulesValidator.DisplayErrors(errors);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RulesModeOnValidationRequested(object sender, ValidationRequestedEventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (!ValidateMode.IsIn(FormValidateMode.Rules, FormValidateMode.Complex))
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_rulesValidator.ClearErrors();
|
|
|
|
|
|
|
|
|
|
var errors = new Dictionary<FieldIdentifier, List<string>>();
|
|
|
|
|
|
|
|
|
|
foreach (var formItem in _formItems)
|
|
|
|
|
{
|
|
|
|
|
var result = formItem.ValidateField();
|
|
|
|
|
if (result.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
errors[formItem.GetFieldIdentifier()] = result.Select(r => r.ErrorMessage).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_rulesValidator.DisplayErrors(errors);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
public void Reset()
|
|
|
|
|
{
|
2020-06-02 07:28:06 +08:00
|
|
|
|
_controls.ForEach(item => item.Reset());
|
2021-05-11 11:13:14 +08:00
|
|
|
|
BuildEditContext();
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void IForm.AddFormItem(IFormItem formItem)
|
|
|
|
|
{
|
|
|
|
|
_formItems.Add(formItem);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 07:28:06 +08:00
|
|
|
|
void IForm.AddControl(IControlValueAccessor valueAccessor)
|
2020-06-01 14:09:28 +08:00
|
|
|
|
{
|
|
|
|
|
this._controls.Add(valueAccessor);
|
|
|
|
|
}
|
2020-06-11 23:11:55 +08:00
|
|
|
|
|
2021-06-24 12:18:18 +08:00
|
|
|
|
void IForm.RemoveControl(IControlValueAccessor valueAccessor)
|
|
|
|
|
{
|
|
|
|
|
if (_controls.Contains(valueAccessor))
|
|
|
|
|
{
|
|
|
|
|
this._controls.Remove(valueAccessor);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-16 08:56:15 +08:00
|
|
|
|
public void Submit()
|
|
|
|
|
{
|
2020-08-02 16:34:34 +08:00
|
|
|
|
var isValid = _editContext.Validate();
|
2020-07-16 08:56:15 +08:00
|
|
|
|
|
|
|
|
|
if (isValid)
|
|
|
|
|
{
|
2020-08-02 16:34:34 +08:00
|
|
|
|
if (OnFinish.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
OnFinish.InvokeAsync(_editContext);
|
|
|
|
|
}
|
2020-07-16 08:56:15 +08:00
|
|
|
|
|
2020-08-02 16:34:34 +08:00
|
|
|
|
OnFinishEvent?.Invoke(this);
|
2020-07-16 08:56:15 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
OnFinishFailed.InvokeAsync(_editContext);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-11 11:13:14 +08:00
|
|
|
|
public bool Validate() => _editContext.Validate();
|
|
|
|
|
|
|
|
|
|
public void ValidationReset() => BuildEditContext();
|
|
|
|
|
|
|
|
|
|
public EditContext EditContext => _editContext;
|
|
|
|
|
|
|
|
|
|
public void BuildEditContext()
|
2020-06-11 23:11:55 +08:00
|
|
|
|
{
|
2021-05-11 11:13:14 +08:00
|
|
|
|
if (_editContext == null)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var newContext = new EditContext(Model);
|
|
|
|
|
foreach (var kv in GetEventInfos())
|
|
|
|
|
{
|
|
|
|
|
FieldInfo fieldInfo = kv.Value.fi;
|
|
|
|
|
EventInfo eventInfo = kv.Value.ei;
|
|
|
|
|
Delegate mdel = fieldInfo.GetValue(_editContext) as Delegate;
|
|
|
|
|
if (mdel != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (Delegate del in mdel.GetInvocationList())
|
|
|
|
|
{
|
|
|
|
|
eventInfo.RemoveEventHandler(_editContext, del);
|
|
|
|
|
eventInfo.AddEventHandler(newContext, del);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_editContext = newContext;
|
2020-06-11 23:11:55 +08:00
|
|
|
|
}
|
2021-01-24 22:52:19 +08:00
|
|
|
|
|
2021-05-11 11:13:14 +08:00
|
|
|
|
static BindingFlags AllBindings
|
2021-01-24 22:52:19 +08:00
|
|
|
|
{
|
2021-05-11 11:13:14 +08:00
|
|
|
|
get { return BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance; }
|
2021-01-24 22:52:19 +08:00
|
|
|
|
}
|
2021-05-08 00:47:52 +08:00
|
|
|
|
|
2021-05-11 11:13:14 +08:00
|
|
|
|
static Dictionary<string, (FieldInfo fi, EventInfo ei)> _eventInfos;
|
|
|
|
|
|
|
|
|
|
static Dictionary<string, (FieldInfo fi, EventInfo ei)> GetEventInfos()
|
|
|
|
|
{
|
|
|
|
|
if (_eventInfos is null)
|
|
|
|
|
{
|
|
|
|
|
_eventInfos = new();
|
|
|
|
|
Type contextType = typeof(EditContext);
|
|
|
|
|
foreach (EventInfo eventInfo in contextType.GetEvents(AllBindings))
|
|
|
|
|
{
|
|
|
|
|
Type declaringType = eventInfo.DeclaringType;
|
|
|
|
|
FieldInfo fieldInfo = declaringType.GetField(eventInfo.Name, AllBindings);
|
|
|
|
|
if (fieldInfo is not null)
|
|
|
|
|
{
|
|
|
|
|
_eventInfos.Add(eventInfo.Name, (fieldInfo, eventInfo));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return _eventInfos;
|
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|