Tab.purs 1.93 KB
Newer Older
1
module Gargantext.Components.Tab where
2

Sudhir Kumar's avatar
Sudhir Kumar committed
3
import Prelude hiding (div)
James Laver's avatar
James Laver committed
4
import Data.FunctorWithIndex (mapWithIndex)
5
import Data.Tuple (Tuple)
James Laver's avatar
James Laver committed
6 7 8
import Data.Tuple.Nested ((/\))
import Reactix as R
import Reactix.DOM.HTML as H
9

10 11
import Gargantext.Utils.Reactix as R2

12
thisModule :: String
13 14
thisModule = "Gargantext.Components.Tab"

15 16 17 18
type TabsProps = (
    selected :: Int
  , tabs :: Array (Tuple String R.Element)
  )
19

James Laver's avatar
James Laver committed
20 21
tabs :: Record TabsProps -> R.Element
tabs props = R.createElement tabsCpt props []
22

James Laver's avatar
James Laver committed
23 24
-- this is actually just the list of tabs, not the tab contents itself
tabsCpt :: R.Component TabsProps
25
tabsCpt = R.hooksComponentWithModule thisModule "tabs" cpt
26
  where
James Laver's avatar
James Laver committed
27 28 29
    cpt props _ = do
      (activeTab /\ setActiveTab) <- R.useState' props.selected
      pure $
30
        H.div {}
James Laver's avatar
James Laver committed
31
        [ H.nav {}
32 33 34
          [ H.div { className: "nav nav-tabs"
                  , title    : "Tab for ngrams"
                  }
35 36 37
            (mapWithIndex (button setActiveTab activeTab) props.tabs) ]
        , H.div { className: "tab-content" } $ mapWithIndex (item activeTab) props.tabs ]
    button setActiveTab selected index (name /\ _) =
James Laver's avatar
James Laver committed
38 39 40 41 42
      H.a { className, on: { click } } [ H.text name ]
      where
        eq = index == selected
        className = "nav-item nav-link" <> (if eq then " active" else "")
        click e = setActiveTab (const index)
43
    item selected index (_ /\ cpt') = tab { selected, index } [ cpt' ]
James Laver's avatar
James Laver committed
44 45 46 47

-- TODO: document what these are (selection, item indices)
type TabProps = ( selected :: Int, index :: Int )

48
tab :: R2.Component TabProps
James Laver's avatar
James Laver committed
49 50 51 52
tab = R.createElement tabCpt

-- | A tab only shows its contents if it is currently selected
tabCpt :: R.Component TabProps
53
tabCpt = R.staticComponent "G.C.Tab.tab" cpt
54
  where
James Laver's avatar
James Laver committed
55 56 57 58 59
    cpt { selected, index } children = H.div { className } children'
      where
        same = selected == index
        className = "tab-pane" <> (if same then "show active" else "fade")
        children' = if same then children else []
60