Commit 6ecca031 authored by James Laver's avatar James Laver

Refactor G.C.Tab

parent 04fb25c1
...@@ -3,67 +3,48 @@ module Gargantext.Components.Tab where ...@@ -3,67 +3,48 @@ module Gargantext.Components.Tab where
import Prelude hiding (div) import Prelude hiding (div)
import Data.Array (fold) import Data.Array (fold)
import Data.Lens (Lens', Prism', over, view) import Data.FunctorWithIndex (mapWithIndex)
import Data.List (List, mapWithIndex, toUnfoldable)
import Data.Tuple (Tuple(..)) import Data.Tuple (Tuple(..))
import React (ReactElement) import Data.Tuple.Nested ((/\))
import React.DOM (a, div, nav, text) import Reactix as R
import React.DOM.Props (className, onClick) import Reactix.DOM.HTML as H
import Thermite ( PerformAction, Render, Spec
, _render, modifyState, focus
, simpleSpec, withState)
type State = { activeTab :: Int } type TabsProps = ( tabs :: Array (Tuple String R.Element), selected :: Int )
data Action = ChangeTab Int tabs :: Record TabsProps -> R.Element
tabs props = R.createElement tabsCpt props []
tabs :: forall state props action. -- this is actually just the list of tabs, not the tab contents itself
Lens' state State -> Prism' action Action tabsCpt :: R.Component TabsProps
-> List (Tuple String (Spec state props action)) tabsCpt = R.hooksComponent "Tabs" cpt
-> Spec state props action
tabs l p ls = withState \st ->
let {activeTab} = view l st in
fold
[ focus l p $ simpleSpec performAction (render activeTab ls)
, wrapper $ fold $ mapWithIndex ( tab activeTab) ls
]
where where
performAction :: forall props. cpt props _ = do
PerformAction State props Action (activeTab /\ setActiveTab) <- R.useState' props.selected
performAction (ChangeTab activeTab) _ _ = pure $
void $ modifyState $ const {activeTab} H.div { className: "tab-content" }
wrapper = over _render \render d p s c -> [ H.nav {}
[div [className "tab-content"] $ render d p s c] [ H.div { className: "nav nav-tabs" }
(mapWithIndex (item setActiveTab activeTab) props.tabs) ] ]
tab :: forall state props action. item setActiveTab selected index (name /\ _) =
Int -> Int -> Tuple String (Spec state props action) H.a { className, on: { click } } [ H.text name ]
-> Spec state props action where
tab sid iid (Tuple name spec) = over _render tabRender spec eq = index == selected
className = "nav-item nav-link" <> (if eq then " active" else "")
click e = setActiveTab (const index)
-- TODO: document what these are (selection, item indices)
type TabProps = ( selected :: Int, index :: Int )
tab :: Record TabProps -> Array R.Element -> R.Element
tab = R.createElement tabCpt
-- | A tab only shows its contents if it is currently selected
tabCpt :: R.Component TabProps
tabCpt = R.staticComponent "Tab" cpt
where where
tabRender renderer d p s c = cpt { selected, index } children = H.div { className } children'
[ div [ className $ "tab-pane " <> where
if sid ==iid same = selected == index
then " show active" className = "tab-pane" <> (if same then "show active" else "fade")
else " fade"] $ children' = if same then children else []
if sid == iid
then renderer d p s c
else []
]
render :: forall state props action.
Int -> List (Tuple String (Spec state props action))
-> Render State props Action
render at ls d p s c =
[ nav []
[ div [className "nav nav-tabs"]
$ toUnfoldable $ mapWithIndex (item at) ls
]
]
where
item :: forall a. Int -> Int -> (Tuple String a) -> ReactElement
item sid iid (Tuple name _) =
a [className $ "nav-item nav-link" <>
if sid == iid
then " active"
else "", onClick \e -> d $ ChangeTab iid] [text name]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment