ant-design-blazor/components/date-picker/RangePickerDefaults.cs
Alex Kryvdyk 6b0a8f45de
feat(module: datepicker): Add DateTimeOffset, DateOnly, TimeOnly support (#3443)
* fix:(module:datepicker): OnChange event in Docs project

add(module:datepicker): generic OnPanelChange event

add(module:datepicker): generic OnChange event

fix(module:datepicker): DateTimeKind is not preserved

add(module:datepicker): DateTimeOffset unit tests

add(module:datepicker): add DateTimeOffset to range picker unit tests

add(module:datepicker): refactor unit tests

fix(module:datepicker): DateTime.Kind is not preserved

feat(module: datepicker). Add DateTimeOffset, DateOnly, TimeOnly support

* fix(module:datepicker): unit test fails

* fix: moment helper test fails in VS Test Explorer in some cases

See https://xunit.net/faq/theory-data-stability-in-vs

* update the docs and demo

---------

Co-authored-by: James Yeung <shunjiey@hotmail.com>
2023-10-19 00:44:03 +08:00

89 lines
3.4 KiB
C#

using System;
using AntDesign.Internal;
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)
{
var isNullable = InternalConvert.IsNullable<TValue>();
var defaultElementValue = Activator.CreateInstance(typeof(TValue).GetElementType());
DateTime?[] evaluatedPickerValue = new DateTime?[2];
var valueTemp = InternalConvert.ToDateTimeArray(value);
var defaultTemp = InternalConvert.ToDateTimeArray(defaultValue);
if (isNullable)
{
if (defaultPickerValue != null)
{
evaluatedPickerValue = InternalConvert.ToDateTimeArray(defaultPickerValue);
}
}
else
{
if (defaultPickerValue != null)
{
evaluatedPickerValue = InternalConvert.ToDateTimeArray(defaultPickerValue);
}
else
{
evaluatedPickerValue = new DateTime?[] { default(DateTime), default(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;
}
}
}