Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow monadic hooks in forward ref components #1103

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ object ScalaFn extends DerivedDisplayName {

private def create[P, C <: Children, CT[-p, +u] <: CtorType[p, u]]
(displayName: String)
(render: Box[P] with facade.PropsWithChildren => VdomNode)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated, just concentrating calls to eval() here in order to avoid repetitions.

(render: Box[P] with facade.PropsWithChildren => Delayed[VdomNode])
(implicit s: CtorType.Summoner.Aux[Box[P], C, CT]): Component[P, CT] = {

val jsRender = render.andThen(_.rawNode): js.Function1[Box[P] with facade.PropsWithChildren, facade.React.Node]
val jsRender = render.andThen(_.eval().rawNode): js.Function1[Box[P] with facade.PropsWithChildren, facade.React.Node]
val rawComponent = jsRender.asInstanceOf[facade.React.StatelessFunctionalComponent[Box[P]]]
rawComponent.setDisplayName = displayName
JsFn.force[Box[P], C](rawComponent)(s)
Expand All @@ -35,13 +35,13 @@ object ScalaFn extends DerivedDisplayName {
// ===================================================================================================================

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

def withChildren[P](render: (P, PropsChildren) => Delayed[VdomNode])(implicit s: CtorType.Summoner[Box[P], Children.Varargs], name: FullName): Component[P, s.CT] =
create[P, Children.Varargs, s.CT](derivedDisplayName)(b => render(b.unbox, PropsChildren(b.children)).eval())(s)
create[P, Children.Varargs, s.CT](derivedDisplayName)(b => render(b.unbox, PropsChildren(b.children)))(s)

def justChildren(render: PropsChildren => Delayed[VdomNode])(implicit name: FullName): Component[Unit, CtorType.Children] =
create(derivedDisplayName)(b => render(PropsChildren(b.children)).eval())
create(derivedDisplayName)(b => render(PropsChildren(b.children)))

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

Expand All @@ -64,13 +64,13 @@ object ScalaFn extends DerivedDisplayName {
// ===================================================================================================================

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

def withChildren[P](render: (P, PropsChildren) => Delayed[VdomNode])(implicit s: CtorType.Summoner[Box[P], Children.Varargs]): Component[P, s.CT] =
create[P, Children.Varargs, s.CT](displayName)(b => render(b.unbox, PropsChildren(b.children)).eval())(s)
create[P, Children.Varargs, s.CT](displayName)(b => render(b.unbox, PropsChildren(b.children)))(s)

def justChildren(render: PropsChildren => Delayed[VdomNode]): Component[Unit, CtorType.Children] =
create(displayName)(b => render(PropsChildren(b.children)).eval())
create(displayName)(b => render(PropsChildren(b.children)))

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ object ReactForwardRefInternals extends DerivedDisplayName {

protected def create[P, C <: Children, CT[-p, +u] <: CtorType[p, u]]
(displayName: String)
(render: (Box[P] with facade.PropsWithChildren, Option[R]) => VdomNode)
(render: (Box[P] with facade.PropsWithChildren, Option[R]) => Delayed[VdomNode])
(implicit s: CtorType.Summoner.Aux[Box[P], C, CT]): Component[P, RefValue, CT]

final def apply(render: Option[R] => VdomNode)(implicit name: FullName): Component[Unit, RefValue, CtorType.Nullary] =
final def apply(render: Option[R] => Delayed[VdomNode])(implicit name: FullName): Component[Unit, RefValue, CtorType.Nullary] =
create(derivedDisplayName)((_, r) => render(r))

final def apply[P](render: (P, Option[R]) => VdomNode)(implicit name: FullName): Component[P, RefValue, CtorType.Props] =
final def apply[P](render: (P, Option[R]) => Delayed[VdomNode])(implicit name: FullName): Component[P, RefValue, CtorType.Props] =
create(derivedDisplayName)((p, r) => render(p.unbox, r))

final def withChildren[P](render: (P, PropsChildren, Option[R]) => VdomNode)(implicit name: FullName): Component[P, RefValue, CtorType.PropsAndChildren] =
final def withChildren[P](render: (P, PropsChildren, Option[R]) => Delayed[VdomNode])(implicit name: FullName): Component[P, RefValue, CtorType.PropsAndChildren] =
create(derivedDisplayName)((b, r) => render(b.unbox, PropsChildren(b.children), r))

final def justChildren(render: (PropsChildren, Option[R]) => VdomNode)(implicit name: FullName): Component[Unit, RefValue, CtorType.Children] =
final def justChildren(render: (PropsChildren, Option[R]) => Delayed[VdomNode])(implicit name: FullName): Component[Unit, RefValue, CtorType.Children] =
create(derivedDisplayName)((b, r) => render(PropsChildren(b.children), r))
}

Expand All @@ -46,26 +46,26 @@ object ReactForwardRefInternals extends DerivedDisplayName {

override protected def create[P, C <: Children, CT[-p, +u] <: CtorType[p, u]]
(displayName: String)
(render: (Box[P] with facade.PropsWithChildren, Option[R]) => VdomNode)
(render: (Box[P] with facade.PropsWithChildren, Option[R]) => Delayed[VdomNode])
(implicit s: CtorType.Summoner.Aux[Box[P], C, CT]): Component[P, RefValue, CT] =
ReactForwardRef.create[P, RefValue, C, CT](displayName)((p, r) => render(p, r.map(_.map(
Js.mounted[P0, S0](_).withRawType[RM]
))))
))).eval())

@inline def withDisplayName(name: String): DisplayNameApplied =
new DisplayNameApplied(name)

class DisplayNameApplied private[ToJsComponent](displayName: String) {
final def apply(render: Option[R] => VdomNode): Component[Unit, RefValue, CtorType.Nullary] =
final def apply(render: Option[R] => Delayed[VdomNode]): Component[Unit, RefValue, CtorType.Nullary] =
create(displayName)((_, r) => render(r))

final def apply[P](render: (P, Option[R]) => VdomNode): Component[P, RefValue, CtorType.Props] =
final def apply[P](render: (P, Option[R]) => Delayed[VdomNode]): Component[P, RefValue, CtorType.Props] =
create(displayName)((p, r) => render(p.unbox, r))

final def withChildren[P](render: (P, PropsChildren, Option[R]) => VdomNode): Component[P, RefValue, CtorType.PropsAndChildren] =
final def withChildren[P](render: (P, PropsChildren, Option[R]) => Delayed[VdomNode]): Component[P, RefValue, CtorType.PropsAndChildren] =
create(displayName)((b, r) => render(b.unbox, PropsChildren(b.children), r))

final def justChildren(render: (PropsChildren, Option[R]) => VdomNode): Component[Unit, RefValue, CtorType.Children] =
final def justChildren(render: (PropsChildren, Option[R]) => Delayed[VdomNode]): Component[Unit, RefValue, CtorType.Children] =
create(displayName)((b, r) => render(PropsChildren(b.children), r))
}
}
Expand All @@ -77,24 +77,24 @@ object ReactForwardRefInternals extends DerivedDisplayName {

override protected def create[P, C <: Children, CT[-p, +u] <: CtorType[p, u]]
(displayName: String)
(render: (Box[P] with facade.PropsWithChildren, Option[R]) => VdomNode)
(render: (Box[P] with facade.PropsWithChildren, Option[R]) => Delayed[VdomNode])
(implicit s: CtorType.Summoner.Aux[Box[P], C, CT]): Component[P, RefValue, CT] =
ReactForwardRef.create[P, RefValue, C, CT](displayName)((p, r) => render(p, r.map(_.map(_.mountedImpure))))
ReactForwardRef.create[P, RefValue, C, CT](displayName)((p, r) => render(p, r.map(_.map(_.mountedImpure))).eval())

@inline def withDisplayName(name: String): DisplayNameApplied =
new DisplayNameApplied(name)

class DisplayNameApplied private[ToScalaComponent](displayName: String) {
final def apply(render: Option[R] => VdomNode): Component[Unit, RefValue, CtorType.Nullary] =
final def apply(render: Option[R] => Delayed[VdomNode]): Component[Unit, RefValue, CtorType.Nullary] =
create(displayName)((_, r) => render(r))

final def apply[P](render: (P, Option[R]) => VdomNode): Component[P, RefValue, CtorType.Props] =
final def apply[P](render: (P, Option[R]) => Delayed[VdomNode]): Component[P, RefValue, CtorType.Props] =
create(displayName)((p, r) => render(p.unbox, r))

final def withChildren[P](render: (P, PropsChildren, Option[R]) => VdomNode): Component[P, RefValue, CtorType.PropsAndChildren] =
final def withChildren[P](render: (P, PropsChildren, Option[R]) => Delayed[VdomNode]): Component[P, RefValue, CtorType.PropsAndChildren] =
create(displayName)((b, r) => render(b.unbox, PropsChildren(b.children), r))

final def justChildren(render: (PropsChildren, Option[R]) => VdomNode): Component[Unit, RefValue, CtorType.Children] =
final def justChildren(render: (PropsChildren, Option[R]) => Delayed[VdomNode]): Component[Unit, RefValue, CtorType.Children] =
create(displayName)((b, r) => render(PropsChildren(b.children), r))
}
}
Expand All @@ -105,12 +105,12 @@ object ReactForwardRef { outer =>

private[component] def create[P, R, C <: Children, CT[-p, +u] <: CtorType[p, u]]
(displayName: String)
(render: (Box[P] with facade.PropsWithChildren, Option[Ref.Simple[R]]) => VdomNode)
(render: (Box[P] with facade.PropsWithChildren, Option[Ref.Simple[R]]) => Delayed[VdomNode])
(implicit s: CtorType.Summoner.Aux[Box[P], C, CT]): Component[P, R, CT] = {

val jsRender: js.Function2[Box[P] with facade.PropsWithChildren, facade.React.ForwardedRef[R], facade.React.Node] =
(p: Box[P] with facade.PropsWithChildren, r: facade.React.ForwardedRef[R]) =>
render(p, Ref.forwardedFromJs(r)).rawNode
render(p, Ref.forwardedFromJs(r)).eval().rawNode

val rawComponent = facade.React.forwardRef(jsRender)
rawComponent.displayName = displayName
Expand All @@ -127,16 +127,16 @@ object ReactForwardRef { outer =>
new DisplayNameApplied(name)


def apply[R](render: Option[Ref.Simple[R]] => VdomNode)(implicit name: FullName): Component[Unit, R, CtorType.Nullary] =
def apply[R](render: Option[Ref.Simple[R]] => Delayed[VdomNode])(implicit name: FullName): Component[Unit, R, CtorType.Nullary] =
create(derivedDisplayName)((_, r) => render(r))

def apply[P, R](render: (P, Option[Ref.Simple[R]]) => VdomNode)(implicit name: FullName): Component[P, R, CtorType.Props] =
def apply[P, R](render: (P, Option[Ref.Simple[R]]) => Delayed[VdomNode])(implicit name: FullName): Component[P, R, CtorType.Props] =
create(derivedDisplayName)((p, r) => render(p.unbox, r))

def withChildren[P, R](render: (P, PropsChildren, Option[Ref.Simple[R]]) => VdomNode)(implicit name: FullName): Component[P, R, CtorType.PropsAndChildren] =
def withChildren[P, R](render: (P, PropsChildren, Option[Ref.Simple[R]]) => Delayed[VdomNode])(implicit name: FullName): Component[P, R, CtorType.PropsAndChildren] =
create(derivedDisplayName)((b, r) => render(b.unbox, PropsChildren(b.children), r))

def justChildren[R](render: (PropsChildren, Option[Ref.Simple[R]]) => VdomNode)(implicit name: FullName): Component[Unit, R, CtorType.Children] =
def justChildren[R](render: (PropsChildren, Option[Ref.Simple[R]]) => Delayed[VdomNode])(implicit name: FullName): Component[Unit, R, CtorType.Children] =
create(derivedDisplayName)((b, r) => render(PropsChildren(b.children), r))

// ===================================================================================================================
Expand All @@ -154,16 +154,16 @@ object ReactForwardRef { outer =>
new ToScalaComponent(())

class DisplayNameApplied private[ReactForwardRef](displayName: String) {
def apply[R](render: Option[Ref.Simple[R]] => VdomNode): Component[Unit, R, CtorType.Nullary] =
def apply[R](render: Option[Ref.Simple[R]] => Delayed[VdomNode]): Component[Unit, R, CtorType.Nullary] =
create(displayName)((_, r) => render(r))

def apply[P, R](render: (P, Option[Ref.Simple[R]]) => VdomNode): Component[P, R, CtorType.Props] =
def apply[P, R](render: (P, Option[Ref.Simple[R]]) => Delayed[VdomNode]): Component[P, R, CtorType.Props] =
create(displayName)((p, r) => render(p.unbox, r))

def withChildren[P, R](render: (P, PropsChildren, Option[Ref.Simple[R]]) => VdomNode): Component[P, R, CtorType.PropsAndChildren] =
def withChildren[P, R](render: (P, PropsChildren, Option[Ref.Simple[R]]) => Delayed[VdomNode]): Component[P, R, CtorType.PropsAndChildren] =
create(displayName)((b, r) => render(b.unbox, PropsChildren(b.children), r))

def justChildren[R](render: (PropsChildren, Option[Ref.Simple[R]]) => VdomNode): Component[Unit, R, CtorType.Children] =
def justChildren[R](render: (PropsChildren, Option[Ref.Simple[R]]) => Delayed[VdomNode]): Component[Unit, R, CtorType.Children] =
create(displayName)((b, r) => render(PropsChildren(b.children), r))
}
}
Loading