Last active
November 16, 2016 12:44
-
-
Save opsb/70a2477494081a4e2806ce6b244a2cbc to your computer and use it in GitHub Desktop.
Replacing customDecoder for elm 0.18
This file contains hidden or 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
| onEnter : Msg -> Attribute Msg | |
| onEnter msg = | |
| let | |
| filterKey ( code, shift ) = | |
| if (isEnter code) && (not shift) then | |
| Ok "triggering" | |
| else | |
| Err "not triggering" | |
| decoder = | |
| JD.customDecoder keyExtractor filterKey | |
| |> JD.map (always msg) | |
| in | |
| on "keydown" decoder | |
| isEnter : Int -> Bool | |
| isEnter code = | |
| code == 13 | |
| keyExtractor : JD.Decoder ( Int, Bool ) | |
| keyExtractor = | |
| JD.object2 (,) | |
| ("keyCode" := JD.int) | |
| ("shiftKey" := JD.bool) | |
| preventDefaultOnEnter : Attribute Msg | |
| preventDefaultOnEnter = | |
| let | |
| eventOptions = | |
| { stopPropagation = False, preventDefault = True } | |
| filterKey ( code, shift ) = | |
| if (isEnter code) && (not shift) then | |
| Ok "preventing default" | |
| else | |
| Err "allowing default" | |
| decoder = | |
| JD.customDecoder keyExtractor filterKey | |
| |> JD.map (always NoOp) | |
| in | |
| onWithOptions "keypress" eventOptions decoder |
This file contains hidden or 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
| onEnter : msg -> Attribute msg | |
| onEnter msg = | |
| let | |
| decoder = | |
| keyExtractor |> JD.andThen (filterEventKey msg) | |
| in | |
| on "keydown" decoder | |
| isEnter : Int -> Bool | |
| isEnter code = | |
| code == 13 | |
| keyExtractor : JD.Decoder ( Int, Bool ) | |
| keyExtractor = | |
| JD.map2 (,) | |
| (field "keyCode" JD.int) | |
| (field "shiftKey" JD.bool) | |
| preventDefaultOnEnter : msg -> Attribute msg | |
| preventDefaultOnEnter msg = | |
| let | |
| eventOptions = | |
| { stopPropagation = False, preventDefault = True } | |
| decoder = | |
| keyExtractor |> JD.andThen (filterEventKey msg) | |
| in | |
| onWithOptions "keypress" eventOptions decoder | |
| filterEventKey msg ( code, shift ) = | |
| if (isEnter code) && (not shift) then | |
| JD.succeed msg | |
| else | |
| JD.fail "not triggering" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment