Skip to content

Commit 72348e7

Browse files
committed
Optional VDOM now requires .whenDefined instead being automatic
1 parent 161f116 commit 72348e7

File tree

3 files changed

+113
-96
lines changed

3 files changed

+113
-96
lines changed

core/src/main/scala/japgolly/scalajs/react/vdom/Implicits.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,16 @@ trait ImplicitsForVdomAttr extends ImplicitsForVdomAttr1 {
4747

4848
// =====================================================================================================================
4949

50+
object ImplicitsForTagMod {
51+
final class OptionExt[O[_], A](o: O[A])(implicit O: OptionLike[O]) {
52+
def whenDefined(implicit f: A => TagMod): TagMod =
53+
O.fold(o, TagMod.Empty)(f)
54+
}
55+
}
56+
5057
trait ImplicitsForTagMod {
51-
implicit def tagModFromOptionLike[O[_], A](o: O[A])(implicit O: OptionLike[O], f: A => TagMod): TagMod =
52-
O.fold(o, TagMod.Empty)(f)
58+
implicit def vdomOptionExt[O[_], A](o: O[A])(implicit O: OptionLike[O]): ImplicitsForTagMod.OptionExt[O, A] =
59+
new ImplicitsForTagMod.OptionExt(o)
5360
}
5461

5562
// =====================================================================================================================

test/src/test/scala/japgolly/scalajs/react/core/vdom/PrefixedTest.scala

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -97,56 +97,61 @@ object PrefixedTest extends TestSuite {
9797

9898
'optional {
9999
'option {
100-
'attr_some - test(<.div(^.cls :=? "hi".some), """<div class="hi"></div>""")
101-
'attr_none - test(<.div(^.cls :=? "h1".none), """<div></div>""")
102-
'style_some - test(<.div(^.color :=? "red".some), """<div style="color:red;"></div>""")
103-
'style_none - test(<.div(^.color :=? "red".none), """<div></div>""")
104-
'attr_some - test(<.div((^.color := "red").some), """<div style="color:red;"></div>""")
105-
'attr_none - test(<.div((^.color := "red").none), """<div></div>""")
106-
'tagMod_some - test(<.div(tagMod.some), """<div class="ho"></div>""")
107-
'tagMod_none - test(<.div(tagMod.none), """<div></div>""")
108-
'tag_some - test(<.div(vdomTag.some), """<div><span></span></div>""")
109-
'tag_none - test(<.div(vdomTag.none), """<div></div>""")
110-
'element_some - test(<.div(vdomElement.some), """<div><p></p></div>""")
111-
'element_none - test(<.div(vdomElement.none), """<div></div>""")
112-
'comp_some - test(<.div(H1("yoo").some), """<div><h1>yoo</h1></div>""")
113-
'comp_none - test(<.div(H1("yoo").none), """<div></div>""")
114-
'text_some - test(<.div("yoo".some), """<div>yoo</div>""")
115-
'text_none - test(<.div("yoo".none), """<div></div>""")
100+
def some[A](a: A): Option[A] = Some(a)
101+
def none[A](a: A): Option[A] = None
102+
'attr_some - test(<.div(^.cls :=? some("hi") ), """<div class="hi"></div>""")
103+
'attr_none - test(<.div(^.cls :=? none("h1") ), """<div></div>""")
104+
'style_some - test(<.div(^.color :=? some("red") ), """<div style="color:red;"></div>""")
105+
'style_none - test(<.div(^.color :=? none("red") ), """<div></div>""")
106+
'tagMod_some - test(<.div(some(tagMod ).whenDefined), """<div class="ho"></div>""")
107+
'tagMod_none - test(<.div(none(tagMod ).whenDefined), """<div></div>""")
108+
'tag_some - test(<.div(some(vdomTag ).whenDefined), """<div><span></span></div>""")
109+
'tag_none - test(<.div(none(vdomTag ).whenDefined), """<div></div>""")
110+
'element_some - test(<.div(some(vdomElement).whenDefined), """<div><p></p></div>""")
111+
'element_none - test(<.div(none(vdomElement).whenDefined), """<div></div>""")
112+
'comp_some - test(<.div(some(H1("yoo") ).whenDefined), """<div><h1>yoo</h1></div>""")
113+
'comp_none - test(<.div(none(H1("yoo") ).whenDefined), """<div></div>""")
114+
'text_some - test(<.div(some("yoo" ).whenDefined), """<div>yoo</div>""")
115+
'text_none - test(<.div(none("yoo" ).whenDefined), """<div></div>""")
116116
}
117117
'jsUndefOr {
118-
'attr_def - test(<.div(^.cls :=? "hi".jsdef), """<div class="hi"></div>""")
119-
'attr_undef - test(<.div(^.cls :=? "hi".undef), """<div></div>""")
120-
'style_def - test(<.div(^.color :=? "red".jsdef), """<div style="color:red;"></div>""")
121-
'style_undef - test(<.div(^.color :=? "red".undef), """<div></div>""")
122-
'attr_def - test(<.div((^.color := "red").jsdef), """<div style="color:red;"></div>""")
123-
'attr_undef - test(<.div((^.color := "red").undef), """<div></div>""")
124-
'tagMod_def - test(<.div(tagMod.jsdef), """<div class="ho"></div>""")
125-
'tagMod_undef - test(<.div(tagMod.undef), """<div></div>""")
126-
'tag_def - test(<.div(vdomTag.jsdef), """<div><span></span></div>""")
127-
'tag_undef - test(<.div(vdomTag.undef), """<div></div>""")
128-
'element_def - test(<.div(vdomElement.jsdef), """<div><p></p></div>""")
129-
'element_undef - test(<.div(vdomElement.undef), """<div></div>""")
130-
'comp_def - test(<.div(H1("yoo").jsdef), """<div><h1>yoo</h1></div>""")
131-
'comp_undef - test(<.div(H1("yoo").undef), """<div></div>""")
132-
'text_def - test(<.div("yoo".jsdef), """<div>yoo</div>""")
133-
'text_undef - test(<.div("yoo".undef), """<div></div>""")
118+
def some[A](a: A): js.UndefOr[A] = a
119+
def none[A](a: A): js.UndefOr[A] = js.undefined
120+
'attr_some - test(<.div(^.cls :=? some("hi") ), """<div class="hi"></div>""")
121+
'attr_none - test(<.div(^.cls :=? none("h1") ), """<div></div>""")
122+
'style_some - test(<.div(^.color :=? some("red") ), """<div style="color:red;"></div>""")
123+
'style_none - test(<.div(^.color :=? none("red") ), """<div></div>""")
124+
'tagMod_some - test(<.div(some(tagMod ).whenDefined), """<div class="ho"></div>""")
125+
'tagMod_none - test(<.div(none(tagMod ).whenDefined), """<div></div>""")
126+
'tag_some - test(<.div(some(vdomTag ).whenDefined), """<div><span></span></div>""")
127+
'tag_none - test(<.div(none(vdomTag ).whenDefined), """<div></div>""")
128+
'element_some - test(<.div(some(vdomElement).whenDefined), """<div><p></p></div>""")
129+
'element_none - test(<.div(none(vdomElement).whenDefined), """<div></div>""")
130+
'comp_some - test(<.div(some(H1("yoo") ).whenDefined), """<div><h1>yoo</h1></div>""")
131+
'comp_none - test(<.div(none(H1("yoo") ).whenDefined), """<div></div>""")
132+
'text_some - test(<.div(some("yoo" ).whenDefined), """<div>yoo</div>""")
133+
'text_none - test(<.div(none("yoo" ).whenDefined), """<div></div>""")
134+
}
135+
'maybe {
136+
import ScalazReact._
137+
import scalaz.Maybe
138+
def some[A](a: A): Maybe[A] = Maybe.Just(a)
139+
def none[A](a: A): Maybe[A] = Maybe.empty
140+
'attr_some - test(<.div(^.cls :=? some("hi") ), """<div class="hi"></div>""")
141+
'attr_none - test(<.div(^.cls :=? none("h1") ), """<div></div>""")
142+
'style_some - test(<.div(^.color :=? some("red") ), """<div style="color:red;"></div>""")
143+
'style_none - test(<.div(^.color :=? none("red") ), """<div></div>""")
144+
'tagMod_some - test(<.div(some(tagMod ).whenDefined), """<div class="ho"></div>""")
145+
'tagMod_none - test(<.div(none(tagMod ).whenDefined), """<div></div>""")
146+
'tag_some - test(<.div(some(vdomTag ).whenDefined), """<div><span></span></div>""")
147+
'tag_none - test(<.div(none(vdomTag ).whenDefined), """<div></div>""")
148+
'element_some - test(<.div(some(vdomElement).whenDefined), """<div><p></p></div>""")
149+
'element_none - test(<.div(none(vdomElement).whenDefined), """<div></div>""")
150+
'comp_some - test(<.div(some(H1("yoo") ).whenDefined), """<div><h1>yoo</h1></div>""")
151+
'comp_none - test(<.div(none(H1("yoo") ).whenDefined), """<div></div>""")
152+
'text_some - test(<.div(some("yoo" ).whenDefined), """<div>yoo</div>""")
153+
'text_none - test(<.div(none("yoo" ).whenDefined), """<div></div>""")
134154
}
135-
// 'maybe {
136-
// import ScalazReact._
137-
// 'attr_just - test(<.div(^.cls :=? "hi".just), """<div class="hi"></div>""")
138-
// 'attr_empty - test(<.div(^.cls :=? "h1".maybeNot), """<div></div>""")
139-
// 'style_just - test(<.div(^.color :=? "red".just), """<div style="color:red;"></div>""")
140-
// 'style_empty - test(<.div(^.color :=? "red".maybeNot), """<div></div>""")
141-
// 'tagMod_just - test(<.div(tagMod.just), """<div class="ho"></div>""")
142-
// 'tagMod_empty - test(<.div(tagMod.maybeNot), """<div></div>""")
143-
// 'tag_just - test(<.div(reacttag.just), """<div><span></span></div>""")
144-
// 'tag_empty - test(<.div(reacttag.maybeNot), """<div></div>""")
145-
// 'element_just - test(<.div(relement.just), """<div><span></span></div>""")
146-
// 'element_empty - test(<.div(relement.maybeNot), """<div></div>""")
147-
// 'comp_just - test(<.div(H1("yoo").just), """<div><h1>yoo</h1></div>""")
148-
// 'comp_empty - test(<.div(H1("yoo").maybeNot), """<div></div>""")
149-
// }
150155
'when {
151156
'tags - test(
152157
<.span(<.span("1").when(true), <.span("2").when(false)),

