Last active
December 4, 2016 22:43
-
-
Save tadjik1/6376fd736575aba36cd6d20497155bea to your computer and use it in GitHub Desktop.
Elm component realised <Link> component from react-router
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
{- usage: | |
View.elm | |
view : Model -> Html Msg | |
view model = | |
div [] | |
[ link { href = "/lala1", text = "change link" } | |
] | |
-} | |
module Components.Link exposing (link) | |
import Json.Decode as Json | |
import Html exposing (Html, Attribute, a, text) | |
import Html.Attributes exposing (href) | |
import Html.Events exposing (onWithOptions) | |
import Message exposing (Msg(NavigateTo)) | |
type alias Options = | |
{ href : String | |
, text : String | |
} | |
preventDefault : String -> Attribute Msg | |
preventDefault href = | |
onWithOptions "click" { stopPropagation = False, preventDefault = True } (Json.succeed (NavigateTo href)) | |
link : Options -> Html Msg | |
link options = | |
a [ href options.href, preventDefault options.href ] [ text options.text ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment