Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
purescript-gargantext
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Grégoire Locqueville
purescript-gargantext
Commits
5f21c692
Commit
5f21c692
authored
May 14, 2019
by
James Laver
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor SearchField to work over a State Search, add G.Utils.Reactix.useLayoutEffect1'
parent
07196b05
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
83 additions
and
44 deletions
+83
-44
SearchField.purs
src/Gargantext/Components/Search/SearchField.purs
+35
-16
Specs.purs
src/Gargantext/Pages/Layout/Specs.purs
+3
-3
SearchBar.purs
src/Gargantext/Pages/Layout/Specs/SearchBar.purs
+40
-25
Reactix.purs
src/Gargantext/Utils/Reactix.purs
+5
-0
No files found.
src/Gargantext/Components/Search/SearchField.purs
View file @
5f21c692
module Gargantext.Components.Search.SearchField where
module Gargantext.Components.Search.SearchField
( Search, Props, searchField, searchFieldComponent )where
import Prelude hiding (div)
import Prelude hiding (div)
import Data.Map as Map
import Data.Map as Map
...
@@ -21,11 +22,16 @@ import Reactix.SyntheticEvent as E
...
@@ -21,11 +22,16 @@ import Reactix.SyntheticEvent as E
select = R.createElement "select"
select = R.createElement "select"
type Search = { category :: String, term :: String }
defaultSearch :: Search
defaultSearch = { category: "PubMed", term: "" }
type Props =
type Props =
-- list of categories to search, or parsers to use on uploads
-- list of categories to search, or parsers to use on uploads
( categories :: Array String
( categories :: Array String
-- State hook for a search
term
, how we get data in and out
-- State hook for a search, how we get data in and out
,
term :: R.State String
,
search :: R.State (Maybe Search)
)
)
searchField :: Record Props -> R.Element
searchField :: Record Props -> R.Element
...
@@ -38,24 +44,37 @@ searchFieldComponent :: R.Memo Props
...
@@ -38,24 +44,37 @@ searchFieldComponent :: R.Memo Props
searchFieldComponent = R.memo (R.hooksComponent "SearchField" cpt) hasChanged
searchFieldComponent = R.memo (R.hooksComponent "SearchField" cpt) hasChanged
where
where
cpt props _ = do
cpt props _ = do
elemRef <- R.useRef $ null
let search = maybe defaultSearch identity (fst props.search)
cat <- R.useState $ \_ -> pure search.category
term <- R.useState $ \_ -> pure search.term
pure $
pure $
div { className: "search-field" }
div { className: "search-field" }
[
select { className: "category" } (cat <$> props.categories)
[
categoryInput cat props.categories
, searchInput
elemRef props.
term
, searchInput term
, submitButton
elemRef props.term
, submitButton
cat term props.search
]
]
cat name = option { value: name } [text name]
hasChanged p p' = (p.categories /= p'.categories) || (fst p.search /= fst p.search)
hasChanged p p' = (p.categories /= p'.categories) || (fst p.term /= fst p.term)
searchInput :: R.Ref (Nullable DOM.Element) -> R.State String -> R.Element
categoryInput :: R.State String -> Array String -> R.Element
searchInput ref (term /\ setTerm) =
categoryInput (cat /\ setCat) cats =
select { className: "category", onChange } (item <$> cats)
where
onChange = mkEffectFn1 $ \e -> setCat (e .. "target" .. "value")
item name = option { value: name } [ text name ]
searchInput :: R.State String -> R.Element
searchInput (term /\ setTerm) =
input { defaultValue: term
input { defaultValue: term
, type: "text"
, type: "text"
, ref: ref
, onChange
, placeholder: placeholder }
, placeholder }
where onChange = mkEffectFn1 $ \e -> setTerm $ e .. "target" .. "value"
submitButton :: R.
Ref (Nullable DOM.Element) -> R.State String
-> R.Element
submitButton :: R.
State String -> R.State String -> R.State (Maybe Search)
-> R.Element
submitButton
ref (_ /\ setTerm
) = button { onClick: click } [ text "Search" ]
submitButton
(cat /\ _) (term /\ _) (_ /\ setSearch
) = button { onClick: click } [ text "Search" ]
where
where
click = mkEffectFn1 $ \_ -> setTerm $ (R.readRef ref) .. "value"
click = mkEffectFn1 $ \_ -> do
case term of
"" -> setSearch Nothing
_ -> setSearch $ Just { category: cat, term: term }
src/Gargantext/Pages/Layout/Specs.purs
View file @
5f21c692
...
@@ -178,9 +178,9 @@ searchBar = simpleSpec defaultPerformAction render
...
@@ -178,9 +178,9 @@ searchBar = simpleSpec defaultPerformAction render
[ divLogo
[ divLogo
, div [ className "collapse navbar-collapse"
, div [ className "collapse navbar-collapse"
]
]
$ [ divDropdownLeft ]
$
[ divDropdownLeft ]
<> [
R'.scuff (SB.searchBar SB.defaultProps)] <>
<> [
R'.scuff (SB.searchBar SB.defaultProps) ]
[ divDropdownRight d s ]
<>
[ divDropdownRight d s ]
]
]
]
]
]
]
...
...
src/Gargantext/Pages/Layout/Specs/SearchBar.purs
View file @
5f21c692
module Gargantext.Pages.Layout.Specs.SearchBar where
module Gargantext.Pages.Layout.Specs.SearchBar
( Props, defaultProps, searchBar, searchBarComponent
) where
import Prelude
import Prelude
import Data.Maybe (Maybe(..))
import Data.Traversable (traverse_)
import Data.Tuple (fst)
import Data.Tuple (fst)
import Data.Tuple.Nested ( (/\) )
import Data.Tuple.Nested ( (/\) )
import Effect.Uncurried (EffectFn1, mkEffectFn1)
import Effect.Uncurried (EffectFn1, mkEffectFn1)
...
@@ -10,12 +14,17 @@ import DOM.Simple.Console
...
@@ -10,12 +14,17 @@ import DOM.Simple.Console
import Gargantext.Utils.Reactix as R'
import Gargantext.Utils.Reactix as R'
import Reactix.DOM.HTML as H
import Reactix.DOM.HTML as H
import Gargantext.Components.Modals.Modal (modalShow)
import Gargantext.Components.Modals.Modal (modalShow)
import Gargantext.Components.Search.SearchField (searchField)
import Gargantext.Components.Search.SearchField (
Search,
searchField)
type Props = ( open :: Boolean, categories :: Array String )
type Props =
( open :: Boolean
, categories :: Array String )
defaultCategories :: Array String
defaultCategories = ["PubMed", "HAL"]
defaultProps :: Record Props
defaultProps :: Record Props
defaultProps = { open: true, categories:
["PubMed", "HAL"]
}
defaultProps = { open: true, categories:
defaultCategories
}
searchBar :: Record Props -> R.Element
searchBar :: Record Props -> R.Element
searchBar p = R.createElement searchBarComponent p []
searchBar p = R.createElement searchBarComponent p []
...
@@ -25,25 +34,31 @@ searchBarComponent = R.hooksComponent "SearchBar" cpt
...
@@ -25,25 +34,31 @@ searchBarComponent = R.hooksComponent "SearchBar" cpt
where
where
cpt props _ = do
cpt props _ = do
open <- R.useState $ \_ -> pure $ props.open
open <- R.useState $ \_ -> pure $ props.open
term <- R.useState $ \_ -> pure ""
search <- R.useState $ \_ -> pure Nothing
R.useLayoutEffect1 (fst term) $ \_ -> do
onSearchChange search
case (fst term) of
"" -> pure unit
term' -> do
log2 "Searching term: " term'
modalShow "addCorpus"
pure $ \_ -> pure unit
pure $ H.div { className: "search-bar-container" }
pure $ H.div { className: "search-bar-container" }
[ toggleButton open, inner open term props ]
[ toggleButton open
toggleButton :: R.State Boolean -> R.Element
, searchFieldContainer open search props.categories ]
toggleButton open =
H.button {onClick: onClickToggleExpanded open, className: "search-bar-toggle"}
searchFieldContainer :: R.State Boolean -> R.State (Maybe Search) -> Array String -> R.Element
[ H.i { className: "material-icons md-36", style: { marginTop: "-5px" } }
searchFieldContainer (true /\ _) search categories =
[ H.text "control_point" ] ]
H.div { className: "search-bar open" } [ searchField { categories, search } ]
inner :: R.State Boolean -> R.State String -> Record Props -> R.Element
searchFieldContainer (false /\ _) _ _ = H.div {className: "search-bar closed"} []
inner (true /\ _) term props = H.div {className: "search-bar open"}
[ searchField { categories: props.categories, term: term } ]
onSearchChange :: R.State (Maybe Search) -> R.Hooks Unit
inner (false /\ _) _ _ = H.div {className: "search-bar closed"} []
onSearchChange (search /\ setSearch) =
R'.useLayoutEffect1' search $ \_ -> traverse_ triggerSearch search
onClickToggleExpanded :: forall e. R.State Boolean -> EffectFn1 e Unit
where
onClickToggleExpanded open = mkEffectFn1 $ \_ -> R'.overState not open
triggerSearch {term, category} = do
log4 "Searching term: " term " in cat:" category
modalShow "addCorpus"
toggleButton :: R.State Boolean -> R.Element
toggleButton open =
H.button { onClick: onToggleExpanded open, className: "search-bar-toggle" }
[ H.i { className: "material-icons md-36"
, style: { marginTop: "-5px", color: "#000" } }
[ H.text "control_point" ] ]
onToggleExpanded :: forall e. R.State Boolean -> EffectFn1 e Unit
onToggleExpanded open = mkEffectFn1 $ \_ -> R'.overState not open
src/Gargantext/Utils/Reactix.purs
View file @
5f21c692
...
@@ -35,3 +35,8 @@ named = flip $ defineProperty "name"
...
@@ -35,3 +35,8 @@ named = flip $ defineProperty "name"
overState :: forall t. (t -> t) -> R.State t -> Effect Unit
overState :: forall t. (t -> t) -> R.State t -> Effect Unit
overState f (state /\ setState) = setState $ f state
overState f (state /\ setState) = setState $ f state
useLayoutEffect1' :: forall a. a -> (Unit -> Effect Unit) -> R.Hooks Unit
useLayoutEffect1' a f = R.useLayoutEffect1 a $ \_ ->
do f unit
pure $ \_ -> pure unit
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment