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

59 lines
1.3 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
{
public string Class => AsString();
2019-12-20 00:20:53 +08:00
internal string OriginalClass { get; set; }
2019-12-08 00:51:07 +08:00
public string AsString()
{
return string.Join(" ", _map.Where(i => i.Value()).Select(i => i.Key()));
2019-12-08 00:51:07 +08:00
}
public override string ToString()
{
return AsString();
}
private readonly Dictionary<Func<string>, Func<bool>> _map = new Dictionary<Func<string>, Func<bool>>();
2019-12-08 00:51:07 +08:00
public ClassMapper Add(string name)
{
_map.Add(() => name, () => true);
2019-12-08 00:51:07 +08:00
return this;
}
public ClassMapper Get(Func<string> funcName)
{
_map.Add(funcName, () => true);
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)
{
_map.Add(() => name, func);
2019-12-08 00:51:07 +08:00
return this;
}
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
}
}