2017-04-22 44 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" 
解決它