F#
HTML
Result
Hosted on
Try WebSharper
<
>
namespace Repro open WebSharper open WebSharper.UI.Next open WebSharper.UI.Next.Html open WebSharper.UI.Next.Client [<JavaScript>] module Lensing = // Input loses focus: // ----------------- // let aliases = // ListModel.Create id [ "Bill"; "Joe" ] // let lensIntoAlias aliasKey = // aliases.LensInto id (fun a n -> n) aliasKey // let Main = // div [ // aliases.View // |> Doc.BindSeqCached (fun (aliasKey: string) -> Doc.Input [] (lensIntoAlias aliasKey)) // aliases.View // |> Doc.BindSeqCached (fun (aliasKey: string) -> div [ text aliasKey ]) // ] // |> Doc.RunById "main" // Workaround 1: // ------------ type Alias = { Key: int; Value: string } let aliases = ListModel.Create (fun a -> a.Key) [ { Key = 1; Value = "Bill" }; { Key = 2; Value = "Joe" } ] let lensIntoAlias aliasKey = aliases.LensInto (fun a -> a.Value) (fun a n -> { a with Value = n }) aliasKey let Main = div [ aliases.Value |> Seq.map (fun al -> Doc.Input [] (lensIntoAlias al.Key)) |> Seq.cast |> Doc.Concat aliases.View |> Doc.BindSeqCached (fun al -> div [ text al.Value ]) ] |> Doc.RunById "main" // Workaround 2: ListModel number of element changes // ------------ // type Alias = { Key: int; Value: string } // let aliases = // ListModel.Create (fun a -> a.Key) [ { Key = 1; Value = "Bill" }; { Key = 2; Value = "Joe" } ] // let lensIntoAlias aliasKey = // aliases.LensInto (fun a -> a.Value) (fun a n -> { a with Value = n }) aliasKey // let trigger = // Var.Create () // let Main = // div // [ // Doc.Button // "Add alias" // [ attr.style "display: block" ] // (fun() -> // aliases.Add({ Key = aliases.Length + 1; Value = "New" }) // // trigger update here // trigger.Value <- ()) // aliases.View // |> View.SnapshotOn aliases.Value trigger.View // |> Doc.BindSeqCached (fun al -> Doc.Input [] (lensIntoAlias al.Key)) // aliases.View // |> Doc.BindSeqCached (fun al -> div [ text al.Value ]) // ] // |> Doc.RunById "main"
<html> <head> <title></title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" /> </head> <body> <div id="main"></div> <!--[BODY]--> </body> </html>