2020-06-01 14:09:28 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2020-07-05 00:06:34 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2021-05-07 15:31:13 +08:00
|
|
|
|
using AntDesign.Core.Extensions;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components;
|
2021-02-20 17:54:12 +08:00
|
|
|
|
using Microsoft.AspNetCore.Components.Web;
|
2021-04-28 11:54:19 +08:00
|
|
|
|
using Microsoft.JSInterop;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2021-01-10 13:19:07 +08:00
|
|
|
|
private TValue _value;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the value of the input. This should be used with two-way binding.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <example>
|
|
|
|
|
/// @bind-Value="model.PropertyName"
|
|
|
|
|
/// </example>
|
2020-10-29 01:20:11 +08:00
|
|
|
|
[Parameter]
|
2021-01-10 13:19:07 +08:00
|
|
|
|
public sealed override TValue Value
|
|
|
|
|
{
|
|
|
|
|
get { return _value; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
TValue orderedValue = SortValue(value);
|
|
|
|
|
var hasChanged = !EqualityComparer<TValue>.Default.Equals(orderedValue, Value);
|
|
|
|
|
if (hasChanged)
|
|
|
|
|
{
|
|
|
|
|
_value = orderedValue;
|
|
|
|
|
OnValueChange(orderedValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-29 01:20:11 +08:00
|
|
|
|
|
2021-01-10 13:19:07 +08:00
|
|
|
|
private DateTime[] _pickerValuesAfterInit = new DateTime[2];
|
|
|
|
|
|
|
|
|
|
[Parameter]
|
|
|
|
|
public EventCallback<DateRangeChangedEventArgs> OnChange { get; set; }
|
2021-01-12 12:48:45 +08:00
|
|
|
|
|
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;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 11:54:19 +08:00
|
|
|
|
private async Task OnInputClick(int index)
|
2020-06-01 14:09:28 +08:00
|
|
|
|
{
|
2021-06-28 11:28:18 +08:00
|
|
|
|
_duringFocus = false;
|
2021-04-28 11:54:19 +08:00
|
|
|
|
if (_duringManualInput)
|
2020-06-01 14:09:28 +08:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-05-07 15:31:13 +08:00
|
|
|
|
_openingOverlay = !_dropDown.IsOverlayShow();
|
|
|
|
|
|
2021-04-28 11:54:19 +08:00
|
|
|
|
//Reset Picker to default in case the picker value was changed
|
|
|
|
|
//but no value was selected (for example when a user clicks next
|
|
|
|
|
//month but does not select any value)
|
|
|
|
|
if (UseDefaultPickerValue[index] && DefaultPickerValue != null)
|
|
|
|
|
{
|
|
|
|
|
PickerValues[index] = _pickerValuesAfterInit[index];
|
|
|
|
|
}
|
|
|
|
|
await _dropDown.Show();
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
2021-04-28 11:54:19 +08:00
|
|
|
|
// clear status
|
|
|
|
|
_pickerStatus[0]._currentShowHadSelectValue = false;
|
|
|
|
|
_pickerStatus[1]._currentShowHadSelectValue = false;
|
2020-07-11 23:03:13 +08:00
|
|
|
|
|
2021-04-28 11:54:19 +08:00
|
|
|
|
if (index == 0)
|
2020-08-14 12:43:48 +08:00
|
|
|
|
{
|
2021-04-28 11:54:19 +08:00
|
|
|
|
// change start picker value
|
|
|
|
|
if (!_inputStart.IsOnFocused && _pickerStatus[index]._hadSelectValue && !UseDefaultPickerValue[index])
|
2021-02-20 17:54:12 +08:00
|
|
|
|
{
|
2021-04-28 11:54:19 +08:00
|
|
|
|
GetIfNotNull(Value, index, notNullValue =>
|
|
|
|
|
{
|
|
|
|
|
ChangePickerValue(notNullValue, index);
|
|
|
|
|
});
|
2021-02-20 17:54:12 +08:00
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
|
2021-04-28 11:54:19 +08:00
|
|
|
|
ChangeFocusTarget(true, false);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// change end picker value
|
|
|
|
|
if (!_inputEnd.IsOnFocused && _pickerStatus[index]._hadSelectValue && !UseDefaultPickerValue[index])
|
2021-04-14 23:17:57 +08:00
|
|
|
|
{
|
2021-04-28 11:54:19 +08:00
|
|
|
|
GetIfNotNull(Value, index, notNullValue =>
|
2021-04-14 23:17:57 +08:00
|
|
|
|
{
|
2021-04-28 11:54:19 +08:00
|
|
|
|
ChangePickerValue(notNullValue, index);
|
2021-04-14 23:17:57 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 11:54:19 +08:00
|
|
|
|
ChangeFocusTarget(false, true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private DateTime? _cacheDuringInput;
|
|
|
|
|
private DateTime _pickerValueCache;
|
|
|
|
|
|
|
|
|
|
protected void OnInput(ChangeEventArgs args, int index = 0)
|
|
|
|
|
{
|
|
|
|
|
if (args == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var array = Value as Array;
|
|
|
|
|
if (!_duringManualInput)
|
|
|
|
|
{
|
|
|
|
|
_duringManualInput = true;
|
|
|
|
|
_cacheDuringInput = array.GetValue(index) as DateTime?;
|
|
|
|
|
_pickerValueCache = PickerValues[index];
|
|
|
|
|
}
|
|
|
|
|
if (FormatAnalyzer.TryPickerStringConvert(args.Value.ToString(), out DateTime changeValue, false))
|
|
|
|
|
{
|
|
|
|
|
array.SetValue(changeValue, index);
|
|
|
|
|
|
|
|
|
|
ChangePickerValue(changeValue, index);
|
|
|
|
|
|
2021-06-10 10:09:19 +08:00
|
|
|
|
if (_isNotifyFieldChanged && (Form?.ValidateOnChange == true))
|
|
|
|
|
{
|
|
|
|
|
EditContext?.NotifyFieldChanged(FieldIdentifier);
|
|
|
|
|
}
|
2021-04-28 11:54:19 +08:00
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
StateHasChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-20 17:54:12 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Method is called via EventCallBack if the keyboard key is no longer pressed inside the Input element.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="e">Contains the key (combination) which was pressed inside the Input element</param>
|
|
|
|
|
/// <param name="index">Refers to picker index - 0 for starting date, 1 for ending date</param>
|
2021-04-28 11:54:19 +08:00
|
|
|
|
protected async Task OnKeyDown(KeyboardEventArgs e, int index)
|
2021-02-20 17:54:12 +08:00
|
|
|
|
{
|
|
|
|
|
if (e == null) throw new ArgumentNullException(nameof(e));
|
|
|
|
|
|
|
|
|
|
var key = e.Key.ToUpperInvariant();
|
2021-05-07 15:31:13 +08:00
|
|
|
|
if (key == "ENTER" || key == "TAB" || key == "ESCAPE")
|
2021-02-20 17:54:12 +08:00
|
|
|
|
{
|
2021-04-28 11:54:19 +08:00
|
|
|
|
_duringManualInput = false;
|
2021-02-20 17:54:12 +08:00
|
|
|
|
var input = (index == 0 ? _inputStart : _inputEnd);
|
|
|
|
|
if (string.IsNullOrWhiteSpace(input.Value))
|
2021-04-28 11:54:19 +08:00
|
|
|
|
ClearValue(index, false);
|
|
|
|
|
else if (!await TryApplyInputValue(index, input.Value))
|
|
|
|
|
return;
|
|
|
|
|
|
2021-05-07 15:31:13 +08:00
|
|
|
|
if (key == "ESCAPE" && _dropDown.IsOverlayShow())
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
await Js.FocusAsync(input.Ref);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 11:54:19 +08:00
|
|
|
|
if (index == 1)
|
|
|
|
|
{
|
|
|
|
|
if (key != "TAB")
|
2021-02-20 17:54:12 +08:00
|
|
|
|
{
|
2021-04-28 11:54:19 +08:00
|
|
|
|
//needed only in wasm, details: https://github.com/dotnet/aspnetcore/issues/30070
|
|
|
|
|
await Task.Yield();
|
|
|
|
|
await Js.InvokeVoidAsync(JSInteropConstants.InvokeTabKey);
|
|
|
|
|
Close();
|
2021-02-20 17:54:12 +08:00
|
|
|
|
}
|
2021-04-28 11:54:19 +08:00
|
|
|
|
else if (!e.ShiftKey)
|
|
|
|
|
{
|
|
|
|
|
Close();
|
2021-05-07 15:31:13 +08:00
|
|
|
|
AutoFocus = false;
|
2021-04-28 11:54:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (index == 0)
|
|
|
|
|
{
|
|
|
|
|
if (key == "TAB" && e.ShiftKey)
|
|
|
|
|
{
|
|
|
|
|
Close();
|
|
|
|
|
AutoFocus = false;
|
|
|
|
|
}
|
|
|
|
|
else if (key != "TAB")
|
2021-02-20 17:54:12 +08:00
|
|
|
|
{
|
|
|
|
|
await Blur(0);
|
|
|
|
|
await Focus(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-28 11:54:19 +08:00
|
|
|
|
return;
|
2021-02-20 17:54:12 +08:00
|
|
|
|
}
|
|
|
|
|
if (key == "ARROWDOWN" && !_dropDown.IsOverlayShow())
|
|
|
|
|
{
|
|
|
|
|
await _dropDown.Show();
|
2021-04-28 11:54:19 +08:00
|
|
|
|
return;
|
2021-02-20 17:54:12 +08:00
|
|
|
|
}
|
|
|
|
|
if (key == "ARROWUP" && _dropDown.IsOverlayShow())
|
|
|
|
|
{
|
|
|
|
|
Close();
|
2021-04-28 11:54:19 +08:00
|
|
|
|
await Task.Yield();
|
|
|
|
|
AutoFocus = true;
|
|
|
|
|
return;
|
2021-02-20 17:54:12 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 11:54:19 +08:00
|
|
|
|
private async Task<bool> TryApplyInputValue(int index, string inputValue)
|
|
|
|
|
{
|
|
|
|
|
if (FormatAnalyzer.TryPickerStringConvert(inputValue, out DateTime changeValue, false))
|
|
|
|
|
{
|
|
|
|
|
var array = Value as Array;
|
|
|
|
|
array.SetValue(changeValue, index);
|
|
|
|
|
var validationSuccess = await ValidateRange(index, changeValue, array);
|
|
|
|
|
if (OnChange.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
await OnChange.InvokeAsync(new DateRangeChangedEventArgs
|
|
|
|
|
{
|
|
|
|
|
Dates = new DateTime?[] { array.GetValue(0) as DateTime?, array.GetValue(1) as DateTime? },
|
|
|
|
|
DateStrings = new string[] { GetInputValue(0), GetInputValue(1) }
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return validationSuccess;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
private async Task<bool> ValidateRange(int index, DateTime newDate, Array array)
|
|
|
|
|
{
|
|
|
|
|
if (index == 0 && array.GetValue(1) is not null && ((DateTime)array.GetValue(1)).CompareTo(newDate) < 0)
|
|
|
|
|
{
|
|
|
|
|
ClearValue(1, false);
|
|
|
|
|
await Blur(0);
|
|
|
|
|
await Focus(1);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else if (index == 1)
|
|
|
|
|
{
|
|
|
|
|
if (array.GetValue(0) is not null && newDate.CompareTo((DateTime)array.GetValue(0)) < 0)
|
|
|
|
|
{
|
|
|
|
|
ClearValue(0, false);
|
|
|
|
|
await Blur(1);
|
|
|
|
|
await Focus(0);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else if (array.GetValue(0) is null)
|
|
|
|
|
{
|
|
|
|
|
await Blur(1);
|
|
|
|
|
await Focus(0);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-28 11:28:18 +08:00
|
|
|
|
|
2021-02-20 17:54:12 +08:00
|
|
|
|
private async Task OnFocus(int index)
|
|
|
|
|
{
|
2021-06-28 11:28:18 +08:00
|
|
|
|
_duringFocus = true;
|
2021-02-20 17:54:12 +08:00
|
|
|
|
if (index == 0)
|
|
|
|
|
{
|
|
|
|
|
if (!_inputStart.IsOnFocused)
|
|
|
|
|
{
|
|
|
|
|
await Blur(1);
|
|
|
|
|
await Focus(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2021-04-15 14:19:26 +08:00
|
|
|
|
{
|
2021-02-20 17:54:12 +08:00
|
|
|
|
if (!_inputEnd.IsOnFocused)
|
|
|
|
|
{
|
|
|
|
|
await Blur(0);
|
|
|
|
|
await Focus(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-28 11:54:19 +08:00
|
|
|
|
AutoFocus = true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-28 11:28:18 +08:00
|
|
|
|
protected override async Task OnBlur(int index)
|
2021-04-28 11:54:19 +08:00
|
|
|
|
{
|
2021-06-28 11:28:18 +08:00
|
|
|
|
//Await for Focus event - if it is going to happen, it will be
|
|
|
|
|
//right after OnBlur. Best way to achieve that is to wait.
|
|
|
|
|
//Task.Yield() does not work here.
|
|
|
|
|
await Task.Delay(1);
|
|
|
|
|
if (_duringFocus)
|
|
|
|
|
{
|
|
|
|
|
_duringFocus = false;
|
|
|
|
|
_shouldRender = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-05-07 15:31:13 +08:00
|
|
|
|
if (_openingOverlay)
|
2021-06-28 11:28:18 +08:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-05-07 15:31:13 +08:00
|
|
|
|
|
2021-04-28 11:54:19 +08:00
|
|
|
|
if (_duringManualInput)
|
|
|
|
|
{
|
|
|
|
|
var array = Value as Array;
|
|
|
|
|
|
|
|
|
|
if (!array.GetValue(index).Equals(_cacheDuringInput))
|
|
|
|
|
{
|
|
|
|
|
//reset picker to Value
|
|
|
|
|
if (IsNullable)
|
|
|
|
|
array.SetValue(_cacheDuringInput, index);
|
|
|
|
|
else
|
|
|
|
|
array.SetValue(_cacheDuringInput.GetValueOrDefault(), index);
|
|
|
|
|
|
|
|
|
|
_pickerStatus[index]._hadSelectValue = !(Value is null && (DefaultValue is not null || DefaultPickerValue is not null));
|
|
|
|
|
ChangePickerValue(_pickerValueCache, index);
|
|
|
|
|
}
|
|
|
|
|
_duringManualInput = false;
|
|
|
|
|
}
|
|
|
|
|
AutoFocus = false;
|
2021-06-28 11:28:18 +08:00
|
|
|
|
return;
|
2021-02-20 17:54:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-14 12:43:48 +08:00
|
|
|
|
protected override void OnInitialized()
|
|
|
|
|
{
|
|
|
|
|
base.OnInitialized();
|
2021-01-10 13:19:07 +08:00
|
|
|
|
RangePickerDefaults.ProcessDefaults(Value, DefaultValue, DefaultPickerValue, PickerValues, UseDefaultPickerValue);
|
|
|
|
|
_pickerValuesAfterInit[0] = PickerValues[0];
|
|
|
|
|
_pickerValuesAfterInit[1] = PickerValues[1];
|
2021-02-05 10:27:32 +08:00
|
|
|
|
if (_value == null)
|
|
|
|
|
{
|
|
|
|
|
_value = CreateInstance();
|
|
|
|
|
ValueChanged.InvokeAsync(_value);
|
|
|
|
|
}
|
2021-01-10 13:19:07 +08:00
|
|
|
|
}
|
2020-08-14 12:43:48 +08:00
|
|
|
|
|
2021-01-10 13:19:07 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handle change of values.
|
2021-01-12 12:48:45 +08:00
|
|
|
|
/// When values are changed, PickerValues should point to those new values
|
2021-01-10 13:19:07 +08:00
|
|
|
|
/// or current date if no values were passed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
|
|
|
|
protected override void OnValueChange(TValue value)
|
|
|
|
|
{
|
|
|
|
|
base.OnValueChange(value);
|
|
|
|
|
//reset all only if not changed using picker
|
2021-01-12 12:48:45 +08:00
|
|
|
|
if (_inputStart?.IsOnFocused != true && _inputEnd?.IsOnFocused != true) // is null or false
|
2020-08-14 12:43:48 +08:00
|
|
|
|
{
|
2021-01-10 13:19:07 +08:00
|
|
|
|
UseDefaultPickerValue[0] = false;
|
|
|
|
|
UseDefaultPickerValue[1] = false;
|
|
|
|
|
_pickerStatus[0]._hadSelectValue = true;
|
|
|
|
|
_pickerStatus[1]._hadSelectValue = true;
|
2020-08-14 12:43:48 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2021-01-10 13:19:07 +08:00
|
|
|
|
if (Value != null)
|
2020-06-01 14:09:28 +08:00
|
|
|
|
{
|
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-11-25 11:08:12 +08:00
|
|
|
|
return Convert.ToDateTime(indexValue, CultureInfo);
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
2021-01-10 13:19:07 +08:00
|
|
|
|
else if (!IsTypedValueNull(DefaultValue, index, out var defaultValue))
|
2020-06-01 14:09:28 +08:00
|
|
|
|
{
|
2021-01-10 13:19:07 +08:00
|
|
|
|
return defaultValue;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
2020-07-11 23:03:13 +08:00
|
|
|
|
return null;
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-10 13:19:07 +08:00
|
|
|
|
private static bool IsTypedValueNull(TValue value, int index, out DateTime? outValue)
|
|
|
|
|
{
|
|
|
|
|
outValue = (DateTime?)(value as Array)?.GetValue(index);
|
|
|
|
|
return outValue == null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-01 14:09:28 +08:00
|
|
|
|
public override void ChangeValue(DateTime value, int index = 0)
|
|
|
|
|
{
|
2021-01-10 13:19:07 +08:00
|
|
|
|
bool isValueInstantiated = Value == null;
|
|
|
|
|
if (isValueInstantiated)
|
|
|
|
|
{
|
|
|
|
|
Value = CreateInstance();
|
|
|
|
|
}
|
|
|
|
|
UseDefaultPickerValue[index] = false;
|
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
|
|
|
|
|
2021-01-10 13:19:07 +08:00
|
|
|
|
//if Value was just now instantiated then set the other index to existing DefaultValue
|
|
|
|
|
if (isValueInstantiated && IsRange && DefaultValue != null)
|
|
|
|
|
{
|
|
|
|
|
var arrayDefault = DefaultValue as Array;
|
|
|
|
|
int oppositeIndex = index == 1 ? 0 : 1;
|
|
|
|
|
array.SetValue(arrayDefault.GetValue(oppositeIndex), oppositeIndex);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
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-10-29 01:20:11 +08:00
|
|
|
|
|
|
|
|
|
if (OnChange.HasDelegate)
|
|
|
|
|
{
|
|
|
|
|
OnChange.InvokeAsync(new DateRangeChangedEventArgs
|
|
|
|
|
{
|
|
|
|
|
Dates = new DateTime?[] { array.GetValue(0) as DateTime?, array.GetValue(1) as DateTime? },
|
|
|
|
|
DateStrings = new string[] { GetInputValue(0), GetInputValue(1) }
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-06-10 10:09:19 +08:00
|
|
|
|
|
|
|
|
|
if (_isNotifyFieldChanged && (Form?.ValidateOnChange == true))
|
|
|
|
|
{
|
|
|
|
|
EditContext?.NotifyFieldChanged(FieldIdentifier);
|
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
2020-07-05 00:06:34 +08:00
|
|
|
|
|
2021-04-28 11:54:19 +08:00
|
|
|
|
public override void ClearValue(int index = -1, bool closeDropdown = true)
|
2020-07-11 23:03:13 +08:00
|
|
|
|
{
|
|
|
|
|
_isSetPicker = false;
|
2020-08-14 12:43:48 +08:00
|
|
|
|
|
|
|
|
|
var array = CurrentValue as Array;
|
2021-04-28 11:54:19 +08:00
|
|
|
|
int[] indexToClear = index == -1 ? new[] { 0, 1 } : new[] { index };
|
2020-08-14 12:43:48 +08:00
|
|
|
|
|
2021-04-28 11:54:19 +08:00
|
|
|
|
string[] pickerHolders = new string[2];
|
|
|
|
|
(pickerHolders[0], pickerHolders[1]) = DatePickerPlaceholder.GetRangePlaceHolderByType(_pickerStatus[0]._initPicker, Locale);
|
2020-08-14 12:43:48 +08:00
|
|
|
|
|
2021-04-28 11:54:19 +08:00
|
|
|
|
foreach (var i in indexToClear)
|
2020-07-05 00:06:34 +08:00
|
|
|
|
{
|
2021-04-28 11:54:19 +08:00
|
|
|
|
if (!IsNullable && DefaultValue != null)
|
2020-07-05 00:06:34 +08:00
|
|
|
|
{
|
2021-04-28 11:54:19 +08:00
|
|
|
|
var defaults = DefaultValue as Array;
|
|
|
|
|
array.SetValue(defaults.GetValue(i), i);
|
2020-07-05 00:06:34 +08:00
|
|
|
|
}
|
2021-04-28 11:54:19 +08:00
|
|
|
|
else
|
2020-07-05 00:06:34 +08:00
|
|
|
|
{
|
2021-04-28 11:54:19 +08:00
|
|
|
|
array.SetValue(default, i);
|
2020-07-05 00:06:34 +08:00
|
|
|
|
}
|
2021-04-28 11:54:19 +08:00
|
|
|
|
_placeholders[i] = pickerHolders[i];
|
|
|
|
|
_pickerStatus[i]._hadSelectValue = false;
|
2020-07-05 00:06:34 +08:00
|
|
|
|
}
|
2021-04-28 11:54:19 +08:00
|
|
|
|
|
|
|
|
|
if (closeDropdown)
|
|
|
|
|
Close();
|
2021-06-01 18:16:07 +08:00
|
|
|
|
if (OnClearClick.HasDelegate)
|
|
|
|
|
OnClearClick.InvokeAsync(null);
|
2020-07-05 00:06:34 +08:00
|
|
|
|
}
|
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);
|
|
|
|
|
|
2021-01-10 13:19:07 +08:00
|
|
|
|
if (!IsNullable)
|
2020-07-11 23:03:13 +08:00
|
|
|
|
{
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-10 13:19:07 +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()
|
|
|
|
|
{
|
2021-02-20 17:54:12 +08:00
|
|
|
|
if (DefaultValue is not null)
|
|
|
|
|
return (TValue)(DefaultValue as Array).Clone();
|
|
|
|
|
|
2021-01-10 13:19:07 +08:00
|
|
|
|
if (IsNullable)
|
2020-08-14 12:43:48 +08:00
|
|
|
|
{
|
|
|
|
|
return (TValue)Array.CreateInstance(typeof(DateTime?), 2).Clone();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return (TValue)Array.CreateInstance(typeof(DateTime), 2).Clone();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2021-05-07 15:31:13 +08:00
|
|
|
|
|
|
|
|
|
private void OverlayVisibleChange(bool visible)
|
|
|
|
|
{
|
|
|
|
|
OnOpenChange.InvokeAsync(visible);
|
|
|
|
|
_openingOverlay = false;
|
|
|
|
|
}
|
2020-06-01 14:09:28 +08:00
|
|
|
|
}
|
|
|
|
|
}
|