using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; using Microsoft.AspNetCore.Components; using OneOf; namespace AntDesign { public partial class Skeleton : AntDomComponentBase { #region Parameters [Parameter] public bool Active { get; set; } [Parameter] public bool Loading { get; set; } = true; /// /// 是否显示标题占位图 /// [Parameter] public bool Title { get; set; } = true; [Parameter] public OneOf TitleWidth { get; set; } /// /// 是否显示头像占位图 /// [Parameter] public bool Avatar { get; set; } [Parameter] public OneOf AvatarSize { get; set; } [Parameter] public string AvatarShape { get; set; } /// /// 是否显示段落占位图 /// [Parameter] public bool Paragraph { get; set; } = true; [Parameter] public int? ParagraphRows { get; set; } [Parameter] public OneOf>> ParagraphWidth { get; set; } #endregion Parameters [Parameter] public RenderFragment ChildContent { get; set; } private IList> _paragraphRowsList; private void SetClassMap() { ClassMapper .Add("ant-skeleton") .If("ant-skeleton-with-avatar", () => this.Avatar) .If("ant-skeleton-active", () => this.Active) .If("ant-skeleton-rtl", () => RTL); } protected override void OnInitialized() { SetClassMap(); UpdateProps(); base.OnInitialized(); } private static string ToCSSUnit(OneOf value) { if (value.IsT0) return StyleHelper.ToCssPixel(value.AsT0.Value); else return StyleHelper.ToCssPixel(value.AsT1); } private void UpdateProps() { SetTitleProps(); SetAvatarProps(); SetParagraphProps(); } private void SetTitleProps() { if (Title == false) return; Console.WriteLine(TitleWidth.Value); if (TitleWidth.Value == null) { if (!Avatar && Paragraph) { TitleWidth = "38%"; } else if (Avatar && Paragraph) { TitleWidth = "50%"; } else { TitleWidth = ""; } } } public void SetAvatarProps() { if (Avatar == false) return; if (AvatarShape == null) { AvatarShape = SkeletonAvatarShape.Circle; } if (AvatarSize.Value == null) { AvatarSize = SkeletonElementSize.Default; } } private void SetParagraphProps() { if (Paragraph == false) return; int rows; if (ParagraphRows.HasValue) { rows = ParagraphRows.Value - 1; } else if (!Avatar && Title) { rows = 2; } else { rows = 1; } if (ParagraphWidth.Value == null) { _paragraphRowsList = CreateRowsList(rows, "61%"); } else { _paragraphRowsList = ParagraphWidth.Match( f0 => CreateRowsList(rows, f0), f1 => CreateRowsList(rows, f1), f2 => f2); } } private static List> CreateRowsList(int rows, OneOf lastWidth) { var rowlist = new List>(); for (int i = 0; i < rows; i++) { rowlist.Add(""); } rowlist.Add(lastWidth); return rowlist; } } }