mirror of
https://gitee.com/ant-design-blazor/ant-design-blazor.git
synced 2024-12-05 05:27:37 +08:00
f351251997
* fix 2911 (css classes gets appended twice) * fix 2911 Co-authored-by: James Yeung <shunjiey@hotmail.com>
57 lines
1.4 KiB
C#
57 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace AntDesign
|
|
{
|
|
public class ClassMapper
|
|
{
|
|
private readonly Dictionary<Func<string>, Func<bool>> _map = new();
|
|
|
|
public ClassMapper() : this(string.Empty)
|
|
{
|
|
}
|
|
|
|
public ClassMapper(string originalClass)
|
|
{
|
|
OriginalClass = originalClass;
|
|
|
|
_map.Add(() => OriginalClass, () => !string.IsNullOrEmpty(OriginalClass));
|
|
}
|
|
|
|
public string Class => ToString();
|
|
|
|
public string OriginalClass { get; internal set; }
|
|
|
|
[Obsolete("Use ToString() instead")]
|
|
public string AsString() => ToString();
|
|
|
|
public override string ToString() => string.Join(' ', _map.Where(i => i.Value()).Select(i => i.Key()));
|
|
|
|
public ClassMapper Add(string name) => Get(() => name);
|
|
|
|
public ClassMapper Get(Func<string> funcName)
|
|
{
|
|
_map.Add(funcName, () => !string.IsNullOrEmpty(funcName()));
|
|
return this;
|
|
}
|
|
|
|
public ClassMapper GetIf(Func<string> funcName, Func<bool> func)
|
|
{
|
|
_map.Add(funcName, func);
|
|
return this;
|
|
}
|
|
|
|
public ClassMapper If(string name, Func<bool> func) => GetIf(() => name, func);
|
|
|
|
public ClassMapper Clear()
|
|
{
|
|
_map.Clear();
|
|
|
|
_map.Add(() => OriginalClass, () => !string.IsNullOrEmpty(OriginalClass));
|
|
|
|
return this;
|
|
}
|
|
}
|
|
}
|