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-07-11 23:03:13 +08:00
|
|
|
|
public partial class RangePicker<TValue> : DatePickerBase<TValue[]>
|
2020-06-01 14:09:28 +08:00
|
|
|
|
{
|
|
|
|
|
public RangePicker()
|
|
|
|
|
{
|
|
|
|
|
IsRange = true;
|
|
|
|
|
|
2020-07-11 23:03:13 +08:00
|
|
|
|
Value = new TValue[2];
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
|
|
|
|
DisabledDate = (date) =>
|
|
|
|
|
{
|
|
|
|
|
if (_pickerStatus[0]._hadSelectValue && _inputEnd.IsOnFocused)
|
|
|
|
|
{
|
2020-07-11 23:03:13 +08:00
|
|
|
|
DateTime? value = null;
|
|
|
|
|
GetIfNotNull(Value[0], notNullValue =>
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
GetIfNotNull(Value[1], notNullValue =>
|
|
|
|
|
{
|
|
|
|
|
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-07-11 23:03:13 +08:00
|
|
|
|
if (BindConverter.TryConvertTo(args.Value.ToString(), CultureInfo, out TValue changeValue))
|
2020-06-01 14:09:28 +08:00
|
|
|
|
{
|
|
|
|
|
Value[index] = changeValue;
|
2020-07-11 23:03:13 +08:00
|
|
|
|
|
|
|
|
|
GetIfNotNull(changeValue, (notNullValue) =>
|
|
|
|
|
{
|
|
|
|
|
_pickerValues[index] = notNullValue;
|
|
|
|
|
});
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get value by picker index
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="index"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public override DateTime? GetIndexValue(int index)
|
|
|
|
|
{
|
|
|
|
|
if (_pickerStatus[index]._hadSelectValue)
|
|
|
|
|
{
|
2020-07-11 23:03:13 +08:00
|
|
|
|
if (Value[index] == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Convert.ToDateTime(Value[index], 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-07-11 23:03:13 +08:00
|
|
|
|
bool result = BindConverter.TryConvertTo<TValue>(
|
|
|
|
|
value.ToString(CultureInfo), CultureInfo, out var dateTime);
|
|
|
|
|
|
|
|
|
|
if (result)
|
|
|
|
|
{
|
|
|
|
|
Value[index] = dateTime;
|
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
|
|
|
|
_pickerStatus[index]._hadSelectValue = true;
|
|
|
|
|
|
|
|
|
|
UpdateCurrentValueAsString(index);
|
|
|
|
|
|
|
|
|
|
if (IsRange && !IsShowTime && Picker != DatePickerType.Time)
|
|
|
|
|
{
|
|
|
|
|
if (_pickerStatus[0]._hadSelectValue && _pickerStatus[1]._hadSelectValue)
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (!IsShowTime && Picker != DatePickerType.Time)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
CurrentValue[0] = default;
|
|
|
|
|
CurrentValue[1] = default;
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-05 00:06:34 +08:00
|
|
|
|
private async Task OnInputClick(int index)
|
|
|
|
|
{
|
|
|
|
|
await _dropDown.Show();
|
|
|
|
|
|
|
|
|
|
if (index == 0)
|
|
|
|
|
{
|
|
|
|
|
// change start picker value
|
|
|
|
|
if (!_inputStart.IsOnFocused && _pickerStatus[index]._hadSelectValue)
|
|
|
|
|
{
|
2020-07-11 23:03:13 +08:00
|
|
|
|
GetIfNotNull(Value[index], notNullValue =>
|
|
|
|
|
{
|
|
|
|
|
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-07-11 23:03:13 +08:00
|
|
|
|
GetIfNotNull(Value[index], notNullValue =>
|
|
|
|
|
{
|
|
|
|
|
ChangePickerValue(notNullValue, index);
|
|
|
|
|
});
|
2020-07-05 00:06:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ChangeFocusTarget(false, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-07-11 23:03:13 +08:00
|
|
|
|
|
|
|
|
|
private void GetIfNotNull(TValue value, Action<DateTime> notNullAction)
|
2020-07-06 19:09:01 +08:00
|
|
|
|
{
|
2020-07-11 23:03:13 +08:00
|
|
|
|
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-07-06 19:09:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|