Last active
March 8, 2018 20:06
-
-
Save trepidacious/0587f5a4a7ff5c2a850b61470524a0d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Routes { | |
sealed trait Page | |
case class SearchPage(term: String) extends Page | |
val routerConfig = RouterConfigDsl[Page].buildConfig { dsl => | |
import dsl._ | |
def uriString(regex: String): RouteB[String] = new RouteB(regex, 1, g => Some(decodeURIComponent(g(0))), encodeURIComponent) | |
/** Captures the (potentially-empty) remaining portion of the URL path, */ | |
def remainingPathOrBlankURIEncoded: RouteB[String] = uriString("(.*)$") | |
//Provide a renderer for a view factory using Pages. | |
def dynRenderP[P <: Page](g: Pages[P, P] => VdomElement): P => Renderer = | |
p => Renderer(r => g(Pages(p, r.narrow[P]))) | |
val searchRoute = ("#search?" ~ remainingPathOrBlankURIEncoded).caseClass[SearchPage] | |
(trimSlashes | |
| dynamicRouteCT(searchRoute) ~> dynRenderP[SearchPage](ResViews.SearchView(_): VdomElement) | |
) | |
.notFound(redirectToPage(HomePage)(Redirect.Replace)) | |
.renderWith(layout) | |
.verify(SearchPage("")) | |
} | |
/** | |
* A current page, and a RouterCtl, e.g. to navigate to new pages | |
* @param current The currently displayed page | |
* @param ctl The RouterCtl | |
* @tparam P The type of page we can route to | |
* @tparam C The type of current page | |
*/ | |
case class Pages[+C, P](current: C, ctl: RouterCtl[P]) | |
val SearchView = | |
ScalaComponent.builder[Pages[SearchPage, SearchPage]]("SearchView") | |
.render(scope => { | |
MuiTextField( | |
value = scope.props.current.term, | |
onChange = (e: ReactEventFromInput, s: String) => e.preventDefaultCB >> scope.props.ctl.set(SearchPage(s)), | |
floatingLabelText = "Search": VdomNode | |
)() | |
} | |
) | |
.configure(Reusability.shouldComponentUpdateWithOverlay) | |
.build |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment