2020-04-28 15:56:56 +08:00
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
2020-05-29 00:33:49 +08:00
|
|
|
|
namespace AntDesign
|
2020-04-28 15:56:56 +08:00
|
|
|
|
{
|
2020-07-11 23:03:13 +08:00
|
|
|
|
public partial class DatePicker<TValue> : DatePickerBase<TValue>
|
2020-04-28 15:56:56 +08:00
|
|
|
|
{
|
2020-07-07 19:13:34 +08:00
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
base.OnInitialized();
|
2020-07-11 23:03:13 +08:00
|
|
|
|
|
|
|
|
|
if (Value != null)
|
2020-07-07 19:13:34 +08:00
|
|
|
|
{
|
2020-07-11 23:03:13 +08:00
|
|
|
|
GetIfNotNull(Value, notNullValue =>
|
|
|
|
|
{
|
|
|
|
|
ChangeValue(notNullValue, 0);
|
|
|
|
|
});
|
2020-07-07 19:13:34 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 15:56:56 +08:00
|
|
|
|
protected void OnInput(ChangeEventArgs args, int index = 0)
|
|
|
|
|
{
|
|
|
|
|
if (args == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-11 23:03:13 +08:00
|
|
|
|
if (TryParseValueFromString(args.Value.ToString(), out TValue changeValue, out _))
|
2020-04-28 15:56:56 +08:00
|
|
|
|
{
|
2020-06-01 14:09:28 +08:00
|
|
|
|
Value = changeValue;
|
2020-07-11 23:03:13 +08:00
|
|
|
|
|
|
|
|
|
GetIfNotNull(changeValue, (notNullValue) =>
|
|
|
|
|
{
|
2020-09-09 00:52:14 +08:00
|
|
|
|
PickerValues[index] = notNullValue;
|
2020-07-11 23:03:13 +08:00
|
|
|
|
});
|
2020-04-28 15:56:56 +08:00
|
|
|
|
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
UpdateCurrentValueAsString();
|
2020-04-28 15:56:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get value by picker index
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="index"></param>
|
|
|
|
|
/// <returns></returns>
|
2020-06-01 14:09:28 +08:00
|
|
|
|
public override DateTime? GetIndexValue(int index)
|
2020-04-28 15:56:56 +08:00
|
|
|
|
{
|
2020-05-10 17:33:09 +08:00
|
|
|
|
if (_pickerStatus[index]._hadSelectValue)
|
|
|
|
|
{
|
2020-07-11 23:03:13 +08:00
|
|
|
|
if (Value == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Convert.ToDateTime(Value, this.CultureInfo);
|
2020-05-10 17:33:09 +08:00
|
|
|
|
}
|
2020-09-09 00:52:14 +08:00
|
|
|
|
else if (DefaultValues[index] != null)
|
2020-05-10 17:33:09 +08:00
|
|
|
|
{
|
2020-09-09 00:52:14 +08:00
|
|
|
|
return DefaultValues[index];
|
2020-05-10 17:33:09 +08:00
|
|
|
|
}
|
2020-07-11 23:03:13 +08:00
|
|
|
|
|
|
|
|
|
return null;
|
2020-04-28 15:56:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
public override void ChangeValue(DateTime value, int index = 0)
|
2020-04-28 15:56:56 +08:00
|
|
|
|
{
|
2020-07-11 23:03:13 +08:00
|
|
|
|
bool result = BindConverter.TryConvertTo<TValue>(
|
|
|
|
|
value.ToString(CultureInfo), CultureInfo, out var dateTime);
|
|
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
Value = dateTime;
|
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
|
|
|
|
_pickerStatus[index]._hadSelectValue = true;
|
2020-04-28 15:56:56 +08:00
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
UpdateCurrentValueAsString();
|
2020-04-28 15:56:56 +08:00
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
if (IsRange && !IsShowTime && Picker != DatePickerType.Time)
|
2020-04-28 15:56:56 +08:00
|
|
|
|
{
|
2020-06-01 14:09:28 +08:00
|
|
|
|
if (_pickerStatus[0]._hadSelectValue && _pickerStatus[1]._hadSelectValue)
|
2020-04-28 15:56:56 +08:00
|
|
|
|
{
|
2020-06-01 14:09:28 +08:00
|
|
|
|
Close();
|
2020-04-28 15:56:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
else if (!IsShowTime && Picker != DatePickerType.Time)
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
2020-04-28 15:56:56 +08:00
|
|
|
|
}
|
2020-07-11 23:03:13 +08:00
|
|
|
|
|
|
|
|
|
protected override void OnValueChange(TValue value)
|
|
|
|
|
{
|
|
|
|
|
base.OnValueChange(value);
|
|
|
|
|
|
|
|
|
|
_pickerStatus[0]._hadSelectValue = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-06 19:09:01 +08:00
|
|
|
|
public override void ClearValue(int index = 0)
|
|
|
|
|
{
|
|
|
|
|
_isSetPicker = false;
|
2020-07-11 23:03:13 +08:00
|
|
|
|
CurrentValue = default;
|
2020-07-06 19:09:01 +08:00
|
|
|
|
Close();
|
|
|
|
|
}
|
2020-07-11 23:03:13 +08:00
|
|
|
|
|
|
|
|
|
private void GetIfNotNull(TValue value, Action<DateTime> notNullAction)
|
|
|
|
|
{
|
|
|
|
|
if (!_isNullable)
|
|
|
|
|
{
|
|
|
|
|
DateTime dateTime = Convert.ToDateTime(value, CultureInfo);
|
|
|
|
|
if (dateTime != DateTime.MinValue)
|
|
|
|
|
{
|
|
|
|
|
notNullAction?.Invoke(dateTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (_isNullable && value != null)
|
|
|
|
|
{
|
|
|
|
|
notNullAction?.Invoke(Convert.ToDateTime(value, CultureInfo));
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-28 15:56:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|