Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
小小的重构了一下
Browse files Browse the repository at this point in the history
  • Loading branch information
teacher-zhou committed Nov 23, 2023
1 parent f6fd8c3 commit 86c4725
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,30 @@
```
")
</CodeContent>
</Example>
<Example Title="年份选择器">
<Description>
用于年份的选择。用户仅需输入年份信息时使用,常用于如年账单等按年记录数据的查询场景。
</Description>
<RunContent>
<TDatePicker @bind-Value="@Year" ViewMode="DatePickerViewMode.Year"/>
</RunContent>
<CodeContent>
@Code.Create(@"
```cshtml-razor
<TDatePicker @bind-Value=""@Year"" ViewMode=""DatePickerViewMode.Year""/>
@code{
DateOnly? Year { get; set; }
}
```
")
</CodeContent>
</Example>
</PageLayout>

@code{
DateTime? TestDate{ get; set; }
}

DateOnly? Year{get;set;}
}

39 changes: 17 additions & 22 deletions src/TDesign/Components/Forms/TDatePicker.build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ void BuildYearBody(RenderTreeBuilder builder)
builder.OpenElement(0, "tr");
builder.AddAttribute(1, "class", "t-date-picker__table-month-row");
var sequence = 0;
for (int i = CurrentYear-5; i <= CurrentYear+5; i++)
for (int i = CurrentYear - 5; i <= CurrentYear + 5; i++)
{
var currentDate = new DateOnly(CurrentYear, 1, 1);
var currentDate = new DateTime(CurrentYear, 1, 1);
var year = i;

BuildBodyCell(builder, $"{year}", isNow: IsToday(currentDate), value: year);
BuildBodyCell(builder, $"{year}", currentDate, isNow: IsToday(currentDate));

if (i % 4 == 0)
{
Expand All @@ -125,9 +125,9 @@ void BuildMonthBody(RenderTreeBuilder builder)
{
var month = i;

var currentDate = new DateOnly(CurrentYear, month, 1);
var currentDate = new DateTime(CurrentYear, month, 1);

BuildBodyCell(builder, $"{i}", isNow: IsToday(currentDate), value: month);
BuildBodyCell(builder, $"{i}", value: currentDate, isNow: IsToday(currentDate));

if (i % 4 == 0)
{
Expand Down Expand Up @@ -164,9 +164,7 @@ void BuildDateBody(RenderTreeBuilder builder)
{
var day = i + lastDays.Day;

//var lastMonthDateTime = new DateTime(lastDays.Year, lastDays.Month, day);

BuildBodyCell(builder, day.ToString(), true);
BuildBodyCell(builder, day.ToString(),new(lastDays.Year, lastDays.Month, day), true);
}
#endregion

Expand All @@ -176,7 +174,7 @@ void BuildDateBody(RenderTreeBuilder builder)
{
var day = i + 1;

BuildBodyCell(builder, day.ToString(), isNow: IsToday(day), value: day);
BuildBodyCell(builder, day.ToString(), isNow: IsToday(day), value: new(CurrentYear, CurrentMonth, day));

if ((day + findLastMonthDayIndex) % 7 == 0)
{
Expand All @@ -195,7 +193,7 @@ void BuildDateBody(RenderTreeBuilder builder)
{
var day = i + 1;

BuildBodyCell(builder, day.ToString(), true);
BuildBodyCell(builder, day.ToString(), new(CurrentYear, CurrentMonth, day), true);
}
#endregion

Expand Down Expand Up @@ -258,12 +256,12 @@ Task Prev(MouseEventArgs e)
{
case DatePickerViewMode.Year:
case DatePickerViewMode.Month:
CurrentValue.AddYears(-1);
CurrentValueAsDateTime.AddYears(-1);
break;
case DatePickerViewMode.Date:
case DatePickerViewMode.Week:
case DatePickerViewMode.Quater:
CurrentValue.AddMonths(-1);
CurrentValueAsDateTime.AddMonths(-1);
break;
default:
throw new ArgumentOutOfRangeException(nameof(ViewMode), $"不在可选的视图范围");
Expand All @@ -276,12 +274,12 @@ Task Next(MouseEventArgs e)
{
case DatePickerViewMode.Year:
case DatePickerViewMode.Month:
CurrentValue.AddYears(1);
CurrentValueAsDateTime.AddYears(1);
break;
case DatePickerViewMode.Date:
case DatePickerViewMode.Week:
case DatePickerViewMode.Quater:
CurrentValue.AddMonths(1);
CurrentValueAsDateTime.AddMonths(1);
break;
default:
throw new ArgumentOutOfRangeException(nameof(ViewMode), $"不在可选的视图范围");
Expand All @@ -290,7 +288,7 @@ Task Next(MouseEventArgs e)
}
Task Current(MouseEventArgs e)
{
CurrentValue = DateTime.Now;
CurrentValueAsDateTime = DateTime.Now;
return this.Refresh();
}

Expand All @@ -308,7 +306,7 @@ void BuildYearSelectControl(RenderTreeBuilder builder)
.Attribute(m => m.ValueChanged, HtmlHelper.Instance.Callback().Create<int>(this, value => ChangeYearMonth(value, CurrentMonth)))
.Content(options =>
{
for (int i = CurrentValue.AddYears(-5).Year; i <= CurrentValue.AddYears(5).Year; i++)
for (int i = CurrentValueAsDateTime.AddYears(-5).Year; i <= CurrentValueAsDateTime.AddYears(5).Year; i++)
{
var index = i;
options.Component<TSelectOption<int>>(sequence: 100)
Expand Down Expand Up @@ -337,8 +335,6 @@ void BuildMonthSelectControl(RenderTreeBuilder builder)
{
var index = i;
//var currentMonth = new DateOnly(CurrentYear, index, 1);
options.Component<TSelectOption<int>>(sequence: i)
.Attribute(m => m.Value, index)
.Attribute(m => m.Label, $"{index}")
Expand All @@ -360,16 +356,15 @@ void BuildMonthSelectControl(RenderTreeBuilder builder)
/// <param name="disabled"></param>
/// <param name="isNow"></param>
/// <param name="value">取决于视图模式的值。</param>
void BuildBodyCell(RenderTreeBuilder builder, string content, bool disabled = false, bool isNow = default, int value = 1)
void BuildBodyCell(RenderTreeBuilder builder, string content, DateTime value, bool disabled = false, bool isNow = default)
{
var selectedDate = new DateTime(CurrentYear, CurrentMonth, value);

builder.AddContent(0, tr =>
{
tr.Element("td", "t-date-picker__cell")
.Class("t-date-picker__cell--now", isNow)
.Class("t-date-picker__cell--active", !disabled && SelectedDates.Contains(selectedDate))
.Callback<MouseEventArgs>("onclick", this, e => SelectItems(new List<DateTime>() { selectedDate }))
.Class("t-date-picker__cell--active", !disabled && SelectedDates.Contains(value))
.Callback<MouseEventArgs>("onclick", this, e => SelectItems(new List<DateTime>() { value }))
.Content(inner =>
{
inner.Div("t-date-picker__cell-inner").Content(content).Close();
Expand Down
74 changes: 26 additions & 48 deletions src/TDesign/Components/Forms/TDatePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
/// </summary>
public partial class TDatePicker<TValue> : TDesignInputComonentBase<TValue>
{
readonly Type[] Support_Types = new[] { typeof(DateOnly), typeof(DateTime), typeof(DateTimeOffset?), typeof(DateOnly?), typeof(DateTime?), typeof(DateTimeOffset?) };
readonly static Type[] Support_Types = new[] { typeof(DateOnly), typeof(DateTime), typeof(DateTimeOffset?), typeof(DateOnly?), typeof(DateTime?), typeof(DateTimeOffset?) };

readonly static DateTime TODAY = DateTime.Now;

/// <summary>
/// 前缀图标,默认是 <see cref="IconName.Calendar"/> 。
/// </summary>
Expand Down Expand Up @@ -52,18 +55,17 @@ public partial class TDatePicker<TValue> : TDesignInputComonentBase<TValue>
/// </summary>
[ParameterApiDoc("高亮显示的日期,默认时当天或当月")]
[Parameter]
public List<DateTime> SelectedDates { get; set; } = new()
{
DateTime.Today
};
public List<DateTime> SelectedDates { get; set; } = new() { TODAY };
/// <summary>
/// 当日期改变时触发的回调。
/// </summary>
[ParameterApiDoc("当日期被选中时触发的回调", Type = "EventCallback<IReadOnlyList<DateTime>>")]
[Parameter] public EventCallback<IReadOnlyList<DateTime>> OnSelected { get; set; }
#region Internal

DateTime CurrentValue { get; set; } = DateTime.Today;


DateTime CurrentValueAsDateTime { get; set; } = TODAY;

/// <summary>
/// 文本框的输入值
Expand Down Expand Up @@ -103,25 +105,24 @@ public partial class TDatePicker<TValue> : TDesignInputComonentBase<TValue>
/// </summary>
int CurrentYear
{
get => CurrentValue.Year;
set => CurrentValue = new(value, CurrentMonth, CurrentDay);
get => CurrentValueAsDateTime.Year;
set => CurrentValueAsDateTime = new(value, CurrentMonth, CurrentDay);
}
/// <summary>
/// 当前查看的月份。
/// </summary>
int CurrentMonth
{
get => CurrentValue.Month;
set => CurrentValue = new(CurrentYear, value, CurrentDay);
get => CurrentValueAsDateTime.Month;
set => CurrentValueAsDateTime = new(CurrentYear, value, CurrentDay);
}

/// <summary>
/// 获取当前选中的天。
/// </summary>
int CurrentDay
{
get => CurrentValue.Day;
set => CurrentValue = new(CurrentYear, CurrentMonth, value);
get => CurrentValueAsDateTime.Day;
}


Expand All @@ -139,13 +140,13 @@ protected override void OnInitialized()
switch (Value)
{
case DateOnly date:
CurrentValue = date.ToDateTime(new());
CurrentValueAsDateTime = date.ToDateTime(new());
break;
case DateTime dateTime:
CurrentValue = dateTime;
CurrentValueAsDateTime = dateTime;
break;
case DateTimeOffset offset:
CurrentValue = offset.DateTime;
CurrentValueAsDateTime = offset.DateTime;
break;
}
}
Expand All @@ -154,10 +155,6 @@ protected override void OnParametersSet()
{
base.OnParametersSet();

CurrentMonth = CurrentValue.Month;
CurrentYear = CurrentValue.Year;


#region 调整第一天是周几的顺序
var theFirstDayIndex = DayOfWeekList.FindIndex(m => m == FirstDayOfWeek);
var orderRange = DayOfWeekList.GetRange(theFirstDayIndex, DayOfWeekList.Count - theFirstDayIndex);
Expand Down Expand Up @@ -195,7 +192,7 @@ protected override void AddContent(RenderTreeBuilder builder, int sequence)
{
input.Component<TInputText<string>>()
.Attribute(m => m.Value, InputValue)
.Attribute(m => m.ValueExpression,()=>Value.ToString())
.Attribute(m => m.ValueExpression,()=>InputValue)
.Attribute(m => m.ValueChanged, InputValueChanged)
.Attribute(m => m.AutoWidth, AutoWidth)
.Attribute(m => m.Alignment, Alignment)
Expand Down Expand Up @@ -229,41 +226,22 @@ async Task SelectItems(IReadOnlyList<DateTime> selectedItems)
}

var value = selectedItems[0];
CurrentValue = value;

//Value = CurrentValue.To(value =>
//{
// value = value switch
// {
// DateOnly => DateOnly.FromDateTime(CurrentValue),
// DateTimeOffset => new DateTimeOffset(CurrentValue),
// _ => CurrentValue,
// };

// var nullableValueType = Nullable.GetUnderlyingType(typeof(TValue));
// if (nullableValueType is not null)
// {
// return (TValue?)Convert.ChangeType(value, nullableValueType);
// }

// return (TValue?)Convert.ChangeType(value, typeof(TValue));
//});

CurrentValueAsDateTime = value;

SelectedDates.Clear();
SelectedDates.Add(CurrentValue);
SelectedDates.Add(value);

InputValue = CurrentValue.ToString(Format);
InputValue = CurrentValueAsDateTime.ToString(Format);
await InputValueChanged.InvokeAsync(InputValue);


Value = InputValue.To(value =>
{
value = value switch
{
DateOnly => DateOnly.FromDateTime(CurrentValue),
DateTimeOffset => new DateTimeOffset(CurrentValue),
_ => CurrentValue,
DateOnly => DateOnly.FromDateTime(CurrentValueAsDateTime),
DateTimeOffset => new DateTimeOffset(CurrentValueAsDateTime),
_ => CurrentValueAsDateTime,
};
var nullableValueType = Nullable.GetUnderlyingType(typeof(TValue));
Expand All @@ -272,11 +250,11 @@ async Task SelectItems(IReadOnlyList<DateTime> selectedItems)
if (nullableValueType == typeof(DateOnly))
{
return (TValue?)typeof(DateOnly)?.GetMethod(nameof(DateOnly.FromDateTime))?.Invoke(value, new object?[] { CurrentValue });
return (TValue?)typeof(DateOnly)?.GetMethod(nameof(DateOnly.FromDateTime))?.Invoke(value, new object?[] { CurrentValueAsDateTime });
}
else if (nullableValueType == typeof(DateTimeOffset))
{
return (TValue?)typeof(DateTimeOffset)?.GetMethod(nameof(DateTimeOffset.FromFileTime))?.Invoke(value, new object?[] { CurrentValue.ToFileTime() });
return (TValue?)typeof(DateTimeOffset)?.GetMethod(nameof(DateTimeOffset.FromFileTime))?.Invoke(value, new object?[] { CurrentValueAsDateTime.ToFileTime() });
}
}
return (TValue)value;
Expand All @@ -292,7 +270,7 @@ Task ChangeYearMonth(int year,int month)
}

bool IsToday(int day) => DateOnly.FromDateTime(DateTime.Today) == new DateOnly(CurrentYear, CurrentMonth, day);
bool IsToday(DateOnly date) => DateOnly.FromDateTime(DateTime.Today) == date;
bool IsToday(DateTime date) => DateTime.Today == date;
}

/// <summary>
Expand Down

0 comments on commit 86c4725

Please sign in to comment.