Skip to content

Commit

Permalink
Support HSX expressions like <input value={project.id}/>
Browse files Browse the repository at this point in the history
It's now possible to pass IDs directly to HSX attributes. Previously it required calling `inptuValue` explicit before.

Old:
```haskell
<input value={inputValue project.id}/>
```

New:
```haskell
<input value={project.id}/>
```
  • Loading branch information
mpscholten committed Nov 8, 2023
1 parent 411f3a5 commit 46a9606
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 4 additions & 1 deletion IHP/ViewSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import qualified IHP.View.CSSFramework as CSSFramework ()
import IHP.View.Types
import qualified IHP.FrameworkConfig as FrameworkConfig
import IHP.Controller.Context

import qualified IHP.HSX.Attribute as HSX

class View theView where
-- | Hook which is called before the render is called
Expand Down Expand Up @@ -262,3 +262,6 @@ liveReloadWebsocketUrl :: (?context :: ControllerContext) => Text
liveReloadWebsocketUrl = ?context.frameworkConfig.ideBaseUrl
|> Text.replace "http://" "ws://"
|> Text.replace "https://" "wss://"

instance InputValue (PrimaryKey table) => HSX.ApplyAttribute (Id' table) where
applyAttribute attr attr' value h = HSX.applyAttribute attr attr' (inputValue value) h
12 changes: 11 additions & 1 deletion Test/ViewSupportSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,14 @@ tests = beforeAll (mockContextNoDatabase WebApplication config) do
runSession (testGet "test/TestWithParam?param=foo") application >>= assertTextExists "isActiveController TestController: True"
it "should return False on a different route" $ withContext do
application <- makeApplication
runSession (testGet "test/TestWithParam?param=foo") application >>= assertTextExists "isActiveController AnotherTestAction: False"
runSession (testGet "test/TestWithParam?param=foo") application >>= assertTextExists "isActiveController AnotherTestAction: False"

describe "HSX" $ do
it "allow using Id's in HSX attributes without explicitly calling inputValue" $ withContext do
let
id :: Id' "users"
id = Id ("70a10b53-a776-470a-91a8-900cdda06aa2" :: UUID)

(ClassyPrelude.tshow [hsx|<input value={id} />|]) `shouldBe` "<input value=\"70a10b53-a776-470a-91a8-900cdda06aa2\"/>"

type instance PrimaryKey "users" = UUID

0 comments on commit 46a9606

Please sign in to comment.