Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 fix(DatePicker): cannot select only the year #2070

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/Masa.Blazor.JS/src/interop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,8 @@ export function scrollToElement(target, offset: number, behavior?: ScrollBehavio
export function scrollToActiveElement(
container,
element = ".active",
position: "center" | number = "center"
position: "center" | number = "center",
smooth: boolean = false
) {
let containerEl: HTMLElement = getDom(container);

Expand All @@ -486,12 +487,9 @@ export function scrollToActiveElement(
return;
}

if (position === 'center') {
containerEl.scrollTop = activeEl.offsetTop - containerEl.offsetHeight / 2 + activeEl.offsetHeight / 2;
}
else {
containerEl.scrollTop = activeEl.offsetTop - position
}
const top: number = position === 'center' ? activeEl.offsetTop - containerEl.offsetHeight / 2 + activeEl.offsetHeight / 2 : activeEl.offsetTop - position;

containerEl.scrollTo({ top, behavior: smooth ? 'smooth' : 'auto' });
}

export function addClsToFirstChild(element, className) {
Expand Down
11 changes: 3 additions & 8 deletions src/Masa.Blazor/Components/DatePicker/MDatePicker.razor
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
? null
: __builder =>
{
<MDatePickerTitle Date="@(Formatters.TitleDate(MultipleValue))"
<MDatePickerTitle Date="@(Type == DatePickerType.Year ? Formatters.Year(TableDate) : Formatters.TitleDate(MultipleValue))"
Disabled="@Disabled"
Readonly="@Readonly"
SelectingYear="@(InternalActivePicker == DatePickerType.Year)"
Expand All @@ -70,15 +70,10 @@
Max="@MaxYear"
Value="@TableYear"
Locale="@CurrentLocale"
OnInput="@OnInput"
Type="@Type"
OnInput="@OnYearClickAsync"
OnYearClick="@OnYearClick">
</MDatePickerYears>

void OnInput(int year)
{
TableDate = new DateOnly(year, TableDate.Month, TableDate.Day);
InternalActivePicker = DatePickerType.Month;
}
};

private RenderFragment GenTableHeader() => __builder =>
Expand Down
26 changes: 26 additions & 0 deletions src/Masa.Blazor/Components/DatePicker/MDatePicker.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,32 @@ protected override IEnumerable<string> BuildComponentClass()
yield return "m-picker--date";
}

private async Task OnYearClickAsync(int year)
{
TableDate = new DateOnly(year, TableDate.Month, TableDate.Day);
if (Type == DatePickerType.Year)
{
// multiple mode is not supported in year mode

Value = TableDate is TValue val ? val : default;

if (ValueChanged.HasDelegate)
{
await ValueChanged.InvokeAsync(Value);
}

//REVIEW:
if (OnInput.HasDelegate)
{
await OnInput.InvokeAsync();
}
}
else
{
InternalActivePicker = DatePickerType.Month;
}
}

private async Task OnMonthClickAsync(DateOnly value)
{
if (Type == DatePickerType.Date)
Expand Down
15 changes: 11 additions & 4 deletions src/Masa.Blazor/Components/DatePicker/MDatePickerYears.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public partial class MDatePickerYears : MasaComponentBase

[Parameter] public CultureInfo Locale { get; set; } = null!;

[Parameter] public DatePickerType Type { get; set; }

private Func<DateOnly, string> Formatter
{
get
Expand All @@ -39,6 +41,11 @@ private async Task HandleOnYearItemClickAsync(int year)
{
await OnInput.InvokeAsync(year);
await OnYearClick.InvokeAsync(year);

if (Type == DatePickerType.Year)
{
_ = ScrollToActiveElementAsync(true);
}
}

protected override IEnumerable<string> BuildComponentClass()
Expand All @@ -52,14 +59,14 @@ protected override async Task OnAfterRenderAsync(bool firstRender)

if (firstRender)
{
var handle = DotNetObjectReference.Create(new IntersectInvoker(OnIntersectAsync));
var handle = DotNetObjectReference.Create(new IntersectInvoker(_ => ScrollToActiveElementAsync()));
await IntersectJSModule.ObserverAsync(Ref, handle);
}
}

private async Task OnIntersectAsync(IntersectEventArgs arg)
private async Task ScrollToActiveElementAsync(bool smooth = false)
{
await Js.InvokeVoidAsync(JsInteropConstants.ScrollToActiveElement, Ref);
await Js.InvokeVoidAsync(JsInteropConstants.ScrollToActiveElement, Ref, ".active", "center", smooth);
}

protected override async ValueTask DisposeAsyncCore()
Expand Down
2 changes: 1 addition & 1 deletion src/Masa.Blazor/wwwroot/js/masa-blazor.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Masa.Blazor/wwwroot/js/masa-blazor.js.map

Large diffs are not rendered by default.

Loading