@@ -7,6 +7,7 @@ import Prelude
77import CSS (border , borderRadius , color , em , float , floatLeft , fontWeight , lineHeight , marginBottom , marginLeft , paddingBottom , paddingLeft , paddingRight , paddingTop , pct , px , rgb , solid , weight , width )
88import Data.Maybe (Maybe (..), maybe )
99import Data.Newtype (wrap )
10+ import Docs.Search.URIHash as URIHash
1011import Effect (Effect )
1112import Effect.Aff (Aff )
1213import Halogen as H
@@ -38,28 +39,45 @@ data Action
3839 | InitKeyboardListener
3940 | HandleKey H.SubscriptionId KeyboardEvent
4041
42+ data Query a
43+ = ReadURIHash a
44+
4145data SearchFieldMessage
4246 = InputUpdated String
4347 | InputCleared
4448 | Focused
4549 | LostFocus
4650
47- component :: forall q i . H.Component HH.HTML q i SearchFieldMessage Aff
51+ component :: forall i . H.Component HH.HTML Query i SearchFieldMessage Aff
4852component =
4953 H .mkComponent
5054 { initialState
5155 , render
5256 , eval: H .mkEval $ H .defaultEval { handleAction = handleAction
57+ , handleQuery = handleQuery
5358 , initialize = Just InitKeyboardListener }
5459 }
5560
61+ handleQuery
62+ :: forall a
63+ . Query a
64+ -> H.HalogenM State Action () SearchFieldMessage Aff (Maybe a )
65+ handleQuery (ReadURIHash next) = do
66+ state <- H .get
67+ H .raise (InputUpdated state.input)
68+ pure Nothing
69+
5670initialState :: forall i . i -> State
5771initialState _ = { input: " " , focused: false }
5872
5973handleAction :: Action -> H.HalogenM State Action () SearchFieldMessage Aff Unit
6074handleAction = case _ of
6175
6276 InitKeyboardListener -> do
77+
78+ input <- H .liftEffect URIHash .getInput
79+ H .modify_ (_ { input = input })
80+
6381 document <- H .liftEffect $ Web .document =<< Web .window
6482 H .subscribe' \sid ->
6583 ES .eventListenerEventSource
@@ -82,9 +100,7 @@ handleAction = case _ of
82100 then do
83101 H .liftEffect do
84102 withSearchField (HTMLInputElement .toHTMLElement >>> Web .blur)
85- else do
86- H .modify_ (_ { input = " " })
87- H .raise $ InputCleared
103+ else clearInput
88104
89105 InputAction input -> do
90106 H .modify_ $ (_ { input = input })
@@ -93,6 +109,7 @@ handleAction = case _ of
93109 state <- H .get
94110 H .liftEffect do
95111 withSearchField (HTMLInputElement .toHTMLElement >>> Web .blur)
112+ H .liftEffect (URIHash .setInput state.input)
96113 H .raise $ InputUpdated state.input
97114
98115 FocusChanged status -> do
@@ -102,6 +119,12 @@ handleAction = case _ of
102119 then Focused
103120 else LostFocus
104121
122+ clearInput :: H.HalogenM State Action () SearchFieldMessage Aff Unit
123+ clearInput = do
124+ H .modify_ (_ { input = " " })
125+ H .liftEffect URIHash .removeHash
126+ H .raise InputCleared
127+
105128withSearchField :: (HTML.HTMLInputElement -> Effect Unit ) -> Effect Unit
106129withSearchField cont = do
107130 doc <- Document .toParentNode <$>
0 commit comments