1.1.4 • Published 4 years ago

react-tabbed-view v1.1.4

Weekly downloads
1
License
MIT
Repository
-
Last release
4 years ago

React TabbedView

A highly customizable, barebones React tab component.

This is not a component you can simply plug in and be satisfied. There are no default styles; everything except the actual functionality is left to you, the user, to implement. Since there are no default styles, it's been made as easy as possible for you to define them.

Install

$ npm install --save react-tabbed-view

Features

  • Highly customizable - no default styles that you would just override anyways. Even the default handling of tab changes can be overridden. * However, not needlessly verbose when you don't need the extra oomph.
  • No mounts/unmounts between changing tabs by default - non-active tabs are simply made invisible with css {display: none}. This is the only time we touch the style of a component. This can be changed so that remounts will happen.

Usage

import React from 'react';
import {TabbedView, Tab, TabTitle, TabContent} from 'react-tabbed-view';

class Component extends React.Component {
  state = {
    selectedKey: 'home'
  }

  handleTabChange = (evt, key) => {
    this.setState({selectedKey: key});
  }

  render() {
    return (
      <TabbedView 
        className="tab-container"
        selectedKey={this.state.selectedKey} 
        onChange={this.handleTabChange}
        renderTab={(children, props) => <div {...props}>{children}</div>}
        renderTabList={children => <div {...props}>{children}</div>}
        renderTabContent={(children, props) => <div {...props}>{children}</div>}
      >
        <Tab tabKey="home">
          <TabTitle className="tab-title">Home</TabTitle>
          <TabContent className="tab-content">
            <h2>Hello World!</h2>
            <p>Lorem ipsum...</p>
          </TabContent>
        </Tab>
        <Tab tabKey="about">
          <TabTitle className="tab-title">This is the title</TabTitle>
          <TabContent className="tab-content">
            This is the content.
          </TabContent>
        </Tab>
      </TabbedView>
    );
  }
}

Components

TabbedView

The root component. Children should be Tab components.

PropTypeDescription
selectedKeystring or numberKey of the selected tab. If a tab with such key cannot be found, then none of the tabs will be selected.
onChangefuncA callback function that will be called when a non-selected tab should be selected. Called when a tab is clicked, or when navigated to through keyboard events.
tabListClassNamestringclassName of the element wrapping all the tabs. Use this to set the className of the tab list element if you don't want to define the whole render function for it.
renderTabfuncA callback that determines how a tab should be rendered.
renderTabListfuncA callback that determines how the component wrapping all of the tabs should be rendered.
renderTabContentfuncA callback that determines how the contents of the selected tab should be rendered.

Rendering callbacks

The props renderTab, renderTabList and renderTabContent should be given special attention. They determine how you want your tabs to behave and look, and even give you a change to override default functionality.

FunctionParametersNotes
renderTabchildren, tabKey, propsprops are the props you give to the TabTitle component, plus the properties onClick and onKeyDown.
renderTabListchildren, propsprops is empty, unless you have specified the tabListClassName prop.
renderTabContentchildren, tabKey, propsprops are the props you give to the TabContent component, plus the property style.

As you can see, you can even choose to override the onClick and onKeyDown events on the tabs; the two events that can fire the onChange callback prop you gave to TabbedView.

You might want the contents of a tab to fire a remount on tab change. You can do this. You simply need to return null in the renderTabContent function when the tab is not active.

You might not want to modify the intricate details of the components (they can be needlessly verbose). Props given to TabTitle will be passed to the render function renderTab, and props given to TabContent will be given to the render function renderTabContent. This way you don't have to define the whole rendering procedure for simply setting a class name.

Tab

Determines the title and contents of a single tab. The children should consist of one TabTitle, and one TabContent, although they can be in any order.

PropTypeDescription
tabKeyany, requiredKey of the tab. TabbedView uses this to determine the selected tab.

Tab determines the logical grouping of a title and content. In reality the titles will be grouped together inside a tab list (whose rendering you can control by defining the renderTabList function on the TabbedView component), and the contents will be one after the other next to the tab list.

TabTitle

Should go inside a Tab component. The contents of this component determine the title of that tab.

PropTypeDescription
selectedClassNamestringIf the tab this component is inside of is selected, this propwill be appended to the className of this component.

If you specify the onClick prop, they will be called with the original event and tabKey parameters before the default onClick handler. If you return false from your own onClick handler, the respective default event handler will not be called.

TabContent

Should go inside a Tab component. The contents of this component determine the contents of that tab.

PropTypeDescription
selectedClassNamestringIf the tab this component is inside of is selected, this propwill be appended to the className of this component.

Note that specifying a style prop with a display property on this component will override the default style given by TabbedView, which makes the component invisible when the tab is not selected. If you specify a style prop with a display property, you will have to handle the visibility yourself.

Licence

See the Licence file.

1.1.4

4 years ago

1.1.3

4 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago