Created
March 8, 2014 18:50
-
-
Save aforemny/9436938 to your computer and use it in GitHub Desktop.
Multiple independent input fields based on the input of a single field
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
| -- This is compiles against 0eae99a737618aaa4d9c7fbd709cf7d6441ddee8. | |
| module DynamicInputFields where | |
| import Graphics.Input (..) | |
| import Graphics.Input.Field (..) | |
| import String | |
| import Dict ( Dict ) | |
| import Dict | |
| -- Create an input field that keeps track of the number of dynamic fields. | |
| inp : Input Content | |
| inp = | |
| input noContent | |
| inputField : Signal Element | |
| inputField = | |
| lift (field def inp.handle id "how many fields do you want?") inp.signal | |
| count : Signal Int | |
| count = lift (maybe 0 id . String.toInt . .string) inp.signal | |
| -- Create a number of input fields with individual contents. | |
| inputs : Input (Int, Content) | |
| inputs = input (0, noContent) | |
| contents : Signal (Dict Int Content) | |
| contents = | |
| let | |
| step : (Int, Content) -> Dict Int Content -> Dict Int Content | |
| step (k, c) d = | |
| Dict.insert k c d | |
| base : Dict Int Content | |
| base = | |
| Dict.empty | |
| in foldp step base inputs.signal | |
| fields : Signal [Element] | |
| fields = | |
| let | |
| nthContent : Int -> Dict Int Content -> Content | |
| nthContent n d = | |
| maybe noContent id (Dict.lookup n d) | |
| nthField : Int -> Content -> Element | |
| nthField n c = | |
| field def inputs.handle ((,) n) (show n) c | |
| in lift2 (\n cs -> map (\k -> nthField k (nthContent k cs)) [1..n]) count contents | |
| -- Put it together | |
| main = lift2 above inputField (lift (flow down) fields) | |
| def : Style | |
| def = defaultStyle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment