on develop help sign in
Set value of input elements in static renderer
181cf77Tom Smalley
reflex-dom-core/src/Reflex/Dom/Builder/Static.hs+16−3
@@ -311,7 +311,14 @@ instance SupportsStaticDomBuilder t m => DomBuilder t (StaticDomBuilderT t m) wh
311 return (e, result)
311 return (e, result)
312 {-# INLINABLE inputElement #-}
312 {-# INLINABLE inputElement #-}
313 inputElement cfg = do
313 inputElement cfg = do
314+ -- Tweak the config to update the "value" attribute appropriately.
315+ -- TODO: warn upon overwriting values.
316+ let adjustedConfig = (_inputElementConfig_elementConfig cfg)
317+ & elementConfig_initialAttributes %~ Map.insert "value" (_inputElementConfig_initialValue cfg)
318+ & elementConfig_modifyAttributes %~ (case _inputElementConfig_setValue cfg of
319+ Nothing -> id
320+ Just e -> \e' -> (Map.singleton "value" . Just <$> e) <> e')
314− (e, _result) <- element "input" (cfg ^. inputElementConfig_elementConfig) $ return ()
321+ (e, _result) <- element "input" adjustedConfig $ return ()
315 let v0 = constDyn $ cfg ^. inputElementConfig_initialValue
322 let v0 = constDyn $ cfg ^. inputElementConfig_initialValue
316 let c0 = constDyn $ cfg ^. inputElementConfig_initialChecked
323 let c0 = constDyn $ cfg ^. inputElementConfig_initialChecked
317 let hasFocus = constDyn False -- TODO should this be coming from initialAtttributes
324 let hasFocus = constDyn False -- TODO should this be coming from initialAtttributes
@@ -327,8 +334,14 @@ instance SupportsStaticDomBuilder t m => DomBuilder t (StaticDomBuilderT t m) wh
327 }
334 }
328 {-# INLINABLE textAreaElement #-}
335 {-# INLINABLE textAreaElement #-}
329 textAreaElement cfg = do
336 textAreaElement cfg = do
330− --TODO: Support setValue event
337+ -- Tweak the config to update the "value" attribute appropriately.
338+ -- TODO: warn upon overwriting values.
339+ let adjustedConfig = (_textAreaElementConfig_elementConfig cfg)
340+ & elementConfig_initialAttributes %~ Map.insert "value" (_textAreaElementConfig_initialValue cfg)
341+ & elementConfig_modifyAttributes %~ (case _textAreaElementConfig_setValue cfg of
342+ Nothing -> id
343+ Just e -> \e' -> (Map.singleton "value" . Just <$> e) <> e')
331− (e, _domElement) <- element "textarea" (cfg ^. textAreaElementConfig_elementConfig) $ return ()
344+ (e, _domElement) <- element "textarea" adjustedConfig $ return ()
332 let v0 = constDyn $ cfg ^. textAreaElementConfig_initialValue
345 let v0 = constDyn $ cfg ^. textAreaElementConfig_initialValue
333 let hasFocus = constDyn False -- TODO should this be coming from initialAtttributes
346 let hasFocus = constDyn False -- TODO should this be coming from initialAtttributes
334 return $ TextAreaElement
347 return $ TextAreaElement
reflex-dom-core/test/hydration.hs+46
@@ -290,6 +290,29 @@ tests withDebugging wdConfig caps _selenium = do
290 return ()
290 return ()
291
291
292 describe "inputElement" $ do
292 describe "inputElement" $ do
293+ describe "static renderer" $ session' $ do
294+ it "sets value attribute" $ runWD $ do
295+ let checkStatic = do
296+ e <- findElemWithRetry $ WD.ByTag "input"
297+ WD.attr e "value" `shouldBeWithRetryM` Just "test"
298+ pure e
299+ checkHydrated e = do
300+ WD.attr e "value" `shouldBeWithRetryM` Just "test"
301+ testWidget' checkStatic checkHydrated $ void $ inputElement $ def
302+ & inputElementConfig_initialValue .~ "test"
303+ it "updates value attribute at postBuild" $ runWD $ do
304+ let checkStatic = do
305+ e <- findElemWithRetry $ WD.ByTag "input"
306+ WD.attr e "value" `shouldBeWithRetryM` Just "test-updated"
307+ pure e
308+ checkHydrated e = do
309+ WD.attr e "value" `shouldBeWithRetryM` Just "test-updated"
310+ testWidget' checkStatic checkHydrated $ do
311+ pb <- getPostBuild
312+ _ <- inputElement $ def
313+ & inputElementConfig_initialValue .~ "test"
314+ & inputElementConfig_setValue .~ ("test-updated" <$ pb)
315+ pure ()
293 describe "hydration" $ session' $ do
316 describe "hydration" $ session' $ do
294 it "doesn't wipe user input when switching over" $ runWD $ do
317 it "doesn't wipe user input when switching over" $ runWD $ do
295 inputRef <- newRef ("" :: Text)
318 inputRef <- newRef ("" :: Text)
@@ -475,6 +498,29 @@ tests withDebugging wdConfig caps _selenium = do
475 liftIO $ writeRef filesRef names
498 liftIO $ writeRef filesRef names
476
499
477 describe "textAreaElement" $ do
500 describe "textAreaElement" $ do
501+ describe "static renderer" $ session' $ do
502+ it "sets value attribute" $ runWD $ do
503+ let checkStatic = do
504+ e <- findElemWithRetry $ WD.ByTag "textarea"
505+ WD.attr e "value" `shouldBeWithRetryM` Just "test"
506+ pure e
507+ checkHydrated e = do
508+ WD.attr e "value" `shouldBeWithRetryM` Just "test"
509+ testWidget' checkStatic checkHydrated $ void $ textAreaElement $ def
510+ & textAreaElementConfig_initialValue .~ "test"
511+ it "updates value attribute at postBuild" $ runWD $ do
512+ let checkStatic = do
513+ e <- findElemWithRetry $ WD.ByTag "textarea"
514+ WD.attr e "value" `shouldBeWithRetryM` Just "test-updated"
515+ pure e
516+ checkHydrated e = do
517+ WD.attr e "value" `shouldBeWithRetryM` Just "test-updated"
518+ testWidget' checkStatic checkHydrated $ do
519+ pb <- getPostBuild
520+ _ <- textAreaElement $ def
521+ & textAreaElementConfig_initialValue .~ "test"
522+ & textAreaElementConfig_setValue .~ ("test-updated" <$ pb)
523+ pure ()
478 describe "hydration" $ session' $ do
524 describe "hydration" $ session' $ do
479 it "doesn't wipe user input when switching over" $ runWD $ do
525 it "doesn't wipe user input when switching over" $ runWD $ do
480 inputRef <- newRef ("" :: Text)
526 inputRef <- newRef ("" :: Text)