-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WindowPrompt.scala
33 lines (25 loc) · 979 Bytes
/
WindowPrompt.scala
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
package japgolly.webapputil.browser
import japgolly.scalajs.react.{CallbackTo, Reusability}
/** Abstraction over `window.prompt`. */
trait WindowPrompt {
def apply(message: String): CallbackTo[Option[String]]
def apply(message: String, default: String): CallbackTo[Option[String]]
}
object WindowPrompt {
val real: WindowPrompt =
new WindowPrompt {
override def apply(message: String): CallbackTo[Option[String]] =
CallbackTo.prompt(message)
override def apply(message: String, default: String): CallbackTo[Option[String]] =
CallbackTo.prompt(message, default)
}
def const(answer: Option[String]): WindowPrompt =
const(CallbackTo.pure(answer))
def const(cb: CallbackTo[Option[String]]): WindowPrompt =
new WindowPrompt {
override def apply(message: String) = cb
override def apply(message: String, default: String) = cb
}
implicit def reusability: Reusability[WindowPrompt] =
Reusability.byRef
}