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

41 lines
1.1 KiB
C#
Raw Normal View History

2019-12-08 00:51:07 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
namespace AntBlazor
{
public class ClassBuilder<T>
{
public ClassBuilder<T> If(string className, Func<T, bool> func)
{
2020-04-24 18:32:50 +08:00
_rules.Add(new ClassBuilderRuleIf<T>(className, func));
2019-12-08 00:51:07 +08:00
return this;
}
public ClassBuilder<T> Get(Func<T, string> func)
{
2020-04-24 18:32:50 +08:00
_rules.Add(new ClassBuilderRuleGet<T>(func));
2019-12-08 00:51:07 +08:00
return this;
}
public ClassBuilder<T> Class(string className)
{
2020-04-24 18:32:50 +08:00
_rules.Add(new ClassBuilderRuleClass<T>(className));
2019-12-08 00:51:07 +08:00
return this;
}
public ClassBuilder<T> Dictionary<TK>(Func<T, TK> func, IDictionary<TK, string> dictionary)
{
2020-04-24 18:32:50 +08:00
_rules.Add(new ClassBuilderRuleDictionary<T, TK>(func, dictionary));
2019-12-08 00:51:07 +08:00
return this;
}
2020-04-24 18:32:50 +08:00
private readonly List<ClassBuilderRule<T>> _rules = new List<ClassBuilderRule<T>>();
2019-12-08 00:51:07 +08:00
public string GetClasses(T data)
{
2020-04-24 18:32:50 +08:00
return string.Join(" ", _rules.Select(i => i.GetClass(data)).Where(i => i != null));
2019-12-08 00:51:07 +08:00
}
}
2020-04-24 18:32:50 +08:00
}