Created
September 9, 2013 13:41
-
-
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
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
{-# 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