fix(module: form): fix input components inside customized form control (#1662)

This commit is contained in:
anranruye 2021-06-24 12:18:18 +08:00 committed by GitHub
parent 10e13c5059
commit a06fd5c2e5
4 changed files with 29 additions and 1 deletions

View File

@ -328,6 +328,8 @@ namespace AntDesign
EditContext.OnValidationStateChanged -= _validationStateChangedHandler;
}
Form?.RemoveControl(this);
base.Dispose(disposing);
}

View File

@ -182,6 +182,14 @@ namespace AntDesign
this._controls.Add(valueAccessor);
}
void IForm.RemoveControl(IControlValueAccessor valueAccessor)
{
if (_controls.Contains(valueAccessor))
{
this._controls.Remove(valueAccessor);
}
}
public void Submit()
{
var isValid = _editContext.Validate();

View File

@ -108,6 +108,8 @@ namespace AntDesign
private ClassMapper _labelClassMapper = new ClassMapper();
private AntLabelAlignType? FormLabelAlign => LabelAlign ?? Form.LabelAlign;
private EventHandler<ValidationStateChangedEventArgs> _validationStateChangedHandler;
protected override void OnInitialized()
{
base.OnInitialized();
@ -191,8 +193,20 @@ namespace AntDesign
return Required ? $"{_prefixCls}-required" : _labelCls;
}
protected override void Dispose(bool disposing)
{
if (CurrentEditContext != null && _validationStateChangedHandler != null)
{
CurrentEditContext.OnValidationStateChanged -= _validationStateChangedHandler;
}
base.Dispose(disposing);
}
void IFormItem.AddControl<TValue>(AntInputComponentBase<TValue> control)
{
if (_control != null) return;
if (control.FieldIdentifier.Model == null)
{
throw new InvalidOperationException($"Please use @bind-Value (or @bind-Values for selected components) in the control with generic type `{typeof(TValue)}`.");
@ -200,7 +214,7 @@ namespace AntDesign
this._control = control;
CurrentEditContext.OnValidationStateChanged += (s, e) =>
_validationStateChangedHandler = (s, e) =>
{
control.ValidationMessages = CurrentEditContext.GetValidationMessages(control.FieldIdentifier).Distinct().ToArray();
this._isValid = !control.ValidationMessages.Any();
@ -208,6 +222,8 @@ namespace AntDesign
StateHasChanged();
};
CurrentEditContext.OnValidationStateChanged += _validationStateChangedHandler;
_formValidationMessages = builder =>
{
var i = 0;

View File

@ -20,6 +20,8 @@ namespace AntDesign.Internal
internal void AddControl(IControlValueAccessor valueAccessor);
internal void RemoveControl(IControlValueAccessor valueAccessor);
internal bool ValidateOnChange { get; }
event Action<IForm> OnFinishEvent;