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>
This commit is contained in:
RVS 2020-07-11 18:17:02 +02:00 committed by GitHub
parent cd2901fa7b
commit 04166ba276
4 changed files with 69 additions and 7 deletions

View File

@ -39,6 +39,8 @@ namespace AntDesign
[Parameter] [Parameter]
public EventCallback<EditContext> OnFinishFailed { get; set; } public EventCallback<EditContext> OnFinishFailed { get; set; }
public bool IsModified => _editContext.IsModified();
private EditContext _editContext; private EditContext _editContext;
private IList<IFormItem> _formItems = new List<IFormItem>(); private IList<IFormItem> _formItems = new List<IFormItem>();
private IList<IControlValueAccessor> _controls = new List<IControlValueAccessor>(); private IList<IControlValueAccessor> _controls = new List<IControlValueAccessor>();
@ -48,6 +50,7 @@ namespace AntDesign
ColLayoutParam IForm.LabelCol => LabelCol; ColLayoutParam IForm.LabelCol => LabelCol;
EditContext IForm.EditContext => _editContext; EditContext IForm.EditContext => _editContext;
string IForm.Size => Size; string IForm.Size => Size;
protected override void OnInitialized() protected override void OnInitialized()
@ -58,13 +61,6 @@ namespace AntDesign
_editContext = new EditContext(Model); _editContext = new EditContext(Model);
} }
protected override void OnParametersSet()
{
base.OnParametersSet();
SetClass();
}
protected void SetClass() protected void SetClass()
{ {
this.ClassMapper.Clear() this.ClassMapper.Clear()
@ -96,5 +92,6 @@ namespace AntDesign
{ {
return _editContext.Validate(); return _editContext.Validate();
} }
} }
} }

View File

@ -10,12 +10,15 @@ namespace AntDesign.Internal
internal ColLayoutParam LabelCol { get; } internal ColLayoutParam LabelCol { get; }
internal EditContext EditContext { get; } internal EditContext EditContext { get; }
internal string Size { get; } internal string Size { get; }
internal void AddFormItem(IFormItem formItem); internal void AddFormItem(IFormItem formItem);
internal void AddControl(IControlValueAccessor valueAccessor); internal void AddControl(IControlValueAccessor valueAccessor);
bool IsModified { get; }
void Reset(); void Reset();
bool Validate(); bool Validate();

View File

@ -0,0 +1,14 @@
---
order: 6
title:
zh-CN: 判断表单是否被修改
en-US: Check if the form has been modified
---
## zh-CN
通过 `IForm.IsModified` 判断表单修改。
## en-US
Check if the form has been modified by using `IForm.IsModified`.

View File

@ -0,0 +1,48 @@
@using System.ComponentModel.DataAnnotations;
@using System.Text.Json;
<Form @ref="form" Model="@model"
LabelCol="new ColLayoutParam { Span = 8 }"
WrapperCol="new ColLayoutParam { Span = 16 }"
OnFinish="OnFinish"
OnFinishFailed="OnFinishFailed">
<FormItem Label="Username">
<Input @bind-Value="@context.Username" />
</FormItem>
<FormItem Label="Password">
<InputPassword @bind-Value="@context.Password" />
</FormItem>
<FormItem WrapperCol="new ColLayoutParam{ Offset = 8, Span = 16 }">
<Checkbox @bind-Value="context.RememberMe">Remember me</Checkbox>
</FormItem>
<FormItem WrapperCol="new ColLayoutParam{ Offset = 8, Span = 16 }">
<Button Type="@ButtonType.Primary" HtmlType="submit" Disabled="!form.IsModified">
Submit
</Button>
</FormItem>
</Form>
@code
{
public class Model
{
[Required]
public string Username { get; set; }
[Required]
public string Password { get; set; }
public bool RememberMe { get; set; } = true;
}
Form<Model> form;
private Model model = new Model();
private void OnFinish(EditContext editContext)
{
Console.WriteLine($"Success:{JsonSerializer.Serialize(model)}");
}
private void OnFinishFailed(EditContext editContext)
{
Console.WriteLine($"Failed:{JsonSerializer.Serialize(model)}");
}
}