all files / packages/tabbed-pane/ TabbedPane.js

0% Statements 0/14
0% Branches 0/2
0% Functions 0/3
0% Lines 0/14
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68                                                                                                                                       
import { forEach } from '../../util'
import { Component } from '../../ui'
 
/*
  A tabbed pane layout component. The actual content is specified via append.
 
  @class TabbedPane
  @component
 
  @prop {Object[]} tabs an array of objects with id and name properties
  @prop {String} activeTab id of currently active tab
 
  @example
 
  ```js
  $$(TabbedPane, {
    tabs: [
      {id: 'tabA', 'A'},
      {id: 'tabB', 'B'},
    ],
    activeTab: 'tabA'
  }).ref('tabbedPane').append(
    tabAContent
  )
  ```
*/
 
class TabbedPane extends Component {
 
  render($$) {
    let el = $$('div').addClass('sc-tabbed-pane')
    let tabsEl = $$('div').addClass('se-tabs')
    forEach(this.props.tabs, function(tab) {
      let tabEl = $$('a')
        .addClass("se-tab")
        .attr({
          href: "#",
          "data-id": tab.id,
        })
        .on('click', this.onTabClicked)
      if (tab.id === this.props.activeTab) {
        tabEl.addClass("sm-active")
      }
      tabEl.append(
        $$('span').addClass('label').append(tab.name)
      )
      tabsEl.append(tabEl)
    }.bind(this))
 
    el.append(tabsEl)
    // Active content
    el.append(
      $$('div').addClass('se-tab-content').ref('tabContent').append(
        this.props.children
      )
    )
    return el
  }
 
  onTabClicked(e) {
    e.preventDefault()
    let tabId = e.currentTarget.dataset.id
    this.send('switchTab', tabId)
  }
}
 
export default TabbedPane