Git Product home page Git Product logo

react-tabs's Introduction

@bsonntag/react-tabs

CircleCI build

Flexible tab components for react.

Installation

Using npm:

$ npm install --save @bsonntag/react-tabs

Using yarn:

$ yarn add @bsonntag/react-tabs

This module uses react's createContext API, so make sure you have at least version 16.3.0 installed.

Example usage

import { Tab, TabPanel, Tabs } from '@bsonntag/react-tabs';
import React, { Component } from 'react';

class App extends Component {

  state = {
    selectedTab: 0
  };

  handleSelect = selectedTab => this.setState({ selectedTab });

  render() {
    return (
      <Tabs
        onSelect={this.handleSelect}
        selectedTab={this.state.selectedTab}
      >
        <ul>
          <Tab tab={0}>
            {({ select }) => (
              <li onClick={select}>
                {'First tab'}
              </li>
            )}
          </Tab>

          <Tab tab={1}>
            {({ select }) => (
              <li onClick={select}>
                {'Second tab'}
              </li>
            )}
          </Tab>
        </ul>

        <TabPanel tab={0}>
          {isSelected => isSelected && (
            <div>
              {'First tab content'}
            </div>
          )}
        </TabPanel>

        <TabPanel tab={1}>
          {isSelected => isSelected && (
            <div>
              {'Second tab content'}
            </div>
          )}
        </TabPanel>
      </Tabs>
    );
  }

}

Components

This module contains three components.

<Tabs />

Works mainly as a provider. It only renders its children.

This is a controlled component, so you'll have to save the selected tab on another component's state.

children: React$Node

This component only renders its children.

onSelect: (tab: string | number) => void

The event handler that is called when a tab is selected. This is called with the selected tab.

selectedTab: string | number

The currently selected tab.

<Tab />

children: (args: { isSelected: boolean, select: () => void }) => React$Node

This component receives a function as its children. It calls the children function with an object with two properties:

  • isSelected: this will be true if the tab is selected.
  • select: a function to call when this tab is selected.

This component will render the result of calling its children.

tab: string | number

This tab's identifier.

<TabPanel />

children: (isSelected: boolean) => React$Node

This component receives a function as its children. It calls the children function with an a boolean that will be true if the tab is selected.

This component will render the result of calling its children.

tab: string | number

This tab's identifier.

Contributing

Please feel free to submit any issues or pull requests.

License

MIT

react-tabs's People

Contributors

bsonntag avatar dependabot[bot] avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

guy-kdm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.