2017-04-22 74 views
2

我在尝试侦听Material.Textfield组件中的onEnter事件时遇到了问题。我想我应该使用Options.on和Decoder来实现它,但我不确定如何实现解码器。任何帮助赞赏在elm-mdl中监听onEnter事件Textfield

[ Card.actions [] 
     [ 
     Textfield.render Mdl [ 1 ] mdl [ Options.on "keydown" someDecoder, Options.onInput ChatInput] [] 
     ] 
    ] 

回答

4

使用Material.Options.on创建自定义事件处理程序

import Html.Events exposing (keyCode) 
import Json.Decode as JD 
import Material.Options as Options 


Textfield.render Mdl [ 1 ] mdl [ Options.on "keydown" (JD.andThen isEnter keyCode) ] [] 


isEnter : number -> JD.Decoder Msg 
isEnter code = 
    if code == 13 then 
     JD.succeed SendMsg 
    else 
     JD.fail "not Enter" 
解决它