ant-design-blazor/components/core/Helpers/ClassMapper.cs

57 lines
1.4 KiB
C#
Raw Normal View History

2019-12-08 00:51:07 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
namespace AntDesign
2019-12-08 00:51:07 +08:00
{
public class ClassMapper
{
private readonly Dictionary<Func<string>, Func<bool>> _map = new();
2019-12-08 00:51:07 +08:00
public ClassMapper() : this(string.Empty)
2019-12-08 00:51:07 +08:00
{
}
public ClassMapper(string originalClass)
2019-12-08 00:51:07 +08:00
{
OriginalClass = originalClass;
_map.Add(() => OriginalClass, () => !string.IsNullOrEmpty(OriginalClass));
2019-12-08 00:51:07 +08:00
}
public string Class => ToString();
2019-12-08 00:51:07 +08:00
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);
2019-12-08 00:51:07 +08:00
public ClassMapper Get(Func<string> funcName)
{
_map.Add(funcName, () => !string.IsNullOrEmpty(funcName()));
2019-12-08 00:51:07 +08:00
return this;
}
public ClassMapper GetIf(Func<string> funcName, Func<bool> func)
{
_map.Add(funcName, func);
2019-12-08 00:51:07 +08:00
return this;
}
public ClassMapper If(string name, Func<bool> func) => GetIf(() => name, func);
2019-12-09 23:42:25 +08:00
public ClassMapper Clear()
{
_map.Clear();
2019-12-20 00:20:53 +08:00
_map.Add(() => OriginalClass, () => !string.IsNullOrEmpty(OriginalClass));
2019-12-20 00:20:53 +08:00
2019-12-09 23:42:25 +08:00
return this;
}
2019-12-08 00:51:07 +08:00
}
}