PanelGroup

suggest change
import React from 'react';
import Tab from './Tab.js';

class PanelGroup extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            panels: props.panels,
            activeTab: props.activeTab
        };
    }

    render() {
        this.tabSet = [];
        this.panelSet = [];
        for (let panelData of this.state.panels) {
            var tabIsActive = this.state.activeTab == panelData.name;
            this.tabSet.push(React.createElement(
                Tab, {
                    name: panelData.name,
                    active: tabIsActive,
                    panelClass: panelData.panelClass,
                    onMouseDown: () => this.openTab(panelData.name)
                }
            ));
            this.panelSet.push(React.createElement(
                panelData.panelClass, {
                    id: panelData.name,
                    active: tabIsActive,
                    ref: tabIsActive ? 'activePanel' : null
                }
            ));
        }
        return React.createElement(
            'div', { className: 'PanelGroup' },
            React.createElement(
                'nav', null,
                React.createElement(
                    'ul', null,
                    ...this.tabSet
                )
            ),
            ...this.panelSet
        );
    }

    openTab(name) {
        this.setState({ activeTab: name }, () => {
            ReactDOM.findDOMNode(this.refs.activePanel).focus();
        });
    }
}

The panels prop of a PanelGroup must be an array of objects. Each object describes one panel:

Don’t forget to set the activeTab prop to the name of the tab that should start open. ReactDOM must be in scope for findDOMNode (React 15 string refs).

When a tab is pressed, the matching panel gets the class name active on its DOM element (so it becomes visible) and receives focus.

Clarification

setState is asynchronous, so focus runs in its callback, after the newly active panel has been committed.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents