From 7800348688c3967eae1a86a11b2c4ce1eaa99286 Mon Sep 17 00:00:00 2001 From: Nikita Gazarov Date: Wed, 19 Feb 2025 02:12:07 +0300 Subject: [PATCH] Test: Fix example to match usage of StyleSetter[String] in laminar v18 --- .../thirdparty/defs/styles/StyleProps.scala | 2 +- .../thirdparty/defs/styles/traits/Auto.scala | 2 +- .../defs/styles/traits/GlobalKeywords.scala | 8 +++--- .../thirdparty/defs/styles/traits/None.scala | 2 +- .../defs/styles/traits/Normal.scala | 2 +- .../scala/com/thirdparty/keys/StyleProp.scala | 25 +++++++++++-------- .../com/raquo/domtypes/GeneratorSpec.scala | 4 +-- 7 files changed, 24 insertions(+), 21 deletions(-) diff --git a/js/src/test/scala/com/thirdparty/defs/styles/StyleProps.scala b/js/src/test/scala/com/thirdparty/defs/styles/StyleProps.scala index fafe0be..9299f3a 100644 --- a/js/src/test/scala/com/thirdparty/defs/styles/StyleProps.scala +++ b/js/src/test/scala/com/thirdparty/defs/styles/StyleProps.scala @@ -16,7 +16,7 @@ trait StyleProps { protected type DSP[V] = DerivedStyleProp[V] - protected type SS = StyleSetter[_] + protected type SS = StyleSetter[String] def styleProp[V](name: String): StyleProp[V] = new StyleProp(name) diff --git a/js/src/test/scala/com/thirdparty/defs/styles/traits/Auto.scala b/js/src/test/scala/com/thirdparty/defs/styles/traits/Auto.scala index 5d718dc..5e190b4 100644 --- a/js/src/test/scala/com/thirdparty/defs/styles/traits/Auto.scala +++ b/js/src/test/scala/com/thirdparty/defs/styles/traits/Auto.scala @@ -10,6 +10,6 @@ import com.thirdparty.setters.StyleSetter trait Auto[V] { this: StyleProp[V] => - lazy val auto: StyleSetter[V] = this := "auto" + lazy val auto: StyleSetter[String] = this := "auto" } diff --git a/js/src/test/scala/com/thirdparty/defs/styles/traits/GlobalKeywords.scala b/js/src/test/scala/com/thirdparty/defs/styles/traits/GlobalKeywords.scala index 638ddab..76df6ef 100644 --- a/js/src/test/scala/com/thirdparty/defs/styles/traits/GlobalKeywords.scala +++ b/js/src/test/scala/com/thirdparty/defs/styles/traits/GlobalKeywords.scala @@ -14,20 +14,20 @@ trait GlobalKeywords[V] { this: StyleProp[V] => * The initial CSS keyword applies the initial (or default) value of a * property to an element. */ - lazy val initial: StyleSetter[V] = this := "initial" + lazy val initial: StyleSetter[String] = this := "initial" /** * The inherit CSS keyword causes the element for which it is specified to * take the computed value of the property from its parent element. */ - lazy val inherit: StyleSetter[V] = this := "inherit" + lazy val inherit: StyleSetter[String] = this := "inherit" /** * The revert CSS keyword reverts the cascaded value of the property from its * current value to the value the property would have had if no changes had * been made by the current style origin to the current element. */ - lazy val revert: StyleSetter[V] = this := "revert" + lazy val revert: StyleSetter[String] = this := "revert" /** * The unset CSS keyword resets a property to its inherited value if the @@ -36,6 +36,6 @@ trait GlobalKeywords[V] { this: StyleProp[V] => * when the property is an inherited property, and like the initial keyword in * the second case, when the property is a non-inherited property. */ - lazy val unset: StyleSetter[V] = this := "unset" + lazy val unset: StyleSetter[String] = this := "unset" } diff --git a/js/src/test/scala/com/thirdparty/defs/styles/traits/None.scala b/js/src/test/scala/com/thirdparty/defs/styles/traits/None.scala index d48b8e3..bda6ef8 100644 --- a/js/src/test/scala/com/thirdparty/defs/styles/traits/None.scala +++ b/js/src/test/scala/com/thirdparty/defs/styles/traits/None.scala @@ -10,6 +10,6 @@ import com.thirdparty.setters.StyleSetter trait None[V] { this: StyleProp[V] => - lazy val none: StyleSetter[V] = this := "none" + lazy val none: StyleSetter[String] = this := "none" } diff --git a/js/src/test/scala/com/thirdparty/defs/styles/traits/Normal.scala b/js/src/test/scala/com/thirdparty/defs/styles/traits/Normal.scala index 57494e8..c62b0f0 100644 --- a/js/src/test/scala/com/thirdparty/defs/styles/traits/Normal.scala +++ b/js/src/test/scala/com/thirdparty/defs/styles/traits/Normal.scala @@ -10,6 +10,6 @@ import com.thirdparty.setters.StyleSetter trait Normal[V] { this: StyleProp[V] => - lazy val normal: StyleSetter[V] = this := "normal" + lazy val normal: StyleSetter[String] = this := "normal" } diff --git a/js/src/test/scala/com/thirdparty/keys/StyleProp.scala b/js/src/test/scala/com/thirdparty/keys/StyleProp.scala index 2f5759f..2df6d4c 100644 --- a/js/src/test/scala/com/thirdparty/keys/StyleProp.scala +++ b/js/src/test/scala/com/thirdparty/keys/StyleProp.scala @@ -4,26 +4,29 @@ import com.thirdparty.defs.styles.traits.GlobalKeywords import com.thirdparty.setters.StyleSetter import scala.language.implicitConversions -import scala.scalajs.js.| case class StyleProp[V]( val domName: String -) extends DerivedStyleBuilder[StyleSetter[_], DerivedStyleProp] with GlobalKeywords[V] { +) extends DerivedStyleBuilder[StyleSetter[String], DerivedStyleProp] with GlobalKeywords[V] { - def := (value: V | String): StyleSetter[V] = StyleSetter(this, value.toString) + def := (value: V): StyleSetter[V] = StyleSetter(this, value.toString) - def := (value: String): StyleSetter[V] = StyleSetter(this, value) + // def := (value: V | String): StyleSetter[V] = StyleSetter(this, value.toString) - override protected def styleSetter(value: String): StyleSetter[_] = this := value + // def := (value: String): StyleSetter[V] = StyleSetter(this, value) + + override protected def styleSetter(value: String): StyleSetter[String] = this := value override protected def derivedStyle[A](encode: A => String): DerivedStyleProp[A] = { new DerivedStyleProp[A](this, encode) } } -// object StyleProp { -// -// implicit def stylePropToStringStyleProp[V](p: StyleProp[V]): StyleProp[String] = { -// p.asInstanceOf[StyleProp[String]] -// } -// } +object StyleProp { + + // In CSS, every style prop can be set to a string value, and this allows it. + // You need to make sure that your StyleProp implementation is safe with such a casting implicit conversion. See Laminar v18+ for example. + implicit def stylePropToStringStyleProp[V](p: StyleProp[V]): StyleProp[String] = { + p.asInstanceOf[StyleProp[String]] + } +} diff --git a/jvm/src/test/scala/com/raquo/domtypes/GeneratorSpec.scala b/jvm/src/test/scala/com/raquo/domtypes/GeneratorSpec.scala index 604bbd2..d550373 100644 --- a/jvm/src/test/scala/com/raquo/domtypes/GeneratorSpec.scala +++ b/jvm/src/test/scala/com/raquo/domtypes/GeneratorSpec.scala @@ -293,7 +293,7 @@ class GeneratorSpec extends AnyFunSpec with Matchers { traitName = traitName, keyKind = "StyleProp", keyKindAlias = "StyleProp", - setterType = "StyleSetter[_]", + setterType = "StyleSetter[String]", setterTypeAlias = "SS", derivedKeyKind = "DerivedStyleProp", derivedKeyKindAlias = "DSP", @@ -340,7 +340,7 @@ class GeneratorSpec extends AnyFunSpec with Matchers { traitExtendsFallbackTypeParam = Some("String"), extendsUnitTraits = styleTrait.extendsUnits, propKind = "StyleProp", - keywordType = keywordType, + keywordType = "StyleSetter[String]", derivedKeyKind = "DerivedStyleProp", lengthUnitsNumType = None, //Some("Int | Double"), defType = LazyVal,