1.0.8 • Published 8 years ago
react-fancy-tab-panel v1.0.8
Fancy Tab Panel
Installations:
- Install via NPM:
npm install react-fancy-tab-panel - Sample Program:
 
import ReactFancyTabPanel from 'react-fancy-tab-panel' const TabPanelExample = ReactFancyTabPanel.TabPanelExample ... render(){ return <TabPanelExample /> } ...- Dependencies:  
- Webpack, Sass loaders
 - React, React-Measure, etc. (installed by npm)
 
 
- Install via NPM:
 Demo:
See https://iceberglet.github.io/ under page "Gallery->Fancy Tab Panel"Basic Idea:
- The 
TabPanelCoreis designed to be behaviour-agnostic. That is to say, it hooks the user action to callbacks provided, and processes tabs mutations uniquely from change of props. E.g. when user clicks add tab button, the tab panel will simply call theonAddTabfunction and does nothing else. Therefore the behaviour is left completely open to the higher level. (e.g. one may initiate a dialogue in this callback to ask for the new tab name, then add the tab by modifying theitemsprop of the tab panel) - To Use a TabPanel, either use a 
TabPanelBaseor modify it to suit your needs. Specifically, you need to consider the behaviours you need. (See below the relevant props to understand which props are relevant and how they are supposed to work) - If a callback is optional, and not provided, tab panel will assume that you forbid such action. (e.g. not providing 
onAddTabwill prevent the add tab button from showing up) 
- The 
 TabPanelCore Props:
Property Name Value needed Is Required Default Note items an array of Tabobjects that are expected to be shownyes undefinedthe order of items is the order of the tabs selected the idof the item selected.yes undefinedIt is YOUR responsibility to make sure the itemsdo contain a tab with such ID!onSelectTab a function that takes in the idof the tab, which can then mutate the stateno undefinedhere you can mutate selectedto update the tabsonAddTab a function hooked when user clicks on the button to add a tab. no undefinedsee Basic Idea on its logic onRemoveTab a function hooked when user removes a tab. takes in the idof the tabno undefinedsee Basic Idea on its logic onFinishDrag a function hooked when user finishes dragging. takes in the array of tabs of the new order yes undefineduser is advised to update the newly ordered array of tabs, since tab-panel does not do anything allowRemoveAll a boolean to tell if user can remove all tabs no falseif onRemoveTabis not given, then this bool is moot.allowDnD a boolean to tell if user can drag and drop to re-arrange tabs no truesetting this to false will make onFinishDragmoot.API Example:
(Base Usage, same as intab-panel-base.js)
NOTE: User can extend this React class or implement a modified version as a functional wrapper
  import React from 'react'
  import { Tab } from './tab';
  import { TabPanelCore } from './tab-panel-core'
  export const TabPanelBase = React.createClass({
    propTypes: {
      items: React.PropTypes.array.isRequired
    },
    getInitialState(){
      let items = this.props.items
      return {
        items, selected: items[0].id
      }
    },
    onSelectTab(id){
      this.setState({selected: id})
    },
    onAddTab(){
      this.setState(s=>{
        let tab = new Tab('New Tab')
        s.items.push(tab);
        s.selected = tab.id
        return s;
      })
    },
    onRemoveTab(id){
      this.setState(s=>{
        let deleted = s.items.findIndex(item=>item.id === id)
        s.items.splice(deleted, 1)
        if(id === s.selected){
          if(s.items.length > 0)
            s.selected = s.items[s.items.length - 1].id
        }
        return s;
      })
    },
    onFinishDrag(items){
      this.setState({
        items
      })
    },
    render(){
      let item = this.state.items.find(item=>item.id === this.state.selected) || {};
      return (<div>
          <TabPanelCore onSelectTab = {this.onSelectTab}
                         onAddTab = {this.onAddTab}
                         onRemoveTab = {this.onRemoveTab}
                         onFinishDrag = {this.onFinishDrag}
                         selected = {this.state.selected}
                         items = {this.state.items}
            />
          <div>{'You Just Selected ' + item.title}</div>
        </div>)
    }
  })