aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/reading/component/widget/Input.scala
blob: 7dac47a45b5893dee280441b38c586db74222509 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package reading.component.widget

import scalatags.JsDom.all._

import org.scalajs.dom.KeyboardEvent
import org.scalajs.dom.html.Input

import scalacss.Defaults._
import scalacss.ScalatagsCss._

import rx._

import reading.component.widget.style.{ Input => InputStyle }

object Input {
  def apply(
    style: StyleA,
    query: Var[String],
    label: String = "",
    onEnter: => Unit = ()
  )(
    implicit
    ctx: Ctx.Owner
  ): Frag = {
    val inputBox = input(
      InputStyle.render,
      InputStyle.input,
      style,
      placeholder := label,
      onkeyup := { (e: KeyboardEvent) =>
        val input = e.target.asInstanceOf[Input]
        query() = input.value
        input.value = input.value
        if (e.keyCode == 13) onEnter
      }
    ).render

    query.trigger {
      inputBox.value = query.now
    }

    inputBox
  }
}