ant-design-blazor/components/timeline/TimelineItem.cs

72 lines
2.1 KiB
C#
Raw Normal View History

2019-12-20 00:20:53 +08:00
using Microsoft.AspNetCore.Components;
using System.Linq;
namespace AntDesign
2019-12-20 00:20:53 +08:00
{
public partial class TimelineItem : AntDomComponentBase
2019-12-20 00:20:53 +08:00
{
[Parameter]
public RenderFragment ChildContent { get; set; }
[Parameter]
public RenderFragment Dot { get; set; }
2019-12-20 00:20:53 +08:00
[Parameter]
public string Color { get; set; } = "blue";
2019-12-20 00:20:53 +08:00
[CascadingParameter] public Timeline ParentTimeline { get; set; }
2019-12-20 00:20:53 +08:00
internal ClassMapper _headClassMapper = new ClassMapper();
2019-12-20 00:20:53 +08:00
internal bool IsLast { get; set; } = false;
2019-12-20 00:20:53 +08:00
//'left' | 'alternate' | 'right'
internal string Position { get; set; } = "";
2019-12-20 00:20:53 +08:00
internal string HeadStyle { get; set; } = "";
2019-12-20 00:20:53 +08:00
internal string ItemClass => ClassMapper.Class;
2019-12-20 00:20:53 +08:00
private readonly string[] _defaultColors = new[] { "blue", "red", "green", "gray" };
2019-12-20 00:20:53 +08:00
protected override void Dispose(bool disposing)
{
ParentTimeline?._items.Remove(this);
base.Dispose(disposing);
}
2019-12-20 00:20:53 +08:00
protected override void OnInitialized()
{
ParentTimeline?.AddItem(this);
this.TryUpdateCustomColor();
2019-12-20 00:20:53 +08:00
base.OnInitialized();
}
protected override void OnParametersSet()
{
this.SetClassMap();
this.TryUpdateCustomColor();
2019-12-20 00:20:53 +08:00
base.OnParametersSet();
}
private void TryUpdateCustomColor()
2019-12-20 00:20:53 +08:00
{
HeadStyle = !_defaultColors.Contains(Color) ? $"border-color:{Color}" : "";
2019-12-20 00:20:53 +08:00
}
internal void SetClassMap()
2019-12-20 00:20:53 +08:00
{
var prefix = "ant-timeline-item";
ClassMapper.Clear().Add(prefix)
.If($"{prefix}-right", () => Position == "right")
.If($"{prefix}-left", () => Position == "left")
.If($"{prefix}-last", () => IsLast);
2019-12-20 00:20:53 +08:00
var headPrefix = "ant-timeline-item-head";
_headClassMapper.Clear().Add(headPrefix)
.If($"{headPrefix}-{Color}", () => _defaultColors.Contains(Color))
2019-12-20 00:20:53 +08:00
.If($"{headPrefix}-custom", () => Dot != null);
}
}
}