Skip to content

Commit 0435800

Browse files
committed
[SimplePrimitives] Optimize checkbox
1 parent b2e0de0 commit 0435800

File tree

2 files changed

+22
-34
lines changed

2 files changed

+22
-34
lines changed

src/Aardvark.UI.Primitives/Primitives/SimplePrimitives.fs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ open Aardvark.UI.Generic
99
module SimplePrimitives =
1010
open System
1111

12-
13-
let private bootCheckBox =
14-
String.concat "" [
15-
"$('#__ID__').checkbox().checkbox('__INITIALSTATE__');"
16-
"$('#__ID__').get(0).addEventListener('click', function(e) { aardvark.processEvent('__ID__', 'onclick'); e.stopPropagation(); }, true);"
17-
"isChecked.onmessage = function(s) { if (s) { $('#__ID__').checkbox('check'); } else { $('#__ID__').checkbox('uncheck'); } };"
18-
]
19-
2012
module internal Html =
2113

2214
let semui =
@@ -200,37 +192,33 @@ module SimplePrimitives =
200192
None
201193

202194
let checkbox (atts : AttributeMap<'msg>) (state : aval<bool>) (toggle : 'msg) (l : list<DomNode<'msg>>) =
203-
let state = if state.IsConstant then AVal.custom (fun t -> state.GetValue t) else state
204-
205-
let ev =
206-
{
207-
clientSide = fun _ _ -> ""
208-
serverSide = fun _ _ _ -> Seq.singleton toggle
209-
}
210-
211-
let boot = bootCheckBox.Replace("__INITIALSTATE__", if state.GetValue() then "check" else "uncheck")
195+
let boot =
196+
String.concat "" [
197+
"const $self = $('#__ID__');"
198+
"$self.checkbox();"
199+
"$self[0].addEventListener('click', function(e) { aardvark.processEvent('__ID__', 'onclick'); e.stopPropagation(); }, true);"
200+
"isChecked.onmessage = function(s) { const behavior = s ? 'set checked' : 'set unchecked'; $self.checkbox(behavior); };"
201+
]
212202

213203
let myAtts =
214204
AttributeMap.ofList [
215-
"onclick", AttributeValue.Event ev
216-
"class", AttributeValue.String "ui checkbox"
205+
clazz "ui checkbox"
206+
onClick (fun _ -> toggle)
217207
]
218208

219209
let atts = AttributeMap.union atts myAtts
220210
require Html.semui (
221211
onBoot' ["isChecked", AVal.channel state] boot (
222-
Incremental.div atts (
223-
alist {
224-
yield input [attribute "type" "checkbox"]
225-
match l with
226-
| [l] ->
227-
match l.NodeTag with
228-
| Some "label" -> yield l
229-
| _ -> yield label [] [l]
230-
| _ ->
231-
yield label [] l
232-
}
233-
)
212+
Incremental.div atts <| AList.ofList [
213+
yield input [attribute "type" "checkbox"]
214+
match l with
215+
| [l] ->
216+
match l.NodeTag with
217+
| Some "label" -> yield l
218+
| _ -> yield label [] [l]
219+
| _ ->
220+
yield label [] l
221+
]
234222
)
235223
)
236224

src/Examples (dotnetcore)/23 - Inputs/App.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,12 @@ let view (model : AdaptiveModel) =
8383
]
8484
]
8585

86-
accordionSimple false [ clazz "inverted item" ] [
86+
accordionSimple true [ clazz "inverted item" ] [
8787
// Checkboxes
8888
"Checkboxes", div [ clazz "menu" ] [
8989
div [ clazz "item" ] [
9090
simplecheckbox {
91-
attributes [clazz "ui inverted checkbox"]
91+
attributes [clazz "inverted"]
9292
state model.active
9393
toggle ToggleActive
9494
content [ text "Is the thing active?"; i [clazz "icon rocket" ] [] ]
@@ -97,7 +97,7 @@ let view (model : AdaptiveModel) =
9797
]
9898

9999
div [ clazz "item" ] [
100-
checkbox [clazz "ui inverted toggle checkbox"] model.active ToggleActive "Is the thing active?"
100+
checkbox [clazz "inverted toggle"] model.active ToggleActive "Is the thing active?"
101101
]
102102
]
103103

0 commit comments

Comments
 (0)