Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Elmish.SimpleInput
- (**
- Minimal application showing how to use Elmish
- You can find more info about Emish architecture and samples at https://elmish.github.io/
- *)
- open Fable.Core.JsInterop
- open Fable.React
- open Fable.React.Props
- open Elmish
- open Elmish.React
- type Ident = string
- type Devotion = {minText:string;maxText:string;id:Ident}
- // MODEL
- type DevotionAffect =
- | Modifier of id:Ident*text:string
- | ADevotion of Devotion
- let getDevotionId =
- function
- | Modifier(ident,_) -> ident
- | ADevotion x -> x.id
- type Model =
- { MaxAll:bool;Values: DevotionAffect list;Checked:Ident Set}
- type Msg =
- | DevotionCheckChange of Ident
- let init () =
- { Values = [
- Modifier ("max","Max Devotions")
- ADevotion {id="crBlue";minText="CrossRoads Blue +18 def";maxText="CrossRoads Blue +24 def"}
- ]
- MaxAll=false
- Checked=Set.empty
- }, Cmd.none
- // UPDATE
- let update (msg:Msg) (model:Model) =
- match msg with
- | DevotionCheckChange ident ->
- printfn "Got a check from %s" ident
- let chk =
- if Set.contains ident model.Checked then
- Set.remove ident model.Checked
- else Set.add ident model.Checked
- {model with
- Checked = chk
- MaxAll= chk.Contains "max"
- }, Cmd.none
- let inline getTgtId (e:Browser.Types.Event) = e.target?id
- // VIEW (rendered with React)
- let labeledChk ident textStyle text isChecked onChange =
- div [] [
- input[Type "checkbox";Id ident;Checked isChecked;OnChange (getTgtId >> onChange)]
- span [Style textStyle][str text]
- ]
- let devChk dispatch isMax =
- function
- | ADevotion x,isChecked ->
- labeledChk x.id
- (if isChecked then [FontStyle "italic"] else List.empty)
- (if isMax then x.maxText else x.minText)
- isChecked
- (fun _ -> Msg.DevotionCheckChange x.id |> dispatch)
- | Modifier (id,text),isChecked ->
- labeledChk id
- List.empty
- text
- isChecked
- (fun _ -> Msg.DevotionCheckChange id |> dispatch)
- let view (model:Model) dispatch =
- div [ Class "main-container" ][
- yield span [][ str "test"]
- yield! model.Values |> Seq.map (fun x ->
- devChk dispatch model.MaxAll (x,getDevotionId x |> model.Checked.Contains ))
- ]
- printfn "starting up"
- // App
- Program.mkProgram init update view
- |> Program.withReact "elmish-app"
- |> Program.run
- // App
- Program.mkProgram init update view
- |> Program.withConsoleTrace
- |> Program.withReactSynchronous "elmish-app"
- |> Program.run
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement