2020-06-02 20:27:53 +08:00
|
|
|
|
using System;
|
2019-12-08 00:51:07 +08:00
|
|
|
|
using System.Collections.Generic;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
using System.Globalization;
|
2019-12-08 00:51:07 +08:00
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using System.Threading.Tasks;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
using AntDesign.Forms;
|
|
|
|
|
using AntDesign.Internal;
|
2020-06-02 20:27:53 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
using Microsoft.AspNetCore.Components.Forms;
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Base class for any input control that optionally supports an <see cref="EditContext"/>.
|
2020-06-01 14:09:28 +08:00
|
|
|
|
/// reference:https://github.com/dotnet/aspnetcore/blob/master/src/Components/Web/src/Forms/InputBase.cs
|
2019-12-08 00:51:07 +08:00
|
|
|
|
/// </summary>
|
2020-06-01 14:09:28 +08:00
|
|
|
|
/// <typeparam name="TValue">the natural type of the input's value</typeparam>
|
2020-06-02 07:28:06 +08:00
|
|
|
|
public abstract class AntInputComponentBase<TValue> : AntDomComponentBase, IControlValueAccessor
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
2020-06-01 14:09:28 +08:00
|
|
|
|
private readonly EventHandler<ValidationStateChangedEventArgs> _validationStateChangedHandler;
|
|
|
|
|
private bool _previousParsingAttemptFailed;
|
|
|
|
|
private ValidationMessageStore _parsingValidationMessages;
|
|
|
|
|
private Type _nullableUnderlyingType;
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
[CascadingParameter(Name = "FormItem")]
|
2020-06-03 20:29:28 +08:00
|
|
|
|
private IFormItem FormItem { get; set; }
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
|
|
|
|
[CascadingParameter(Name = "Form")]
|
2020-06-07 00:47:18 +08:00
|
|
|
|
protected IForm Form { get; set; }
|
|
|
|
|
|
2020-06-25 19:23:16 +08:00
|
|
|
|
public string[] ValidationMessages { get; set; } = Array.Empty<string>();
|
|
|
|
|
|
2020-06-07 00:47:18 +08:00
|
|
|
|
private string _formSize;
|
|
|
|
|
[CascadingParameter(Name = "FormSize")]
|
|
|
|
|
public string FormSize
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _formSize;
|
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_formSize = value;
|
|
|
|
|
Size = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets a collection of additional attributes that will be applied to the created element.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter(CaptureUnmatchedValues = true)]
|
|
|
|
|
public IReadOnlyDictionary<string, object> AdditionalAttributes { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the value of the input. This should be used with two-way binding.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <example>
|
|
|
|
|
/// @bind-Value="model.PropertyName"
|
|
|
|
|
/// </example>
|
2019-12-08 00:51:07 +08:00
|
|
|
|
[Parameter]
|
2020-06-01 14:09:28 +08:00
|
|
|
|
public TValue Value { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets a callback that updates the bound value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<TValue> ValueChanged { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets an expression that identifies the bound value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter]
|
|
|
|
|
public Expression<Func<TValue>> ValueExpression { get; set; }
|
|
|
|
|
|
2020-06-07 00:47:18 +08:00
|
|
|
|
[Parameter]
|
|
|
|
|
public string Size { get; set; } = AntSizeLDSType.Default;
|
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the associated <see cref="EditContext"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected EditContext EditContext { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the <see cref="FieldIdentifier"/> for the bound value.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal FieldIdentifier FieldIdentifier { get; set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the current value of the input.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected TValue CurrentValue
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
2020-06-01 14:09:28 +08:00
|
|
|
|
get => Value;
|
2019-12-08 00:51:07 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
2020-06-01 14:09:28 +08:00
|
|
|
|
var hasChanged = !EqualityComparer<TValue>.Default.Equals(value, Value);
|
|
|
|
|
if (hasChanged)
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
2020-06-01 14:09:28 +08:00
|
|
|
|
Value = value;
|
|
|
|
|
_ = ValueChanged.InvokeAsync(value);
|
|
|
|
|
|
|
|
|
|
if (_isNotifyFieldChanged)
|
|
|
|
|
{
|
2020-06-03 11:01:43 +08:00
|
|
|
|
EditContext?.NotifyFieldChanged(FieldIdentifier);
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
2019-12-08 00:51:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-01 14:09:28 +08:00
|
|
|
|
/// Gets or sets the current value of the input, represented as a string.
|
2019-12-08 00:51:07 +08:00
|
|
|
|
/// </summary>
|
2020-06-01 14:09:28 +08:00
|
|
|
|
protected string CurrentValueAsString
|
|
|
|
|
{
|
|
|
|
|
get => FormatValueAsString(CurrentValue);
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_parsingValidationMessages?.Clear();
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
bool parsingFailed;
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
if (_nullableUnderlyingType != null && string.IsNullOrEmpty(value))
|
|
|
|
|
{
|
|
|
|
|
// Assume if it's a nullable type, null/empty inputs should correspond to default(T)
|
|
|
|
|
// Then all subclasses get nullable support almost automatically (they just have to
|
|
|
|
|
// not reject Nullable<T> based on the type itself).
|
|
|
|
|
parsingFailed = false;
|
|
|
|
|
CurrentValue = default;
|
|
|
|
|
}
|
|
|
|
|
else if (TryParseValueFromString(value, out var parsedValue, out var validationErrorMessage))
|
|
|
|
|
{
|
|
|
|
|
parsingFailed = false;
|
|
|
|
|
CurrentValue = parsedValue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
parsingFailed = true;
|
|
|
|
|
|
2020-06-03 20:29:28 +08:00
|
|
|
|
if (EditContext != null)
|
2020-06-01 14:09:28 +08:00
|
|
|
|
{
|
2020-06-03 20:29:28 +08:00
|
|
|
|
if (_parsingValidationMessages == null)
|
|
|
|
|
{
|
|
|
|
|
_parsingValidationMessages = new ValidationMessageStore(EditContext);
|
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
2020-06-03 20:29:28 +08:00
|
|
|
|
_parsingValidationMessages.Add(FieldIdentifier, validationErrorMessage);
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
2020-06-03 20:29:28 +08:00
|
|
|
|
// Since we're not writing to CurrentValue, we'll need to notify about modification from here
|
|
|
|
|
EditContext.NotifyFieldChanged(FieldIdentifier);
|
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// We can skip the validation notification if we were previously valid and still are
|
2020-06-03 20:29:28 +08:00
|
|
|
|
if ((parsingFailed || _previousParsingAttemptFailed) && EditContext != null)
|
2020-06-01 14:09:28 +08:00
|
|
|
|
{
|
|
|
|
|
EditContext.NotifyValidationStateChanged();
|
|
|
|
|
_previousParsingAttemptFailed = parsingFailed;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
2020-06-07 00:47:18 +08:00
|
|
|
|
private TValue _firstValue;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
private bool _isNotifyFieldChanged = true;
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs an instance of <see cref="InputBase{TValue}"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected AntInputComponentBase()
|
|
|
|
|
{
|
|
|
|
|
_validationStateChangedHandler = (sender, eventArgs) => StateHasChanged();
|
|
|
|
|
}
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-01 14:09:28 +08:00
|
|
|
|
/// Formats the value as a string. Derived classes can override this to determine the formating used for <see cref="CurrentValueAsString"/>.
|
2019-12-08 00:51:07 +08:00
|
|
|
|
/// </summary>
|
2020-06-01 14:09:28 +08:00
|
|
|
|
/// <param name="value">The value to format.</param>
|
|
|
|
|
/// <returns>A string representation of the value.</returns>
|
|
|
|
|
protected virtual string FormatValueAsString(TValue value)
|
|
|
|
|
=> value?.ToString();
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-01 14:09:28 +08:00
|
|
|
|
/// Parses a string to create an instance of <typeparamref name="TValue"/>. Derived classes can override this to change how
|
|
|
|
|
/// <see cref="CurrentValueAsString"/> interprets incoming values.
|
2019-12-08 00:51:07 +08:00
|
|
|
|
/// </summary>
|
2020-06-01 14:09:28 +08:00
|
|
|
|
/// <param name="value">The string value to be parsed.</param>
|
|
|
|
|
/// <param name="result">An instance of <typeparamref name="TValue"/>.</param>
|
|
|
|
|
/// <param name="validationErrorMessage">If the value could not be parsed, provides a validation error message.</param>
|
|
|
|
|
/// <returns>True if the value could be parsed; otherwise false.</returns>
|
|
|
|
|
protected virtual bool TryParseValueFromString(string value, out TValue result, out string validationErrorMessage)
|
|
|
|
|
{
|
|
|
|
|
var success = BindConverter.TryConvertTo<TValue>(
|
|
|
|
|
value, CultureInfo.CurrentCulture, out var parsedValue);
|
|
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
|
{
|
|
|
|
|
result = parsedValue;
|
|
|
|
|
validationErrorMessage = null;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
result = default;
|
|
|
|
|
validationErrorMessage = $"{FieldIdentifier.FieldName} field isn't valid.";
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
|
|
|
|
|
FormItem?.AddControl(this);
|
|
|
|
|
Form?.AddControl(this);
|
|
|
|
|
|
|
|
|
|
_firstValue = Value;
|
|
|
|
|
}
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override Task SetParametersAsync(ParameterView parameters)
|
|
|
|
|
{
|
|
|
|
|
parameters.SetParameterProperties(this);
|
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
if (EditContext == null)
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
2020-06-01 14:09:28 +08:00
|
|
|
|
// This is the first run
|
|
|
|
|
// Could put this logic in OnInit, but its nice to avoid forcing people who override OnInit to call base.OnInit()
|
2019-12-08 00:51:07 +08:00
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
if (Form?.EditContext == null)
|
|
|
|
|
{
|
|
|
|
|
return base.SetParametersAsync(ParameterView.Empty);
|
2019-12-08 00:51:07 +08:00
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
|
|
|
|
if (ValueExpression == null)
|
2019-12-08 00:51:07 +08:00
|
|
|
|
{
|
2020-06-01 14:09:28 +08:00
|
|
|
|
return base.SetParametersAsync(ParameterView.Empty);
|
2019-12-08 00:51:07 +08:00
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
|
|
|
|
EditContext = Form?.EditContext;
|
|
|
|
|
FieldIdentifier = FieldIdentifier.Create(ValueExpression);
|
|
|
|
|
_nullableUnderlyingType = Nullable.GetUnderlyingType(typeof(TValue));
|
|
|
|
|
|
|
|
|
|
EditContext.OnValidationStateChanged += _validationStateChangedHandler;
|
|
|
|
|
}
|
|
|
|
|
else if (Form?.EditContext != EditContext)
|
|
|
|
|
{
|
|
|
|
|
// Not the first run
|
|
|
|
|
|
|
|
|
|
// We don't support changing EditContext because it's messy to be clearing up state and event
|
|
|
|
|
// handlers for the previous one, and there's no strong use case. If a strong use case
|
|
|
|
|
// emerges, we can consider changing this.
|
|
|
|
|
throw new InvalidOperationException($"{GetType()} does not support changing the " +
|
|
|
|
|
$"{nameof(EditContext)} dynamically.");
|
2019-12-08 00:51:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
// For derived components, retain the usual lifecycle with OnInit/OnParametersSet/etc.
|
2019-12-08 00:51:07 +08:00
|
|
|
|
return base.SetParametersAsync(ParameterView.Empty);
|
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (EditContext != null)
|
|
|
|
|
{
|
|
|
|
|
EditContext.OnValidationStateChanged -= _validationStateChangedHandler;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void ResetValue()
|
|
|
|
|
{
|
|
|
|
|
_isNotifyFieldChanged = false;
|
|
|
|
|
CurrentValue = _firstValue;
|
|
|
|
|
_isNotifyFieldChanged = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-02 07:28:06 +08:00
|
|
|
|
void IControlValueAccessor.Reset()
|
2020-06-01 14:09:28 +08:00
|
|
|
|
{
|
|
|
|
|
ResetValue();
|
|
|
|
|
}
|
2019-12-08 00:51:07 +08:00
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|