All files Breadcrumb.jsx

100% Statements 19/19
100% Branches 8/8
100% Functions 4/4
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59            4x     1x                   2x 1x     1x       4x 4x   4x 7x 7x   7x       7x 4x       4x       4x 4x 1x     3x            
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import BreadcrumbItem from './BreadcrumbItem';
 
export default class Breadcrumb extends Component {
    constructor(props) {
        super(props);
    }
 
    static propTypes = {
        items: PropTypes.arrayOf(PropTypes.shape({
            label: PropTypes.string,
            value: PropTypes.oneOfType([PropTypes.number, PropTypes.string])
        })).isRequired,
        value: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
        onChange: PropTypes.func
    }
 
    onChange(item) {
        if (this.props.value === item.value) {
            return;
        }
 
        this.props.onChange && this.props.onChange(item);
    }
 
    _renderItem() {
        const { value, items } = this.props;
        let breadcrumbItems = [];
 
        for (let i = 0; i < items.length; i++) {
            const item = items[i];
            const isActive = item.value === value;
 
            breadcrumbItems.push(
                <BreadcrumbItem key={`breadcrumb-item${i}`} isActive={isActive} label={item.label} onClick={this.onChange.bind(this, item)} />
            );
 
            if (isActive) {
                break;
            }
        }
 
        return breadcrumbItems;
    }
 
    render() {
        const items = this._renderItem();
        if (items.length === 1) {
            return null;
        }
 
        return (
            <div className='breadcrumb'>
                {items}
            </div>
        );
    }
}