ant-design-blazor/components/timeline/Timeline.razor.cs

164 lines
4.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2019-12-20 00:20:53 +08:00
using System.Linq;
using Microsoft.AspNetCore.Components;
using OneOf;
2019-12-20 00:20:53 +08:00
namespace AntDesign
2019-12-20 00:20:53 +08:00
{
public partial class Timeline : AntDomComponentBase
2019-12-20 00:20:53 +08:00
{
/// <summary>
/// 'left' | 'alternate' | 'right'
/// </summary>
[Parameter]
public string Mode
{
get => _mode;
set
{
if (_mode != value)
{
_mode = value;
_waitingItemUpdate = true;
}
}
}
2019-12-20 00:20:53 +08:00
[Parameter]
public bool Reverse
{
get => _reverse;
set
{
if (_reverse != value)
{
_reverse = value;
_waitingItemUpdate = true;
}
}
}
2019-12-20 00:20:53 +08:00
[Parameter]
public OneOf<string, RenderFragment> Pending
2019-12-20 00:20:53 +08:00
{
get => _pending;
set
2019-12-20 00:20:53 +08:00
{
if (_pending.Value != value.Value)
{
_pending = value;
_pendingItem = _pending.Value == null ? null : new TimelineItem()
{
Dot = PendingDot ?? _loadingDot,
Class = "ant-timeline-item-pending"
};
_pending.Switch(str =>
{
_pendingItem.ChildContent = b =>
{
b.AddContent(0, str);
};
}, rf =>
{
_pendingItem.ChildContent = rf;
});
_pendingItem?.SetClassMap();
_waitingItemUpdate = true;
}
2019-12-20 00:20:53 +08:00
}
}
private static readonly RenderFragment _loadingDot = builder =>
{
builder.OpenComponent<Icon>(0);
builder.AddAttribute(1, nameof(Icon.Type), "loading");
builder.CloseComponent();
};
private TimelineItem _pendingItem;
2019-12-20 00:20:53 +08:00
[Parameter]
public RenderFragment PendingDot { get; set; }
[Parameter]
public RenderFragment ChildContent { get; set; }
internal IList<TimelineItem> _items = new List<TimelineItem>();
2019-12-20 00:20:53 +08:00
protected IList<TimelineItem> _displayItems = new List<TimelineItem>();
2019-12-20 00:20:53 +08:00
2020-04-24 18:32:50 +08:00
private readonly bool _isPendingBoolean = false;
private OneOf<string, RenderFragment> _pending;
private bool _reverse;
private bool _waitingItemUpdate = false;
private string _mode;
2019-12-20 00:20:53 +08:00
protected override void OnInitialized()
{
base.OnInitialized();
SetClassMap();
2019-12-20 00:20:53 +08:00
}
private void SetItems()
2019-12-20 00:20:53 +08:00
{
var pitems = _pendingItem != null ? new[] { _pendingItem } : Array.Empty<TimelineItem>();
_displayItems = Reverse ? pitems.Concat(UpdateChildren(_items.Reverse())).ToList() : UpdateChildren(_items).Concat(pitems).ToList();
}
protected override void OnAfterRender(bool firstRender)
{
base.OnAfterRender(firstRender);
if (firstRender || _waitingItemUpdate)
{
_waitingItemUpdate = false;
SetItems();
StateHasChanged();
}
2019-12-20 00:20:53 +08:00
}
protected void SetClassMap()
2019-12-20 00:20:53 +08:00
{
var prefix = "ant-timeline";
ClassMapper.Clear()
.Add(prefix)
.If($"{prefix}-right", () => Mode == "right")
.If($"{prefix}-alternate", () => Mode == "alternate")
.If($"{prefix}-pending", () => Pending.Value != null)
.If($"{prefix}-reverse", () => Reverse);
2019-12-20 00:20:53 +08:00
}
internal void AddItem(TimelineItem item)
2019-12-20 00:20:53 +08:00
{
this._items.Add(item);
2019-12-20 00:20:53 +08:00
StateHasChanged();
}
protected IEnumerable<TimelineItem> UpdateChildren(IEnumerable<TimelineItem> items)
2019-12-20 00:20:53 +08:00
{
if (!items.Any())
yield break;
var length = items.Count();
for (int i = 0; i < length; i++)
{
var item = items.ElementAt(i);
item.IsLast = i == length - 1;
item.Position =
this.Mode == "left" || Mode == null ? null
: this.Mode == "right" ? "right"
: this.Mode == "alternate" && i % 2 == 0 ? "left"
2019-12-20 00:20:53 +08:00
: "right";
item.SetClassMap();
2019-12-20 00:20:53 +08:00
yield return item;
}
}
}
}