all files / src/visualizations/ Legend.jsx

50% Statements 12/24
0% Branches 0/8
0% Functions 0/4
47.83% Lines 11/23
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                                                                                                
import React from 'react';
import PropTypes from 'prop-types';
 
import './Legend.css';
 
const propTypes = {
  categories: PropTypes.object,
  toggleCategory: PropTypes.func,
  showSingleCategory: PropTypes.func,
  position: PropTypes.oneOf(['tl', 'tr', 'bl', 'br']),
};
 
const defaultProps = {
  categories: {},
  toggleCategory: () => {},
  showSingleCategory: () => {},
  position: 'tr',
};
 
export default class Legend extends React.PureComponent {
  render() {
    if (Object.keys(this.props.categories).length === 0) {
      return null;
    }
 
    const categories = Object.entries(this.props.categories).map(([k, v]) => {
      const style = { color: 'rgba(' + v.color.join(', ') + ')' };
      const icon = v.enabled ? '\u25CF' : '\u25CB';
      return (
        <li>
          <a
            href="#"
            onClick={() => this.props.toggleCategory(k)}
            onDoubleClick={() => this.props.showSingleCategory(k)}
          >
            <span style={style}>{icon}</span> {k}
          </a>
        </li>
      );
    });
 
    const vertical = this.props.position.charAt(0) === 't' ? 'top' : 'bottom';
    const horizontal = this.props.position.charAt(1) === 'r' ? 'right' : 'left';
    const style = {
      [vertical]: '0px',
      [horizontal]: '10px',
    };
 
    return (
      <div className={'legend'} style={style}>
        <ul className={'categories'}>{categories}</ul>
      </div>
    );
  }
}
 
Legend.propTypes = propTypes;
Legend.defaultProps = defaultProps;