File tree Expand file tree Collapse file tree 3 files changed +76
-0
lines changed
Expand file tree Collapse file tree 3 files changed +76
-0
lines changed Original file line number Diff line number Diff line change @@ -54,4 +54,41 @@ public void CanRenderImportMap()
5454 _output . WriteLine ( html ) ;
5555 Assert . Equal ( @"<script type=""importmap"">{""import"":{""name"":""./path/to/module""}}</script>" , html ) ;
5656 }
57+
58+
59+
60+
61+ [ Fact ]
62+ public void CanRenderConditionalFalse ( )
63+ {
64+ var hasClass = false ;
65+ var div = Div ( If ( hasClass , Class ( "class" ) ) ) ;
66+
67+ var html = div . ToString ( ) ;
68+ _output . WriteLine ( html ) ;
69+ Assert . Equal ( "<div></div>" , html ) ;
70+
71+ }
72+
73+ [ Fact ]
74+ public void CanRenderConditionalTrue ( )
75+ {
76+ var hasClass = true ;
77+ var div = Div ( If ( hasClass , Class ( "class" ) ) ) ;
78+
79+ var html = div . ToString ( ) ;
80+ _output . WriteLine ( html ) ;
81+ Assert . Equal ( "<div class=\" class\" ></div>" , html ) ;
82+ }
83+
84+ [ Fact ]
85+ public void CanRenderConditionalTrueFunctionContent ( )
86+ {
87+ var hasClass = true ;
88+ var div = Div ( If ( hasClass , ( ) => Class ( "class" ) ) ) ;
89+
90+ var html = div . ToString ( ) ;
91+ _output . WriteLine ( html ) ;
92+ Assert . Equal ( "<div class=\" class\" ></div>" , html ) ;
93+ }
5794}
Original file line number Diff line number Diff line change 1+ using System . Collections ;
2+
3+ namespace Cubist . Helium ;
4+
5+ /// <summary> Adds content or not, based on a condition </summary>
6+ public class Conditional : IEnumerable
7+ {
8+ private readonly Func < bool > _condition ;
9+ private readonly Func < object > _content ;
10+
11+ /// <summary> Creates a new <see cref="Conditional"/> instance </summary>
12+ public Conditional ( Func < bool > condition , Func < object > content )
13+ {
14+ _condition = condition ;
15+ _content = content ;
16+ }
17+ /// <inheritdoc cref="IEnumerable.GetEnumerator"/>>
18+ public IEnumerator GetEnumerator ( )
19+ {
20+ if ( _condition ( ) )
21+ yield return _content ( ) ;
22+ }
23+ }
Original file line number Diff line number Diff line change @@ -227,6 +227,22 @@ public static (string, object?) Class(params string[] classes)
227227 /// <inheritdoc cref="Tags.I"/>
228228 public static He I ( params object [ ] content ) => new ( Tags . I ) { content } ;
229229
230+ /// <inheritdoc cref="Conditional"/>
231+ public static object If ( bool condition , params object [ ] content )
232+ => If ( ( ) => condition , content ) ;
233+
234+ /// <inheritdoc cref="Conditional"/>
235+ public static object If ( Func < bool > condition , params object [ ] content )
236+ => new Conditional ( condition , ( ) => content ) ;
237+
238+ /// <inheritdoc cref="Conditional"/>
239+ public static object If < T > ( bool condition , Func < T > content ) where T : notnull
240+ => If ( ( ) => condition , content ) ;
241+
242+ /// <inheritdoc cref="Conditional"/>
243+ public static object If < T > ( Func < bool > condition , Func < T > content ) where T : notnull
244+ => new Conditional ( condition , ( ) => content ( ) ) ;
245+
230246 /// <inheritdoc cref="Tags.Iframe"/>
231247 public static He Iframe ( string title , string src ) => new ( Tags . Iframe )
232248 {
You can’t perform that action at this time.
0 commit comments