Skip to content

Commit

Permalink
Merge pull request #1001 from japgolly/topic/fncomp-with-reuse
Browse files Browse the repository at this point in the history
Add ScalaFnComponent withReuse methods
  • Loading branch information
japgolly authored Oct 22, 2021
2 parents 42c49d6 + a175e3b commit 43d7634
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import japgolly.scalajs.react.internal._
import japgolly.scalajs.react.vdom.VdomNode
import japgolly.scalajs.react.{Children, CtorType, PropsChildren, facade}
import scala.scalajs.js
import japgolly.scalajs.react.Reusability

object ScalaFn {

Expand All @@ -23,6 +24,11 @@ object ScalaFn {
.mapUnmounted(_.mapUnmountedProps(_.unbox))
}

@inline def withHooks[P] =
HookComponentBuilder.apply[P]

// ===================================================================================================================

def apply[P](render: P => VdomNode)(implicit s: CtorType.Summoner[Box[P], Children.None]): Component[P, s.CT] =
create[P, Children.None, s.CT](b => render(b.unbox))(s)

Expand All @@ -32,6 +38,17 @@ object ScalaFn {
def justChildren(render: PropsChildren => VdomNode): Component[Unit, CtorType.Children] =
create(b => render(PropsChildren(b.children)))

@inline def withHooks[P] =
HookComponentBuilder.apply[P]
// ===================================================================================================================

def withReuse[P](render: P => VdomNode)(implicit s: CtorType.Summoner[Box[P], Children.None], r: Reusability[P]): Component[P, s.CT] =
withHooks[P].renderWithReuse(render)(s, r)

def withReuseBy[P, A](reusableInputs: P => A)(render: A => VdomNode)(implicit s: CtorType.Summoner[Box[P], Children.None], r: Reusability[A]): Component[P, s.CT] =
withHooks[P].renderWithReuseBy(reusableInputs)(render)(s, r)

def withChildrenAndReuse[P](render: (P, PropsChildren) => VdomNode)(implicit s: CtorType.Summoner[Box[P], Children.Varargs], rp: Reusability[P], rc: Reusability[PropsChildren]): Component[P, s.CT] =
withHooks[P].withPropsChildren.renderWithReuse(i => render(i.props, i.propsChildren))

def withChildrenAndReuse[P, A](reusableInputs: (P, PropsChildren) => A)(render: A => VdomNode)(implicit s: CtorType.Summoner[Box[P], Children.Varargs], r: Reusability[A]): Component[P, s.CT] =
withHooks[P].withPropsChildren.renderWithReuseBy(i => reusableInputs(i.props, i.propsChildren))(render)
}
2 changes: 2 additions & 0 deletions doc/changelog/2.0.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ You can run the script in the Migration section at the bottom of the page to aut
* Add:
* `AsyncCallback.debounce(duration): AsyncCallback[Unit]`
* `Callback.debounce(duration): Callback`
* `ScalaFnComponent.withReuse{,By}`
* `ScalaFnComponent.withChildrenAndReuse{,By}`
* `ReactTestUtils` is now a `trait` as well as an `object` so that you can mix it into your own test utils collection
* Upgrade deps
* Cats-effect to 3.2.9
Expand Down

0 comments on commit 43d7634

Please sign in to comment.