|
| 1 | +package com.yang_bo |
| 2 | + |
| 3 | +import com.thoughtworks.binding.Binding.BindingSeq |
| 4 | +import org.scalajs.dom.* |
| 5 | +import slinky.core.facade.* |
| 6 | + |
| 7 | +private[yang_bo] trait BindingHtmlToReactImplicits2Or3 { |
| 8 | + |
| 9 | + /** Implicitly convents an `html"..."` interpolation into a React component. |
| 10 | + * @example |
| 11 | + * The following code creates a spinner from the `html"..."` interpolation |
| 12 | + * provided by [[com.yang_bo.html]]: |
| 13 | + * {{{ |
| 14 | + * import com.thoughtworks.binding.Binding |
| 15 | + * import com.thoughtworks.binding.Binding.Var |
| 16 | + * import com.yang_bo.html.* |
| 17 | + * import org.scalajs.dom.* |
| 18 | + * |
| 19 | + * def spinner(currentNumber: Var[Int]) = html""" |
| 20 | + * <button |
| 21 | + * id="minus" |
| 22 | + * onclick=${ (e: Event) => currentNumber.value -= 1 } |
| 23 | + * >-</button> |
| 24 | + * <label>${currentNumber.bind.toString}</label> |
| 25 | + * <button |
| 26 | + * id="plus" |
| 27 | + * onclick=${ (e: Event) => currentNumber.value += 1 } |
| 28 | + * >+</button> |
| 29 | + * """ |
| 30 | + * }}} |
| 31 | + * The `html"..."` interpolation can be then used as a React component with |
| 32 | + * the help of [[BindingHtmlToReact.Implicits]]: |
| 33 | + * {{{ |
| 34 | + * import com.yang_bo.BindingHtmlToReact.Implicits.* |
| 35 | + * import slinky.web.html.* |
| 36 | + * val currentNumber = Var(50) |
| 37 | + * def reactRoot = fieldset( |
| 38 | + * legend("I am a React component that contains an html interpolation"), |
| 39 | + * spinner(currentNumber) |
| 40 | + * ) |
| 41 | + * }}} |
| 42 | + * Then, the `html"..."` interpolation can be |
| 43 | + * [[com.yang_bo.html.render render]]ed into the html document, |
| 44 | + * {{{ |
| 45 | + * import slinky.web.ReactDOM |
| 46 | + * import slinky.testrenderer.TestRenderer |
| 47 | + * import org.scalajs.dom.document |
| 48 | + * TestRenderer.act { () => |
| 49 | + * ReactDOM.render(reactRoot, document.body) |
| 50 | + * } |
| 51 | + * currentNumber.value should be(50) |
| 52 | + * document.body.innerHTML should be( |
| 53 | + * """<fieldset><legend>I am a React component that contains an html interpolation</legend><span> |
| 54 | + * <button id="minus">-</button> |
| 55 | + * <label>50</label> |
| 56 | + * <button id="plus">+</button> |
| 57 | + * </span></fieldset>""" |
| 58 | + * ) |
| 59 | + * }}} |
| 60 | + * and, the `html"..."` interpolation can respond to UI events |
| 61 | + * {{{ |
| 62 | + * document |
| 63 | + * .getElementById("minus") |
| 64 | + * .asInstanceOf[HTMLButtonElement] |
| 65 | + * .onclick( |
| 66 | + * new MouseEvent("click", new MouseEventInit { |
| 67 | + * bubbles = true |
| 68 | + * cancelable = true |
| 69 | + * }) |
| 70 | + * ) |
| 71 | + * currentNumber.value should be(49) |
| 72 | + * document.body.innerHTML should be( |
| 73 | + * """<fieldset><legend>I am a React component that contains an html interpolation</legend><span> |
| 74 | + * <button id="minus">-</button> |
| 75 | + * <label>49</label> |
| 76 | + * <button id="plus">+</button> |
| 77 | + * </span></fieldset>""" |
| 78 | + * ) |
| 79 | + * }}} |
| 80 | + */ |
| 81 | + def bindingSeqToReactElement( |
| 82 | + bindingSeq: BindingSeq[Node] |
| 83 | + ): ReactElement |
| 84 | +} |
0 commit comments