L
L
lookingfor22020-10-19 12:05:00
React
lookingfor2, 2020-10-19 12:05:00

How does the component work?

Parent component that implements tabulation

return (
        <>
            <div className="cabinet__history-title">
                <h1>
                    История операций
                </h1>
            </div>
            <TabBar>
                <TabBarItem label="Операции по займам">
                    <div className="loans-history">
                        {
                            OperationsLoan.map((loan, index) => {
                                return <Loan loan={loan} key={index}/>;
                            })
                        }
                    </div>
                     </TabBarItem>
            </TabBar>

        </>
    );

The child component that is called
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';

import TabBarNav from './TabBarNav.jsx';

class TabBar extends Component {
    static propTypes = {
        children: PropTypes.node,
        className: PropTypes.string,
        vertical: PropTypes.bool,
    };

    static defaultProps = {
        children: null,
        className: '',
        vertical: false,
    };

    state = {
        activeTab: null,
    }

    componentDidMount() {
        const { children = [] } = this.props;

        const activeTab = this.getChildrenLabels(children)[0];

        this.setActiveTab(activeTab);
    }

    getChildrenLabels = children => children.map(({ props }) => props.label)

    setActiveTab = activeTab => {
        const { activeTab: currentTab } = this.state;

        if (currentTab !== activeTab) {
            this.setState({
                activeTab,
            });
        }
    }

    renderTabs = () => {
        const { children = [] } = this.props;
        const { activeTab } = this.state;

        return this.getChildrenLabels(children).map(navLabel => (
            <TabBarNav
                key={navLabel}
                navLabel={navLabel}
                className={classNames({ active: activeTab === navLabel })}
                onChangeActiveTab={this.setActiveTab}
            />
        ));
    }
    renderActiveTab = () => {
        const { activeTab } = this.state;
        return activeTab;
    }

    render() {
        console.log(this.props)
        const { activeTab } = this.state;
        const {
            children, className, vertical, ...attrs
        } = this.props;

        const classes = classNames(
            'cabinet__history-content',
            className,
            { vertical },
        );
        return (
            <div className={classes} {...attrs}>
                <div className="cabinet__history-active_nav">
                    {this.renderActiveTab()}
                </div>
                <div className="cabinet__history-navigation">
                    {this.renderTabs()}
                </div>
                <div className="cabinet__history-items">
                    {React.Children.map(children, child => React.cloneElement(child, { activeTab }))}
                </div>
            </div>
        );
    }
}

export default TabBar;


Question:
- In general, how does this component work?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question