在elm-mdl Textfield中收听onEnter事件

par*_*i82 2 elm elm-mdl

我正在尝试在Material.Textfield组件中侦听onEnter事件时遇到问题.我想我应该使用Options.on和Decoder来实现它,但我不知道如何实现解码器.任何帮助赞赏

  [ Card.actions []
      [ 
         Textfield.render Mdl [ 1 ] mdl [ Options.on "keydown" someDecoder, Options.onInput ChatInput] []
      ]
   ]
Run Code Online (Sandbox Code Playgroud)

par*_*i82 5

通过使用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"
Run Code Online (Sandbox Code Playgroud)