on develop help sign in
Set value of input elements in static renderer
commit181cf7779d686aabc7579b43c75fee10b2e7145e authorTom Smalley <tfsmalley@gmail.com> dateWed 8 Jul 2020, 14:56 (6 years ago) parent9c696a0
2 files changed+62−3
reflex-dom-core/src/Reflex/Dom/Builder/Static.hs+16−3 reflex-dom-core/test/hydration.hs+46
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
311311 return (e, result)
312312 {-# INLINABLE inputElement #-}
313313 inputElement cfg = do
314− (e, _result) <- element "input" (cfg ^. inputElementConfig_elementConfig) $ return ()
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')
321+ (e, _result) <- element "input" adjustedConfig $ return ()
315322 let v0 = constDyn $ cfg ^. inputElementConfig_initialValue
316323 let c0 = constDyn $ cfg ^. inputElementConfig_initialChecked
317324 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
327334 }
328335 {-# INLINABLE textAreaElement #-}
329336 textAreaElement cfg = do
330− --TODO: Support setValue event
331− (e, _domElement) <- element "textarea" (cfg ^. textAreaElementConfig_elementConfig) $ return ()
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')
344+ (e, _domElement) <- element "textarea" adjustedConfig $ return ()
332345 let v0 = constDyn $ cfg ^. textAreaElementConfig_initialValue
333346 let hasFocus = constDyn False -- TODO should this be coming from initialAtttributes
334347 return $ TextAreaElement
reflex-dom-core/test/hydration.hs+46
@@ -290,6 +290,29 @@ tests withDebugging wdConfig caps _selenium = do
290290 return ()
291291
292292 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 ()
293316 describe "hydration" $ session' $ do
294317 it "doesn't wipe user input when switching over" $ runWD $ do
295318 inputRef <- newRef ("" :: Text)
@@ -475,6 +498,29 @@ tests withDebugging wdConfig caps _selenium = do
475498 liftIO $ writeRef filesRef names
476499
477500 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 ()
478524 describe "hydration" $ session' $ do
479525 it "doesn't wipe user input when switching over" $ runWD $ do
480526 inputRef <- newRef ("" :: Text)