Skip to content

Commit ad0b0f0

Browse files
committed
More specific types for ReactTag
1 parent 94c9627 commit ad0b0f0

File tree

5 files changed

+20
-18
lines changed

5 files changed

+20
-18
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ object Extra {
5050
final class StringExt(private val s: String) extends AnyVal {
5151
@inline def reactAttr : Attr = Attr(s)
5252
@inline def reactStyle: Style = Style(s, s)
53-
@inline def reactTag : ReactTag = makeAbstractReactTag(s, Scalatags.NamespaceHtml.implicitNamespace)
53+
@inline def reactTag[N <: TopNode]: ReactTagOf[N] = makeAbstractReactTag(s, Scalatags.NamespaceHtml.implicitNamespace)
5454
}
5555

5656
trait Tags {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ abstract class Implicits extends LowPri {
6666
@inline implicit final def _react_cssNumber [T: Numeric](t: T): CssNumber = new CssNumber(t)
6767

6868
// Rendering
69-
@inline implicit final def _react_autoRender (t: ReactTag) : ReactElement = t.render
70-
@inline implicit final def _react_autoRenderS(t: Seq[ReactTag]): Seq[ReactElement] = t.map(_.render)
69+
@inline implicit final def _react_autoRender [T <: TopNode](t: ReactTagOf[T]) : ReactElement = t.render
70+
@inline implicit final def _react_autoRenderS[T <: TopNode](t: Seq[ReactTagOf[T]]): Seq[ReactElement] = t.map(_.render)
7171

7272
// Extensions
7373
@inline implicit final def _react_ext_attr(a: Attr) = new Extra.AttrExt(a)

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package japgolly.scalajs.react.vdom
22

3-
import org.scalajs.dom
43
import scala.annotation.{elidable, implicitNotFound}
54
import scala.scalajs.js
65
import japgolly.scalajs.react._
76
import Scalatags._
87

98
/**
10-
* Represents a value that can be nested within a [[ReactTag]]. This can be
9+
* Represents a value that can be nested within a [[ReactTagOf]]. This can be
1110
* another [[TagMod]], but can also be a CSS style or HTML attribute binding,
1211
* which will add itself to the node's attributes but not appear in the final
1312
* `children` list.
@@ -34,9 +33,10 @@ object TagMod {
3433
TagModComposition(ms.toVector)
3534
}
3635

37-
final case class ReactTag private[vdom](tag: String,
38-
modifiers: List[Seq[TagMod]],
39-
namespace: Namespace) extends DomFrag {
36+
final case class ReactTagOf[+N <: TopNode] private[vdom](
37+
tag: String,
38+
modifiers: List[Seq[TagMod]],
39+
namespace: Namespace) extends DomFrag {
4040

4141
def render: ReactElement = {
4242
val b = new Builder()
@@ -71,7 +71,7 @@ final case class ReactTag private[vdom](tag: String,
7171
}
7272
}
7373

74-
def apply(xs: TagMod*): ReactTag =
74+
def apply(xs: TagMod*): ReactTagOf[N] =
7575
this.copy(tag = tag, modifiers = xs :: modifiers)
7676

7777
override def toString = render.toString
@@ -215,9 +215,9 @@ private[vdom] object Scalatags {
215215
override def apply(b: Builder, s: Style, t: T[A]) = ot.foreach(t)(v(b, s, _))
216216
}
217217

218-
@inline def makeAbstractReactTag(tag: String, namespaceConfig: Namespace): ReactTag = {
218+
@inline def makeAbstractReactTag[N <: TopNode](tag: String, namespaceConfig: Namespace): ReactTagOf[N] = {
219219
Escaping.assertValidTag(tag)
220-
ReactTag(tag, Nil, namespaceConfig)
220+
ReactTagOf[N](tag, Nil, namespaceConfig)
221221
}
222222

223223
implicit final class SeqFrag[A <% Frag](xs: Seq[A]) extends Frag {
@@ -258,16 +258,16 @@ private[vdom] object Scalatags {
258258

259259
implicit class STStringExt(private val s: String) extends AnyVal {
260260
/**
261-
* Converts the string to a [[ReactTag]]
261+
* Converts the string to a [[ReactTagOf]]
262262
*/
263-
def tag[N <: dom.Node](implicit namespaceConfig: Namespace): ReactTag =
263+
def tag[N <: TopNode](implicit namespaceConfig: Namespace): ReactTagOf[N] =
264264
makeAbstractReactTag(s, namespaceConfig)
265265

266266
/**
267-
* Converts the string to a void [[ReactTag]]; that means that they cannot
267+
* Converts the string to a void [[ReactTagOf]]; that means that they cannot
268268
* contain any content, and can be rendered as self-closing tags.
269269
*/
270-
def voidTag[N <: dom.Node](implicit namespaceConfig: Namespace): ReactTag =
270+
def voidTag[N <: TopNode](implicit namespaceConfig: Namespace): ReactTagOf[N] =
271271
makeAbstractReactTag(s, namespaceConfig)
272272

273273
/**
@@ -284,7 +284,7 @@ private[vdom] object Scalatags {
284284
}
285285

286286
/**
287-
* Allows you to modify a [[ReactTag]] by adding a Seq containing other nest-able
287+
* Allows you to modify a [[ReactTagOf]] by adding a Seq containing other nest-able
288288
* objects to its list of children.
289289
*/
290290
implicit class SeqNode[A <% TagMod](xs: Seq[A]) extends TagMod {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package object vdom {
1919

2020
// If you're wondering why abstract class instead of trait, https://issues.scala-lang.org/browse/SI-4767
2121
abstract class Base extends Implicits {
22-
final type ReactTag = japgolly.scalajs.react.vdom.ReactTag
22+
final type ReactTag = japgolly.scalajs.react.vdom.ReactTagOf[TopNode]
2323
final type TagMod = japgolly.scalajs.react.vdom.TagMod
2424
@inline final def TagMod = japgolly.scalajs.react.vdom.TagMod
2525
@inline final def EmptyTag = japgolly.scalajs.react.vdom.EmptyTag

extra/src/main/scala/japgolly/scalajs/react/extra/router/RouterCtl.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package japgolly.scalajs.react.extra.router
22

33
import japgolly.scalajs.react._
44
import japgolly.scalajs.react.extra.Reusability
5+
import japgolly.scalajs.react.vdom.ReactTagOf
56
import japgolly.scalajs.react.vdom.prefix_<^._
7+
import org.scalajs.dom.html
68

79
/**
810
* Router controller. A client API to the router.
@@ -25,7 +27,7 @@ abstract class RouterCtl[A] {
2527
final def setOnClick(target: A): TagMod =
2628
^.onClick ==> setEH(target)
2729

28-
final def link(target: A): ReactTag =
30+
final def link(target: A): ReactTagOf[html.Anchor] =
2931
<.a(^.href := urlFor(target).value, setOnClick(target))
3032

3133
final def contramap[B](f: B => A): RouterCtl[B] =

0 commit comments

Comments
 (0)