test/src/test/scala/japgolly/scalajs/react/core/vdom/UnprefixedTest.scala

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -99,56 +99,61 @@ object UnprefixedTest extends TestSuite {
9999

100100
'optional {
101101
'option {
102-
'attr_some - test(div(cls :=? "hi".some), """<div class="hi"></div>""")
103-
'attr_none - test(div(cls :=? "h1".none), """<div></div>""")
104-
'style_some - test(div(color :=? "red".some), """<div style="color:red;"></div>""")
105-
'style_none - test(div(color :=? "red".none), """<div></div>""")
106-
'attr_some - test(div((color := "red").some), """<div style="color:red;"></div>""")
107-
'attr_none - test(div((color := "red").none), """<div></div>""")
108-
'tagMod_some - test(div(tagMod.some), """<div class="ho"></div>""")
109-
'tagMod_none - test(div(tagMod.none), """<div></div>""")
110-
'tag_some - test(div(vdomTag.some), """<div><span></span></div>""")
111-
'tag_none - test(div(vdomTag.none), """<div></div>""")
112-
'element_some - test(div(vdomElement.some), """<div><p></p></div>""")
113-
'element_none - test(div(vdomElement.none), """<div></div>""")
114-
'comp_some - test(div(H1("yoo").some), """<div><h1>yoo</h1></div>""")
115-
'comp_none - test(div(H1("yoo").none), """<div></div>""")
116-
'text_some - test(div("yoo".some), """<div>yoo</div>""")
117-
'text_none - test(div("yoo".none), """<div></div>""")
102+
def some[A](a: A): Option[A] = Some(a)
103+
def none[A](a: A): Option[A] = None
104+
'attr_some - test(div(cls :=? some("hi") ), """<div class="hi"></div>""")
105+
'attr_none - test(div(cls :=? none("h1") ), """<div></div>""")
106+
'style_some - test(div(color :=? some("red") ), """<div style="color:red;"></div>""")
107+
'style_none - test(div(color :=? none("red") ), """<div></div>""")
108+
'tagMod_some - test(div(some(tagMod ).whenDefined), """<div class="ho"></div>""")
109+
'tagMod_none - test(div(none(tagMod ).whenDefined), """<div></div>""")
110+
'tag_some - test(div(some(vdomTag ).whenDefined), """<div><span></span></div>""")
111+
'tag_none - test(div(none(vdomTag ).whenDefined), """<div></div>""")
112+
'element_some - test(div(some(vdomElement).whenDefined), """<div><p></p></div>""")
113+
'element_none - test(div(none(vdomElement).whenDefined), """<div></div>""")
114+
'comp_some - test(div(some(H1("yoo") ).whenDefined), """<div><h1>yoo</h1></div>""")
115+
'comp_none - test(div(none(H1("yoo") ).whenDefined), """<div></div>""")
116+
'text_some - test(div(some("yoo" ).whenDefined), """<div>yoo</div>""")
117+
'text_none - test(div(none("yoo" ).whenDefined), """<div></div>""")
118118
}
119119
'jsUndefOr {
120-
'attr_def - test(div(cls :=? "hi".jsdef), """<div class="hi"></div>""")
121-
'attr_undef - test(div(cls :=? "hi".undef), """<div></div>""")
122-
'style_def - test(div(color :=? "red".jsdef), """<div style="color:red;"></div>""")
123-
'style_undef - test(div(color :=? "red".undef), """<div></div>""")
124-
'attr_def - test(div((color := "red").jsdef), """<div style="color:red;"></div>""")
125-
'attr_undef - test(div((color := "red").undef), """<div></div>""")
126-
'tagMod_def - test(div(tagMod.jsdef), """<div class="ho"></div>""")
127-
'tagMod_undef - test(div(tagMod.undef), """<div></div>""")
128-
'tag_def - test(div(vdomTag.jsdef), """<div><span></span></div>""")
129-
'tag_undef - test(div(vdomTag.undef), """<div></div>""")
130-
'element_def - test(div(vdomElement.jsdef), """<div><p></p></div>""")
131-
'element_undef - test(div(vdomElement.undef), """<div></div>""")
132-
'comp_def - test(div(H1("yoo").jsdef), """<div><h1>yoo</h1></div>""")
133-
'comp_undef - test(div(H1("yoo").undef), """<div></div>""")
134-
'text_def - test(div("yoo".jsdef), """<div>yoo</div>""")
135-
'text_undef - test(div("yoo".undef), """<div></div>""")
120+
def some[A](a: A): js.UndefOr[A] = a
121+
def none[A](a: A): js.UndefOr[A] = js.undefined
122+
'attr_some - test(div(cls :=? some("hi") ), """<div class="hi"></div>""")
123+
'attr_none - test(div(cls :=? none("h1") ), """<div></div>""")
124+
'style_some - test(div(color :=? some("red") ), """<div style="color:red;"></div>""")
125+
'style_none - test(div(color :=? none("red") ), """<div></div>""")
126+
'tagMod_some - test(div(some(tagMod ).whenDefined), """<div class="ho"></div>""")
127+
'tagMod_none - test(div(none(tagMod ).whenDefined), """<div></div>""")
128+
'tag_some - test(div(some(vdomTag ).whenDefined), """<div><span></span></div>""")
129+
'tag_none - test(div(none(vdomTag ).whenDefined), """<div></div>""")
130+
'element_some - test(div(some(vdomElement).whenDefined), """<div><p></p></div>""")
131+
'element_none - test(div(none(vdomElement).whenDefined), """<div></div>""")
132+
'comp_some - test(div(some(H1("yoo") ).whenDefined), """<div><h1>yoo</h1></div>""")
133+
'comp_none - test(div(none(H1("yoo") ).whenDefined), """<div></div>""")
134+
'text_some - test(div(some("yoo" ).whenDefined), """<div>yoo</div>""")
135+
'text_none - test(div(none("yoo" ).whenDefined), """<div></div>""")
136+
}
137+
'maybe {
138+
import ScalazReact._
139+
import scalaz.Maybe
140+
def some[A](a: A): Maybe[A] = Maybe.Just(a)
141+
def none[A](a: A): Maybe[A] = Maybe.empty
142+
'attr_some - test(div(cls :=? some("hi") ), """<div class="hi"></div>""")
143+
'attr_none - test(div(cls :=? none("h1") ), """<div></div>""")
144+
'style_some - test(div(color :=? some("red") ), """<div style="color:red;"></div>""")
145+
'style_none - test(div(color :=? none("red") ), """<div></div>""")
146+
'tagMod_some - test(div(some(tagMod ).whenDefined), """<div class="ho"></div>""")
147+
'tagMod_none - test(div(none(tagMod ).whenDefined), """<div></div>""")
148+
'tag_some - test(div(some(vdomTag ).whenDefined), """<div><span></span></div>""")
149+
'tag_none - test(div(none(vdomTag ).whenDefined), """<div></div>""")
150+
'element_some - test(div(some(vdomElement).whenDefined), """<div><p></p></div>""")
151+
'element_none - test(div(none(vdomElement).whenDefined), """<div></div>""")
152+
'comp_some - test(div(some(H1("yoo") ).whenDefined), """<div><h1>yoo</h1></div>""")
153+
'comp_none - test(div(none(H1("yoo") ).whenDefined), """<div></div>""")
154+
'text_some - test(div(some("yoo" ).whenDefined), """<div>yoo</div>""")
155+
'text_none - test(div(none("yoo" ).whenDefined), """<div></div>""")
136156
}
137-
// 'maybe {
138-
// import ScalazReact._
139-
// 'attr_just - test(div(cls :=? "hi".just), """<div class="hi"></div>""")
140-
// 'attr_empty - test(div(cls :=? "h1".maybeNot), """<div></div>""")
141-
// 'style_just - test(div(color :=? "red".just), """<div style="color:red;"></div>""")
142-
// 'style_empty - test(div(color :=? "red".maybeNot), """<div></div>""")
143-
// 'tagMod_just - test(div(tagMod.just), """<div class="ho"></div>""")
144-
// 'tagMod_empty - test(div(tagMod.maybeNot), """<div></div>""")
145-
// 'tag_just - test(div(reacttag.just), """<div><span></span></div>""")
146-
// 'tag_empty - test(div(reacttag.maybeNot), """<div></div>""")
147-
// 'element_just - test(div(relement.just), """<div><span></span></div>""")
148-
// 'element_empty - test(div(relement.maybeNot), """<div></div>""")
149-
// 'comp_just - test(div(H1("yoo").just), """<div><h1>yoo</h1></div>""")
150-
// 'comp_empty - test(div(H1("yoo").maybeNot), """<div></div>""")
151-
// }
152157
'when {
153158
'tags - test(
154159
span(span("1").when(true), span("2").when(false)),

0 commit comments

Comments
 (0)