|
| 1 | +package japgolly.scalajs.react |
| 2 | + |
| 3 | +import scala.scalajs.js |
| 4 | + |
| 5 | +/** |
| 6 | + * A component that takes `props` as an argument and returns the element to render. |
| 7 | + * |
| 8 | + * These components behave just like a React class with only a `render` method defined. |
| 9 | + * Since no component instance is created for a functional component, any ref added to one will evaluate to `null`. |
| 10 | + * Functional components do not have lifecycle methods. |
| 11 | + * |
| 12 | + * "In the future, we’ll also be able to make performance optimizations specific to these components by avoiding |
| 13 | + * unnecessary checks and memory allocations." |
| 14 | + * - https://facebook.github.io/react/blog/2015/10/07/react-v0.14.html |
| 15 | + * |
| 16 | + * @since React 0.14 |
| 17 | + */ |
| 18 | +sealed trait FunctionalComponent[-P] extends js.Any |
| 19 | +// type FunctionalComponent[-P] = js.Function1[P, ReactElement] |
| 20 | +// ↑ Doesn't work for Scala for two reasons: |
| 21 | +// 1) Application returns a `ReactElement` immediately which makes it just a normal function. |
| 22 | +// 2) React expects P to be a JavaScript object which Scala values are not. |
| 23 | + |
| 24 | +object FunctionalComponent { |
| 25 | + |
| 26 | + /** |
| 27 | + * Creates a [[FunctionalComponent]]. |
| 28 | + * |
| 29 | + * If `props` is `Unit`, there is also [[ReactComponentB.static]] which also sets `shouldComponentUpdate` to `false` |
| 30 | + * which should be more efficient. |
| 31 | + */ |
| 32 | + def apply[P](render: P => ReactElement): FunctionalComponent[P] = { |
| 33 | + val f = (o: WrapObj[P]) => render(o.v) |
| 34 | + val jf = f: js.Function1[WrapObj[P], ReactElement] |
| 35 | + jf.asInstanceOf[FunctionalComponent[P]] |
| 36 | + } |
| 37 | + |
| 38 | + @inline implicit class Ops[P](private val f: FunctionalComponent[P]) extends AnyVal { |
| 39 | + def apply(props: P) = |
| 40 | + // This is what JSX/Babel does: |
| 41 | + React.createElement(f, WrapObj(props)) |
| 42 | + } |
| 43 | + |
| 44 | + // =================================================================================================================== |
| 45 | + |
| 46 | + /** |
| 47 | + * A [[FunctionalComponent]] that accepts [[PropsChildren]] in addition to `props`. |
| 48 | + */ |
| 49 | + sealed trait WithChildren[-P] extends js.Any |
| 50 | + |
| 51 | + def withChildren[P](render: (P, PropsChildren) => ReactElement): WithChildren[P] = { |
| 52 | + val f = (o: WrapObj[P]) => render(o.v, o.asInstanceOf[js.Dynamic].children.asInstanceOf[PropsChildren]) |
| 53 | + val jf = f: js.Function1[WrapObj[P], ReactElement] |
| 54 | + jf.asInstanceOf[WithChildren[P]] |
| 55 | + } |
| 56 | + |
| 57 | + @inline implicit class WithChildrenOps[P](private val f: FunctionalComponent.WithChildren[P]) extends AnyVal { |
| 58 | + def apply(props: P, children: ReactNode*) = |
| 59 | + // This is what JSX/Babel does: |
| 60 | + React.createElement(f, WrapObj(props), children: _*) |
| 61 | + } |
| 62 | +} |
0 commit comments