我希望创建一个按钮,按下它后,表单会增加一个新的输入框或文本域。
(btn, clicked) = Input.button "Click me!"
-- Count how often the clicked signal is true
clicks = countIf id clicked
main = lift2 flow (constant down) $ lift nInputs clicks
nInputs n =
let helper n acc =
if n<=0 then btn : acc
else
-- Input.textField returns a pair containing the field as well as a signal
-- that you can use to access the field's contents. Since I don't actually
-- ever do anything with the contents, I just ignore the signal here.
-- In your real code, you'd probably want to keep the signal around as well.
let (field, _) = Input.textField $ "input " ++ (show n)
in helper (n-1) $ field : acc
in helper n []
仅使用count
将计算信号更改的次数。由于每次点击都会导致信号的值从true更改为false,然后再次更改回来,因此每次点击会计算2次更改。通过使用countIf id
,我们只计算信号为true的次数,因此计算点击次数。