Last active
November 8, 2017 06:51
-
-
Save cathylill/92963cfed3c4454b5c9cfe080addb877 to your computer and use it in GitHub Desktop.
Add items to a list
This file contains 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 OverloadedStrings #-} | |
{-# LANGUAGE RecursiveDo #-} | |
import Control.Lens | |
import Data.Text (Text) | |
import qualified Data.Text as Text | |
import Data.Map (Map) | |
import qualified Data.Map as Map | |
import Data.Set (Set) | |
import qualified Data.Set as Set | |
import Reflex.Collection | |
import Reflex.Time | |
import Reflex.Dom | |
main :: IO () | |
main = mdo | |
mainWidget $ do | |
el "h1" $ text "Reflex FRP Autocomplete Example - Canva Frontend Meetup" | |
el "h2" $ text "Part 3 - Adding items to a list" | |
el "section" $ do | |
eAdd <- addItem | |
dMap <- itemsMap eAdd | |
pure () | |
addItem :: MonadWidget t m => m (Event t Text) | |
addItem = mdo | |
addItemText <- textInput $ def | |
& setValue .~ ("" <$ eAddItemClick) | |
eAddItemClick <- button "Add" | |
let eAtClick = Text.strip <$> current (value addItemText) <@ eAddItemClick | |
pure eAtClick | |
itemsMap :: MonadWidget t m => Event t Text -> m (Dynamic t (Map Int Text)) | |
itemsMap eAdd = do | |
dCount <- count eAdd | |
dMap <- foldDyn ($) Map.empty $ | |
Map.insert <$> current dCount <@> eAdd | |
_ <- el "ul" . list dMap $ | |
el "li" . dynText | |
pure dMap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment