ant-design-blazor/components/date-picker/RangePickerDefaults.cs
Andrzej Bakun 2a05064c25 fix(module:date-picker): OneOf to TValue, default value for picker, optimizations (#933)
* fix(module:DatePicker): input OnClick has new event handler

* fix(module:DateTime): remove misleading reliance on picker index

* fix(module: DatePicker & RangePicker): DefaultValue type change

DefaultValue type change to align with Value type

* fix(module:rangepicker): add default values helper with tests

* fix(module:datepicker): ChangePickerValue action declaration fix

Picker value is served based on input index (start/end)

* fix(module:DatePicker): optimization

ViewStartDate, MaxRow & MaxCol do not cause refresh if not changed

* fix(module:DatePicker): min date fix

ArgumentOutOfRangeException fix for dates before DateTime.MinValue

* fix(module:RangePicker): sorted values

Values get ordered on set

* fix(module:DatePicker): OneOf switch to TValue

* fix(module:DatePicker): code optimization and PickerValue fix

PickerValue fix gets first panel value and evaluates second panel value

* fix(module:DatePicker): default values evaluation

* fix(module:RangePicker): default picker value fix

* docs(module:DatePicker): switch to TValue for DefaultValue

* fix(module:RangePicker): other value picker fix

* fix(module:RangePicker): on value init include DefaultValue

Co-authored-by: James Yeung <shunjiey@hotmail.com>
2021-01-10 13:19:07 +08:00

96 lines
3.7 KiB
C#

using System;
namespace AntDesign
{
/// <summary>
/// Evaluates what values should be inserted to PickerValues (the date each
/// picker will focus on when first shown).
/// </summary>
public static class RangePickerDefaults
{
public static void ProcessDefaults<TValue>(TValue value, TValue defaultValue,
TValue defaultPickerValue, DateTime[] pickerValues, bool[] useDefaultPickerValue)
{
bool isNullable = false;
Type type = typeof(TValue);
if (type.IsAssignableFrom(typeof(DateTime?)) || type.IsAssignableFrom(typeof(DateTime?[])))
{
isNullable = true;
}
var defaultElementValue = Activator.CreateInstance(typeof(TValue).GetElementType());
Array valueTemp, defaultTemp;
DateTime?[] evaluatedPickerValue = new DateTime?[2];
if (isNullable)
{
if (defaultPickerValue != null)
{
evaluatedPickerValue = defaultPickerValue as DateTime?[];
}
valueTemp = value as DateTime?[];
defaultTemp = defaultValue as DateTime?[];
}
else
{
if (defaultPickerValue != null)
{
evaluatedPickerValue[0] = (defaultPickerValue as DateTime[])[0];
evaluatedPickerValue[1] = (defaultPickerValue as DateTime[])[1];
}
else
{
evaluatedPickerValue = new DateTime?[] { default(DateTime), default(DateTime) };
}
valueTemp = value as DateTime[];
defaultTemp = defaultValue as DateTime[];
}
useDefaultPickerValue[0] = EvaluateDefault(0, defaultElementValue, isNullable, evaluatedPickerValue, valueTemp, defaultTemp, pickerValues);
useDefaultPickerValue[1] = EvaluateDefault(1, defaultElementValue, isNullable, evaluatedPickerValue, valueTemp, defaultTemp, pickerValues);
if (useDefaultPickerValue[0])
{
pickerValues[0] = evaluatedPickerValue[0] ?? pickerValues[0];
}
if (useDefaultPickerValue[1])
{
pickerValues[1] = evaluatedPickerValue[1] ?? pickerValues[1];
}
}
private static bool EvaluateDefault(int index, object defaultElementValue, bool isNullable,
DateTime?[] evaluatedPickerValue, Array valueTemp, Array defaultTemp, DateTime[] pickerValues)
{
if (evaluatedPickerValue[index].Equals(defaultElementValue))
{
if (valueTemp != null && !(valueTemp.GetValue(index)?.Equals(defaultElementValue) ?? true))
{
evaluatedPickerValue[index] = (DateTime?)valueTemp.GetValue(index);
}
else if (defaultTemp != null && !(defaultTemp.GetValue(index)?.Equals(defaultElementValue) ?? true))
{
evaluatedPickerValue[index] = (DateTime?)defaultTemp.GetValue(index);
}
else if (!isNullable && valueTemp != null && (valueTemp.GetValue(index)?.Equals(defaultElementValue) ?? true))
{
evaluatedPickerValue[index] = (DateTime?)valueTemp.GetValue(index);
}
else
{
if (index == 1)
{
pickerValues[1] = pickerValues[0];
evaluatedPickerValue[1] = pickerValues[1];
}
return false;
}
}
return true;
}
}
}