ant-design-blazor/components/form/Form.razor.cs
RVS 04166ba276 feat(module: form): add IsModified property (#327)
* feat: is form modified property and method exposed
subject: new method for form context, Is form modified

* fix: interface and doc

Co-authored-by: Ratomir Vukadin <ratomirvu@maestralsolutions.com>
Co-authored-by: James Yeung <shunjiey@hotmail.com>
2020-07-12 00:17:02 +08:00

98 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using AntDesign.Forms;
using AntDesign.Internal;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Forms;
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]
public ColLayoutParam LabelCol { get; set; }
[Parameter]
public ColLayoutParam WrapperCol { get; set; }
[Parameter]
public string Size { get; set; }
[Parameter]
public TModel Model { get; set; }
[Parameter]
public bool Loading { get; set; }
[Parameter]
public EventCallback<EditContext> OnFinish { get; set; }
[Parameter]
public EventCallback<EditContext> OnFinishFailed { get; set; }
public bool IsModified => _editContext.IsModified();
private EditContext _editContext;
private IList<IFormItem> _formItems = new List<IFormItem>();
private IList<IControlValueAccessor> _controls = new List<IControlValueAccessor>();
ColLayoutParam IForm.WrapperCol => WrapperCol;
ColLayoutParam IForm.LabelCol => LabelCol;
EditContext IForm.EditContext => _editContext;
string IForm.Size => Size;
protected override void OnInitialized()
{
base.OnInitialized();
SetClass();
_editContext = new EditContext(Model);
}
protected void SetClass()
{
this.ClassMapper.Clear()
.Add(_prefixCls)
.Add($"{_prefixCls}-{Layout.ToLower()}")
;
}
public void HandleValidSubmit()
{
}
public void Reset()
{
_controls.ForEach(item => item.Reset());
}
void IForm.AddFormItem(IFormItem formItem)
{
_formItems.Add(formItem);
}
void IForm.AddControl(IControlValueAccessor valueAccessor)
{
this._controls.Add(valueAccessor);
}
public bool Validate()
{
return _editContext.Validate();
}
}
}