所以我有一个简单的示例布局,其中包含一个列表框、一个按钮和一个 textarea,单击按钮会更改 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"
我想要做的是让更改取决于列表框选择(例如,有"new text"
或"First"
基于"Second"
选择)。
我可以很容易地通过组合userSelection
和facts
作为选择
facts . userSelection :: ListBox a -> Behavior (Maybe a)
但是因为设置 textarea 的值是用
set text :: String -> UI Element -> UI Element
我不知道如何解决选择是Behavior
.
这一切对我来说似乎有点生疏,我想知道这样做的正确方法是什么。也许我应该在列表框选择完成或更改时已经做一些事情,而不仅仅是在按下按钮时。