Skip to content

Instantly share code, notes, and snippets.

@HeinrichApfelmus
Created September 9, 2013 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save HeinrichApfelmus/6495734 to your computer and use it in GitHub Desktop.
Save HeinrichApfelmus/6495734 to your computer and use it in GitHub Desktop.
Implementation of an input element adhering to the three principles laid out in http://apfelmus.nfshost.com/blog/2012/03/29-frp-three-principles-bidirectional-gui.html
{-# LANGUAGE RecordWildCards #-}
import Data.Monoid
import Control.Monad
import qualified Graphics.UI.Threepenny as UI
import Graphics.UI.Threepenny
data Input = Input
{ iElement :: Element
, iUserValue :: Event String
}
newInput :: Behavior String -> IO Input
newInput bValue = do
iElement <- UI.input
bEditing <- stepper False $ and <$>
unions [True <$ UI.domEvent "focus" iElement, False <$ UI.blur iElement]
onChange bValue $ \s -> do
editing <- currentValue bEditing
when (not editing) $ void $ element iElement # set text s
let iUserValue = valueChange iElement
return $ Input {..}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment