2020-06-01 14:09:28 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
2020-07-05 00:06:34 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
|
|
|
|
|
|
|
|
|
namespace AntDesign
|
|
|
|
|
{
|
2020-08-14 12:43:48 +08:00
|
|
|
|
public partial class RangePicker<TValue> : DatePickerBase<TValue>
|
2020-06-01 14:09:28 +08:00
|
|
|
|
{
|
|
|
|
|
public RangePicker()
|
|
|
|
|
{
|
|
|
|
|
IsRange = true;
|
|
|
|
|
|
|
|
|
|
DisabledDate = (date) =>
|
|
|
|
|
{
|
2020-08-14 12:43:48 +08:00
|
|
|
|
var array = Value as Array;
|
|
|
|
|
|
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
if (_pickerStatus[0]._hadSelectValue && _inputEnd.IsOnFocused)
|
|
|
|
|
{
|
2020-07-11 23:03:13 +08:00
|
|
|
|
DateTime? value = null;
|
2020-08-14 12:43:48 +08:00
|
|
|
|
GetIfNotNull(Value, 0, notNullValue =>
|
2020-07-11 23:03:13 +08:00
|
|
|
|
{
|
|
|
|
|
value = notNullValue;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (value != null)
|
|
|
|
|
{
|
|
|
|
|
return DateHelper.FormatDateByPicker(date.Date, Picker) < DateHelper.FormatDateByPicker(((DateTime)value).Date, Picker);
|
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
if (_pickerStatus[1]._hadSelectValue && _inputStart.IsOnFocused)
|
|
|
|
|
{
|
2020-07-11 23:03:13 +08:00
|
|
|
|
DateTime? value = null;
|
2020-08-14 12:43:48 +08:00
|
|
|
|
GetIfNotNull(Value, 1, notNullValue =>
|
2020-07-11 23:03:13 +08:00
|
|
|
|
{
|
|
|
|
|
value = notNullValue;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (value != null)
|
|
|
|
|
{
|
2020-08-05 07:47:32 +08:00
|
|
|
|
return DateHelper.FormatDateByPicker(date.Date, Picker) > DateHelper.FormatDateByPicker(((DateTime)value).Date, Picker);
|
2020-07-11 23:03:13 +08:00
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void OnInput(ChangeEventArgs args, int index = 0)
|
|
|
|
|
{
|
|
|
|
|
if (args == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 12:43:48 +08:00
|
|
|
|
var array = Value as Array;
|
2020-07-11 23:03:13 +08:00
|
|
|
|
|
2020-08-14 12:43:48 +08:00
|
|
|
|
if (BindConverter.TryConvertTo(args.Value.ToString(), CultureInfo, out DateTime changeValue))
|
|
|
|
|
{
|
|
|
|
|
array.SetValue(changeValue, index);
|
|
|
|
|
_pickerValues[index] = changeValue;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-11 23:03:13 +08:00
|
|
|
|
UpdateCurrentValueAsString();
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 12:43:48 +08:00
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
base.OnInitialized();
|
|
|
|
|
|
|
|
|
|
if (Value != null)
|
|
|
|
|
{
|
|
|
|
|
GetIfNotNull(Value, 0, notNullValue =>
|
|
|
|
|
{
|
|
|
|
|
ChangeValue(notNullValue, 0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
GetIfNotNull(Value, 1, notNullValue =>
|
|
|
|
|
{
|
|
|
|
|
ChangeValue(notNullValue, 1);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Value = CreateInstance();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get value by picker index
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="index"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override DateTime? GetIndexValue(int index)
|
|
|
|
|
{
|
|
|
|
|
if (_pickerStatus[index]._hadSelectValue)
|
|
|
|
|
{
|
2020-08-14 12:43:48 +08:00
|
|
|
|
var array = Value as Array;
|
|
|
|
|
var indexValue = array.GetValue(index);
|
|
|
|
|
|
|
|
|
|
if (indexValue == null)
|
2020-07-11 23:03:13 +08:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 12:43:48 +08:00
|
|
|
|
return Convert.ToDateTime(indexValue, this.CultureInfo);
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
else if (_defaultValues[index] != null)
|
|
|
|
|
{
|
|
|
|
|
return (DateTime)_defaultValues[index];
|
|
|
|
|
}
|
2020-07-11 23:03:13 +08:00
|
|
|
|
|
|
|
|
|
return null;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void ChangeValue(DateTime value, int index = 0)
|
|
|
|
|
{
|
2020-08-14 12:43:48 +08:00
|
|
|
|
var array = Value as Array;
|
2020-07-11 23:03:13 +08:00
|
|
|
|
|
2020-08-14 12:43:48 +08:00
|
|
|
|
array.SetValue(value, index);
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
|
|
|
|
_pickerStatus[index]._hadSelectValue = true;
|
2020-08-14 12:43:48 +08:00
|
|
|
|
_pickerStatus[index]._currentShowHadSelectValue = true;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
|
|
|
|
UpdateCurrentValueAsString(index);
|
|
|
|
|
|
2020-08-14 12:43:48 +08:00
|
|
|
|
if (!IsShowTime && Picker != DatePickerType.Time)
|
2020-06-01 14:09:28 +08:00
|
|
|
|
{
|
2020-08-14 12:43:48 +08:00
|
|
|
|
if (_pickerStatus[0]._currentShowHadSelectValue && _pickerStatus[1]._currentShowHadSelectValue)
|
2020-06-01 14:09:28 +08:00
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-05 00:06:34 +08:00
|
|
|
|
|
2020-07-11 23:03:13 +08:00
|
|
|
|
public override void ClearValue(int index = 0)
|
|
|
|
|
{
|
|
|
|
|
_isSetPicker = false;
|
2020-08-14 12:43:48 +08:00
|
|
|
|
|
|
|
|
|
var array = CurrentValue as Array;
|
|
|
|
|
array.SetValue(default, 0);
|
|
|
|
|
array.SetValue(default, 1);
|
|
|
|
|
|
|
|
|
|
_pickerStatus[0]._hadSelectValue = false;
|
|
|
|
|
_pickerStatus[1]._hadSelectValue = false;
|
|
|
|
|
|
2020-07-11 23:03:13 +08:00
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-05 00:06:34 +08:00
|
|
|
|
private async Task OnInputClick(int index)
|
|
|
|
|
{
|
|
|
|
|
await _dropDown.Show();
|
|
|
|
|
|
2020-08-14 12:43:48 +08:00
|
|
|
|
// clear status
|
|
|
|
|
_pickerStatus[0]._currentShowHadSelectValue = false;
|
|
|
|
|
_pickerStatus[1]._currentShowHadSelectValue = false;
|
|
|
|
|
|
2020-07-05 00:06:34 +08:00
|
|
|
|
if (index == 0)
|
|
|
|
|
{
|
|
|
|
|
// change start picker value
|
|
|
|
|
if (!_inputStart.IsOnFocused && _pickerStatus[index]._hadSelectValue)
|
|
|
|
|
{
|
2020-08-14 12:43:48 +08:00
|
|
|
|
GetIfNotNull(Value, index, notNullValue =>
|
2020-07-11 23:03:13 +08:00
|
|
|
|
{
|
|
|
|
|
ChangePickerValue(notNullValue, index);
|
|
|
|
|
});
|
2020-07-05 00:06:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChangeFocusTarget(true, false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// change end picker value
|
|
|
|
|
if (!_inputEnd.IsOnFocused && _pickerStatus[index]._hadSelectValue)
|
|
|
|
|
{
|
2020-08-14 12:43:48 +08:00
|
|
|
|
GetIfNotNull(Value, index, notNullValue =>
|
2020-07-11 23:03:13 +08:00
|
|
|
|
{
|
|
|
|
|
ChangePickerValue(notNullValue, index);
|
|
|
|
|
});
|
2020-07-05 00:06:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChangeFocusTarget(false, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-11 23:03:13 +08:00
|
|
|
|
|
2020-08-14 12:43:48 +08:00
|
|
|
|
private void GetIfNotNull(TValue value, int index, Action<DateTime> notNullAction)
|
2020-07-06 19:09:01 +08:00
|
|
|
|
{
|
2020-08-14 12:43:48 +08:00
|
|
|
|
var array = value as Array;
|
|
|
|
|
var indexValue = array.GetValue(index);
|
|
|
|
|
|
2020-07-11 23:03:13 +08:00
|
|
|
|
if (!_isNullable)
|
|
|
|
|
{
|
2020-08-14 12:43:48 +08:00
|
|
|
|
DateTime dateTime = Convert.ToDateTime(indexValue, CultureInfo);
|
2020-07-11 23:03:13 +08:00
|
|
|
|
if (dateTime != DateTime.MinValue)
|
|
|
|
|
{
|
|
|
|
|
notNullAction?.Invoke(dateTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-14 12:43:48 +08:00
|
|
|
|
if (_isNullable && indexValue != null)
|
2020-07-11 23:03:13 +08:00
|
|
|
|
{
|
2020-08-14 12:43:48 +08:00
|
|
|
|
notNullAction?.Invoke(Convert.ToDateTime(indexValue, CultureInfo));
|
2020-07-11 23:03:13 +08:00
|
|
|
|
}
|
2020-07-06 19:09:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 12:43:48 +08:00
|
|
|
|
private TValue CreateInstance()
|
|
|
|
|
{
|
|
|
|
|
if (_isNullable)
|
|
|
|
|
{
|
|
|
|
|
return (TValue)Array.CreateInstance(typeof(DateTime?), 2).Clone();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (TValue)Array.CreateInstance(typeof(DateTime), 2).Clone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void UpdateCurrentValueAsString(int index = 0)
|
|
|
|
|
{
|
|
|
|
|
if (EditContext != null)
|
|
|
|
|
{
|
|
|
|
|
CurrentValueAsString = $"{GetInputValue(0)},{GetInputValue(1)}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool TryParseValueFromString(string value, out TValue result, out string validationErrorMessage)
|
|
|
|
|
{
|
|
|
|
|
result = default;
|
|
|
|
|
validationErrorMessage = $"{FieldIdentifier.FieldName} field isn't valid.";
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(value))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string[] values = value.Split(",");
|
|
|
|
|
|
|
|
|
|
if (values.Length != 2)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var success0 = BindConverter.TryConvertTo<DateTime>(values[0], CultureInfo, out var dateTime0);
|
|
|
|
|
var success1 = BindConverter.TryConvertTo<DateTime>(values[1], CultureInfo, out var dateTime1);
|
|
|
|
|
|
|
|
|
|
if (success0 && success1)
|
|
|
|
|
{
|
|
|
|
|
result = CreateInstance();
|
|
|
|
|
|
|
|
|
|
var array = result as Array;
|
|
|
|
|
|
|
|
|
|
array.SetValue(dateTime0, 0);
|
|
|
|
|
array.SetValue(dateTime1, 1);
|
|
|
|
|
|
|
|
|
|
validationErrorMessage = null;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|