Skip to content

Commit

Permalink
Use plain js WithSizeProps
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed Jul 29, 2022
1 parent bd272dc commit 755be58
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ object PanelStack extends FunctionComponent[PanelStackProps] {
val props = compProps.wrapped
val topComp = props.stack.peek.component

<(withSizeComp())(^.wrapped := WithSizeProps({ (width, height) =>
<(withSizeComp())(^.plain := WithSizeProps({ (width, height) =>
<(PanelStack.Context.Provider)(^.contextValue := props.copy(width = width, height = height))(
<(topComp)()(),
compProps.children
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ class PanelStackSpec extends TestSpec with TestRendererUtils {
}

private def renderWithSize(element: ReactElement): TestInstance = {
val withSizeProps = findComponentProps(testRender(element), withSizeComp)
val withSizeProps = findComponentProps(testRender(element), withSizeComp, plain = true)
createTestRenderer(withSizeProps.render(width, height)).root
}

Expand Down
2 changes: 2 additions & 0 deletions ui/src/main/scala/farjs/ui/Exports.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ object Exports {
val TextAlign: TextAlign = farjs.ui.TextAlign

val TextLine: ReactClass = farjs.ui.TextLine()

val WithSize: ReactClass = farjs.ui.WithSize()
}
4 changes: 1 addition & 3 deletions ui/src/main/scala/farjs/ui/WithSize.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import scommons.react._
import scommons.react.blessed._
import scommons.react.hooks._

case class WithSizeProps(render: (Int, Int) => ReactElement)

object WithSize extends FunctionComponent[WithSizeProps] {

protected def render(props: Props): ReactElement = {
Expand All @@ -24,7 +22,7 @@ object WithSize extends FunctionComponent[WithSizeProps] {
setSize((currBox.width, currBox.height))
}
)(
props.wrapped.render(width, height)
props.plain.render(width, height)
)
}
}
24 changes: 24 additions & 0 deletions ui/src/main/scala/farjs/ui/WithSizeProps.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package farjs.ui

import scommons.react.ReactElement

import scala.scalajs.js

sealed trait WithSizeProps extends js.Object {
val render: js.Function2[Int, Int, ReactElement]
}

object WithSizeProps {

def apply(render: js.Function2[Int, Int, ReactElement]): WithSizeProps = {
js.Dynamic.literal(
render = render
).asInstanceOf[WithSizeProps]
}

def unapply(arg: WithSizeProps): Option[js.Function2[Int, Int, ReactElement]] = {
Some(
arg.render
)
}
}
2 changes: 1 addition & 1 deletion ui/src/main/scala/farjs/ui/menu/BottomMenu.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object BottomMenu extends FunctionComponent[Unit] {

protected def render(compProps: Props): ReactElement = {

<(withSizeComp())(^.wrapped := WithSizeProps({ (width, _) =>
<(withSizeComp())(^.plain := WithSizeProps({ (width, _) =>

<(bottomMenuViewComp())(^.wrapped := BottomMenuViewProps(
width = width,
Expand Down
4 changes: 2 additions & 2 deletions ui/src/test/scala/farjs/ui/WithSizeSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class WithSizeSpec extends TestSpec with TestRendererUtils {
<.text()(s"width: $width, height: $height")
})
val boxMock = literal("width" -> 1, "height" -> 2)
val comp = createTestRenderer(<(WithSize())(^.wrapped := props)(), { el =>
val comp = createTestRenderer(<(WithSize())(^.plain := props)(), { el =>
if (el.`type` == "box".asInstanceOf[js.Any]) boxMock
else null
}).root
Expand All @@ -38,7 +38,7 @@ class WithSizeSpec extends TestSpec with TestRendererUtils {
val props = WithSizeProps({ (width, height) =>
<.text()(s"width: $width, height: $height")
})
val comp = <(WithSize())(^.wrapped := props)()
val comp = <(WithSize())(^.plain := props)()
val boxMock = literal("width" -> 1, "height" -> 2)

//when
Expand Down
2 changes: 1 addition & 1 deletion ui/src/test/scala/farjs/ui/menu/BottomMenuSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BottomMenuSpec extends TestSpec with TestRendererUtils {
private def assertBottomMenu(result: TestInstance): Unit = {
val (width, height) = (80, 25)

assertTestComponent(result, withSizeComp) { case WithSizeProps(render) =>
assertTestComponent(result, withSizeComp, plain = true) { case WithSizeProps(render) =>
val result = createTestRenderer(render(width, height)).root

assertTestComponent(result, bottomMenuViewComp) { case BottomMenuViewProps(resWidth, resItems) =>
Expand Down

0 comments on commit 755be58

Please sign in to comment.