# All files / src/components Node.jsx

77.78% Statements 14/18
50% Branches 2/4
60% Functions 3/5
77.78% Lines 14/18

    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
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97

    3x

    3x

    3x

    7x
    7x
    7x
    7x

    7x

    1x

    7x

    7x

    7x

    3x
    3x

     import { Bar } from '@vx/shape';
    import React from 'react';
    import PropTypes from 'prop-types';

    import { nodeShape } from '../propShapes';

    export const DEFAULT_NODE_WIDTH = 7;

    const propTypes = {
      node: nodeShape.isRequired,
      x: PropTypes.number.isRequired,
      y: PropTypes.number.isRequired,
      width: PropTypes.number,
      height: PropTypes.number.isRequired,
      fill: PropTypes.string,
      onMouseOver: PropTypes.func,
      onMouseOut: PropTypes.func,
      onClick: PropTypes.func,
    };

    const defaultProps = {
      width: DEFAULT_NODE_WIDTH,
      fill: 'magenta',
      onMouseOver: null,
      onMouseOut: null,
      onClick: null,
    };

    class Node extends React.PureComponent {
      constructor(props) {
        super(props);
        this.onClick = this.onClick.bind(this);
        this.onMouseOut = this.onMouseOut.bind(this);
        this.onMouseOver = this.onMouseOver.bind(this);

        this.state = {
          isMousedOver: false,
        };
      }

      onClick(e) {
        this.props.onClick(e);
      }

      onMouseOver(e) {
        this.setState({ isMousedOver: true });
        this.props.onMouseOver(e);
      }

      onMouseOut(e) {
        this.setState({ isMousedOver: false });
        this.props.onMouseOut(e);
      }

      render() {
        const {
          isMousedOver,
        } = this.state;

        const {
          node,
          x,
          y,
          width,
          height,
          fill,
        } = this.props;

        return (
          <g
            onClick={this.onClick}
            onMouseOver={this.onMouseOver}
            onMouseOut={this.onMouseOut}
            data-node={node.id}
          >
            <Bar
              x={x}
              y={y}
              width={Math.max(1, width)}
              height={Math.max(1, height)}
              fill={fill}
              stroke={isMousedOver ? '#484848' : '#FFF'}
              strokeWidth={isMousedOver ? 2 : 1}
              rx={2}
              ry={2}
              vectorEffect="non-scaling-stroke"
            />
          </g>
        );
      }
    }

    Node.propTypes = propTypes;
    Node.defaultProps = defaultProps;

    export default Node;