ant-design-blazor/components/calendar/Calendar.razor.cs
Nino Padrutt 5809c80011 feat(module: date-picker): support localization (#803)
* start trying to add a different first day of the week based on localization.

* add first day of week parameter and get weekday local  from .net

* fix some issues

* extend demo

* remove no longed needed Locale parameter

* fix range for selection

* add german localization

* add entry for API section

* remove obsolet things from the react ant documentation

* add description to chart documentation

* translate general configuration to english.

* code cleanup

* use LocalProvider to get first day of week.

* use localeprovider instead of date picker localization

* remove no longer used parameter

* delete no longer used interface

* change from enum to string due to parsing issue.

* add missing formats and use culture info from localprovider

* correct locales

* us Sunday as a default

* remove no longer used culture info parameter

* adjust locales

* add parameter for Locale instead of directly accessing it.

* add inheritance to access locale

* typo

* readd culture info parameter

* fixes for locale jsons

* adjust demo and api

* small adjustments on docs, set monday in chinese as first day of the week.

* use locale in calendar as well.

* adjust docs

* use enum

* adjust demo

* adjust for different starting date.

* add defaults for DatePickerLocale

* add short week days to locale files

* use days from locale file

* code cleanup

use consts instead of magic numbers, add code comment and use dayOfWeek As param instead of the whole locale object. Remove no longed used method.

* not sure about the chinese translation so I remove them

* Revert "not sure about the chinese translation so I remove them"

This reverts commit 54017513c7b684494cf06184b4051a4dcfc43850.
2020-11-25 11:08:12 +08:00

185 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
namespace AntDesign
{
public partial class Calendar : AntDomComponentBase, IDatePicker
{
DateTime IDatePicker.CurrentDate { get; set; } = DateTime.Now;
DateTime? IDatePicker.HoverDateTime { get; set; }
[Parameter]
public DateTime Value { get; set; } = DateTime.Now;
private DateTime _defaultValue;
[Parameter]
public DateTime DefaultValue
{
get
{
return _defaultValue;
}
set
{
_defaultValue = value;
Value = _defaultValue;
}
}
[Parameter]
public DateTime[] ValidRange { get; set; }
[Parameter]
public string Mode { get; set; } = DatePickerType.Month;
[Parameter]
public bool FullScreen { get; set; } = true;
[Parameter]
public EventCallback<DateTime> OnSelect { get; set; }
[Parameter]
public EventCallback<DateTime> OnChange { get; set; }
[Parameter]
public Func<CalendarHeaderRenderArgs, RenderFragment> HeaderRender { get; set; }
[Parameter]
public Func<DateTime, RenderFragment> DateCellRender { get; set; }
[Parameter]
public Func<DateTime, RenderFragment> DateFullCellRender { get; set; }
[Parameter]
public Func<DateTime, RenderFragment> MonthCellRender { get; set; }
[Parameter]
public Func<DateTime, RenderFragment> MonthFullCellRender { get; set; }
[Parameter]
public Action<DateTime, string> OnPanelChange { get; set; }
[Parameter]
public Func<DateTime, bool> DisabledDate { get; set; } = null;
[Parameter]
public DatePickerLocale Locale { get; set; } = LocaleProvider.CurrentLocale.DatePicker;
[Parameter]
public CultureInfo CultureInfo { get; set; } = LocaleProvider.CurrentLocale.CurrentCulture;
protected string _picker;
protected readonly DateTime[] PickerValues = new DateTime[] { DateTime.Now, DateTime.Now };
protected Stack<string> _prePickerStack = new Stack<string>();
public readonly string PrefixCls = "ant-picker-calendar";
protected override void OnInitialized()
{
base.OnInitialized();
_picker = Mode switch
{
DatePickerType.Month => DatePickerType.Date,
DatePickerType.Year => DatePickerType.Month,
_ => DatePickerType.Date,
};
if (ValidRange != null)
{
if (Value < ValidRange[0])
{
Value = ValidRange[0];
}
else if (Value > ValidRange[1])
{
Value = ValidRange[1];
}
}
}
protected override void OnParametersSet()
{
this.SetClass();
base.OnParametersSet();
}
protected void SetClass()
{
this.ClassMapper.Clear()
.Add(PrefixCls)
.If($"{PrefixCls}-full", () => FullScreen)
;
}
protected async Task OnSelectValue(DateTime date)
{
Value = date;
await OnSelect.InvokeAsync(date);
await OnChange.InvokeAsync(date);
StateHasChanged();
}
public async Task ChangeValue(DateTime date)
{
await OnSelectValue(date);
StateHasChanged();
}
public void ChangePickerType(string type, int index)
{
Mode = type;
string mode = type switch
{
DatePickerType.Year => DatePickerType.Month,
DatePickerType.Month => DatePickerType.Date,
_ => DatePickerType.Date,
};
_prePickerStack.Push(_picker);
_picker = mode;
OnPanelChange?.Invoke(PickerValues[index], _picker);
StateHasChanged();
}
public void ChangePickerType(string type)
{
ChangePickerType(type, 0);
}
public void Close()
{
}
public int GetOnFocusPickerIndex()
{
return 0;
}
void IDatePicker.InvokeStateHasChanged()
{
StateHasChanged();
}
string IDatePicker.GetFormatValue(DateTime value, int index)
{
return value.ToString(CultureInfo);
}
void IDatePicker.ChangePlaceholder(string placeholder, int index)
{
}
public string Picker { get { return _picker; } }
}
}