1 | module Text.HTML.Select
10 | data SelectEntry : (t : Type) -> Type where
11 | Title : String -> SelectEntry t
12 | Entry : (val : t) -> String -> SelectEntry t
18 | ix : List (SelectEntry t) -> Nat -> Maybe t
20 | ix (Entry v _ :: xs) 0 = Just v
21 | ix (Title s :: xs) 0 = Nothing
22 | ix (x :: xs) (S k) = ix xs k
24 | app : {0 a : _} -> SnocList (a,List b) -> SnocList b -> a -> SnocList (a,List b)
26 | app sx sy x = sx :< (x, sy <>> [])
29 | SnocList (String,List (Nat,t,String))
30 | -> SnocList (Nat,t,String)
33 | -> List (SelectEntry t)
34 | -> List (String,List (Nat,t,String))
35 | groups sg sp l n [] = app sg sp l <>> []
36 | groups sg sp l n (Title s :: xs) = groups (app sg sp l) [<] s (S n) xs
37 | groups sg sp l n (Entry v s :: xs) = groups sg (sp :< (n,v,s)) l (S n) xs
41 | (entries : List (SelectEntry t))
42 | -> (sel : t -> Bool)
43 | -> (toEvent : t -> e)
44 | -> (attrs : List (Attribute Select e))
46 | selectEntries es sel toEv attrs =
51 | (attrs ++ [onChangeMaybe (map toEv . ix es . cast)])
52 | (groups [<] [<] "" 0 es >>= grp)
54 | opt : (Nat,t,String) -> Node e
55 | opt (x,v,s) = option [value (show x), selected (sel v)] [Text s]
57 | grp : (String,List (Nat,t,String)) -> List (Node e)
58 | grp ("",ps) = map opt ps
59 | grp (s,ps) = [optgroup [label s] $
map opt ps]
72 | -> (sel : t -> Bool)
73 | -> (display : t -> String)
74 | -> (toEvent : t -> e)
75 | -> (attrs : List (Attribute Select e))
77 | selectFromListBy vs sel f = selectEntries ((\x => Entry x $
f x) <$> vs) sel
90 | -> (values : List t)
92 | -> (display : t -> String)
93 | -> (toEvent : t -> e)
94 | -> (attrs : List (Attribute Select e))
96 | selectFromList vs i = selectFromListBy vs ((i ==) . Just)