on develop help sign in
Remove Read and Show instance for dropdown keys, improve dropdown setvalue
c335f83Ali Abrar
default.nix+3−3
@@ -1,8 +1,8 @@
1{ mkDerivation, pkgs, dependent-map, ghcjs-dom, lens
1{ mkDerivation, pkgs, dependent-map, ghcjs-dom, lens
2−, mtl, ref-tf, reflex, safe, text, these
2+, mtl, ref-tf, reflex, text, these
3, transformers, data-default, semigroups, aeson
3, transformers, data-default, semigroups, aeson
4, ghc, webkitgtk3-javascriptcore, exception-transformers
4, ghc, webkitgtk3-javascriptcore, exception-transformers
5−, dependent-sum-template, bifunctors, raw-strings-qq
5+, dependent-sum-template, bifunctors, bimap, raw-strings-qq
6}:
6}:
7
7
8mkDerivation {
8mkDerivation {
@@ -17,7 +17,6 @@ mkDerivation {
17 these
17 these
18 lens
18 lens
19 ghcjs-dom
19 ghcjs-dom
20− safe
21 text
20 text
22 data-default
21 data-default
23 semigroups
22 semigroups
@@ -26,6 +25,7 @@ mkDerivation {
26 exception-transformers
25 exception-transformers
27 dependent-sum-template
26 dependent-sum-template
28 bifunctors
27 bifunctors
28+ bimap
29 ] ++ (if (ghc.pname or null) == "ghcjs" then [ ] else [ webkitgtk3-javascriptcore raw-strings-qq ]);
29 ] ++ (if (ghc.pname or null) == "ghcjs" then [ ] else [ webkitgtk3-javascriptcore raw-strings-qq ]);
30 license = null;
30 license = null;
31}
31}
reflex-dom.cabal+2−2
@@ -35,7 +35,6 @@ library
35 ghcjs-dom >= 0.2.1 && < 0.3,
35 ghcjs-dom >= 0.2.1 && < 0.3,
36 transformers == 0.3.* || == 0.4.*,
36 transformers == 0.3.* || == 0.4.*,
37 lens >= 4.7 && < 4.14,
37 lens >= 4.7 && < 4.14,
38− safe == 0.3.*,
39 text == 1.2.*,
38 text == 1.2.*,
40 bytestring == 0.10.*,
39 bytestring == 0.10.*,
41 data-default == 0.5.*,
40 data-default == 0.5.*,
@@ -44,7 +43,8 @@ library
44 exception-transformers == 0.4.*,
43 exception-transformers == 0.4.*,
45 directory == 1.2.*,
44 directory == 1.2.*,
46 dependent-sum-template >= 0.0.0.4 && < 0.1,
45 dependent-sum-template >= 0.0.0.4 && < 0.1,
47− bifunctors >= 4.2 && < 6
46+ bifunctors >= 4.2 && < 6,
47+ bimap >= 0.3 && < 0.4
48
48
49 if impl(ghcjs)
49 if impl(ghcjs)
50 hs-source-dirs: src-ghcjs
50 hs-source-dirs: src-ghcjs
src/Reflex/Dom/Widget/Input.hs+23−18
@@ -16,14 +16,16 @@ import GHCJS.DOM.EventM
16import GHCJS.DOM.File
16import GHCJS.DOM.File
17import qualified GHCJS.DOM.FileList as FileList
17import qualified GHCJS.DOM.FileList as FileList
18import Data.Monoid
18import Data.Monoid
19−import Data.Map as Map
19+import qualified Data.Map as Map
20−import Control.Lens
20+import Control.Lens hiding (ix)
21import Control.Monad hiding (forM_)
21import Control.Monad hiding (forM_)
22import Control.Monad.IO.Class
22import Control.Monad.IO.Class
23+import qualified Data.Bimap as B
23import Data.Default
24import Data.Default
24−import Data.Maybe
25−import Safe
26import Data.Dependent.Sum (DSum (..))
25import Data.Dependent.Sum (DSum (..))
26+import Data.Map (Map)
27+import Data.Maybe
28+import Text.Read
27
29
28data TextInput t
30data TextInput t
29 = TextInput { _textInput_value :: Dynamic t String
31 = TextInput { _textInput_value :: Dynamic t String
@@ -191,26 +193,29 @@ instance (Reflex t, Ord k, Show k, Read k) => Default (DropdownConfig t k) where
191 }
193 }
192
194
193--TODO: We should allow the user to specify an ordering instead of relying on the ordering of the Map
195--TODO: We should allow the user to specify an ordering instead of relying on the ordering of the Map
194−--TODO: Get rid of Show k and Read k by indexing the possible values ourselves
195-- | Create a dropdown box
196-- | Create a dropdown box
196-- The first argument gives the initial value of the dropdown; if it is not present in the map of options provided, it will be added with an empty string as its text
197-- The first argument gives the initial value of the dropdown; if it is not present in the map of options provided, it will be added with an empty string as its text
197−dropdown :: forall k t m. (MonadWidget t m, Ord k, Show k, Read k) => k -> Dynamic t (Map k String) -> DropdownConfig t k -> m (Dropdown t k)
198+dropdown :: forall k t m. (MonadWidget t m, Ord k) => k -> Dynamic t (Map k String) -> DropdownConfig t k -> m (Dropdown t k)
198dropdown k0 options (DropdownConfig setK attrs) = do
199dropdown k0 options (DropdownConfig setK attrs) = do
200+ optionsWithAddedKeys <- combineDyn Map.union options <=< foldDyn Map.union (k0 =: "") $ fmap (=: "") setK
201+ defaultKey <- holdDyn k0 setK
202+ (indexedOptions, ixKeys) <- splitDyn <=< forDyn optionsWithAddedKeys $ \os ->
203+ let xs = map (\(ix, (k, v)) -> ((ix, k), ((ix, k), v))) $ zip [0::Int ..] $ Map.toList os
204+ ixVals = Map.fromList $ map snd xs
205+ ixKeys = B.fromList $ map fst xs
206+ in (ixVals, ixKeys)
199− (eRaw, _) <- elDynAttr' "select" attrs $ do
207+ (eRaw, _) <- elDynAttr' "select" attrs $ listWithKey indexedOptions $ \(ix, k) v -> do
200− optionsWithDefault <- mapDyn (`Map.union` (k0 =: "")) options
201− listWithKey optionsWithDefault $ \k v -> do
202− elAttr "option" ("value" =: show k <> if k == k0 then "selected" =: "selected" else mempty) $ dynText v
208+ optionAttrs <- mapDyn (\dk -> "value" =: show ix <> if dk == k then "selected" =: "selected" else mempty) defaultKey
209+ elDynAttr "option" optionAttrs $ dynText v
203 let e = castToHTMLSelectElement $ _el_element eRaw
210 let e = castToHTMLSelectElement $ _el_element eRaw
204− performEvent_ $ fmap (Select.setValue e . Just . show) setK
211+ performEvent_ $ fmap (Select.setValue e . Just . show) $ attachDynWithMaybe (flip B.lookupR) ixKeys setK
205− eChange <- wrapDomEvent e (`on` change) $ do
212+ eChange <- attachDynWith (\ks s -> join $ B.lookup <$> join (readMaybe <$> s) <*> pure ks) ixKeys <$> (wrapDomEvent e (`on` change) $ Select.getValue e)
206− kStr <- fromMaybe "" <$> Select.getValue e
207− return $ readMay kStr
208− let readKey opts mk = fromMaybe k0 $ do
213+ let readKey keys mk = fromMaybe k0 $ do
209 k <- mk
214 k <- mk
210− guard $ Map.member k opts
215+ guard $ B.memberR k keys
211 return k
216 return k
212− dValue <- combineDyn readKey options =<< holdDyn (Just k0) (leftmost [eChange, fmap Just setK])
217+ dValue <- combineDyn readKey ixKeys <=< holdDyn (Just k0) $ leftmost [eChange, fmap Just setK]
213− return $ Dropdown dValue (attachDynWith readKey options eChange)
218+ return $ Dropdown dValue (attachDynWith readKey ixKeys eChange)
214
219
215liftM concat $ mapM makeLenses
220liftM concat $ mapM makeLenses
216 [ ''TextAreaConfig
221 [ ''TextAreaConfig