So I have a simple example layout with a listbox, a button and a textarea, where clicking the button changes the text in the textarea:
import Control.Applicative
import Control.Monad
import Data.Maybe
import qualified Graphics.UI.Threepenny as UI
import Graphics.UI.Threepenny.Core
main :: IO ()
main = startGUI defaultConfig setup
setup :: Window -> UI ()
setup w = do
return w # set UI.title "Simple example"
listBox <- UI.listBox (pure ["First", "Second"]) (pure Nothing) ((UI.string .) <$> (pure id))
button <- UI.button # set UI.text "Button"
display <- UI.textarea # set UI.text "Initial value"
element listBox # set (attr "size") "10"
getBody w #+ [element listBox, element button, element display]
on UI.click button $ const $ do
element display # set UI.text "new text"
What I wanted to do is have the change be dependent on the listbox selection (for example have the "new text"
be "First"
or "Second"
based on the selection).
I can quite easily get the selection by combining userSelection
and facts
as
facts . userSelection :: ListBox a -> Behavior (Maybe a)
but because setting the value for the textarea is done with
set text :: String -> UI Element -> UI Element
I don't know how to work around the fact that the selection is a Behavior
.
All this seems a bit unidiomatic to me and I was wondering what would be the correct way to do this. Maybe I should do something already when the listbox selection is done or changed and not only when the button is pressed.