# All files / components/Sender index.js

100% Statements 30/30
66.67% Branches 4/6
100% Functions 13/13
100% Lines 30/30

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
    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
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160

    1x

    7x
    7x

    7x
    7x
    7x
    7x

    7x

    7x

    1x

    1x

    5x
    5x
    5x
    5x
    5x

    3x

    3x

    3x
    3x

    1x

    1x
    1x
    1x

    1x
    1x

    9x
    9x

    1x

    1x

     import React, { Component } from 'react';
    import PropTypes from 'prop-types';
    import styled from 'styled-components';
    import { rgba } from 'polished';

    import MdSend from 'react-icons/lib/md/send';
    import MdAttachment from 'react-icons/lib/md/attachment';

    import { textColor, themeColor } from '../../utils/style';

    const StyledSenderContainer = styled.div`
      min-height: 56px;
      max-height: 200px;
      color: ${(props) => textColor(props.theme, 'light', 'primary')};
      background-color: ${(props) => rgba(themeColor(props.theme, 'foreground', 'base'), 0.1)};
      background-color: rgba(94, 124, 139, 0.1);
      align-items: center;
      display: flex;

      border-radius: 0;
      flex-shrink: 0;
      & > div {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;

        min-height: 56px;
        max-height: 200px;
      }

      textarea {
        position: absolute;
        bottom: 0;
        left: 0;
        border: 0;
        box-sizing: border-box;
        padding: 22px 100px 10px 30px;
        width: 100%;
        height: 100%;

        color: ${(props) => textColor(props.theme, 'light', 'primary')};
        font-size: ${(props) => props.theme.typography.classes.body.fontSize};
        line-height: ${(props) => props.theme.typography.classes.body.lineHeight};
        font-weight: ${(props) => props.theme.typography.weights.fontWeightMedium};

        white-space: pre-wrap;
        background-color: rgba(94, 124, 139, 0.1);
        resize: none;
        border: none;
      }

      textarea:focus {
        background-color: ${(props) => props.theme.palette.common.white};
        outline: none;
        box-shadow: 0 0 100px 0 rgba(150,165,190,.24);
        transition: background-color .2s ease,box-shadow .2s ease,-webkit-box-shadow .2s ease;
      }

      textarea::placeholder {
        color: ${(props) => textColor(props.theme, 'light', 'hint')};
      }
    `;

    const StyledSenderButtonsContainer = styled.div`
      position: absolute;
      top: 0px;
      right: 30px;
      height: 56px;
      line-height: 56px;
      button {
        border: none;
        background: transparent;
      }
    `;

    const StyledActionButton = styled.button`
        cursor: pointer;
        color: rgb(94, 124, 139, .3);
        &:hover {
          color: rgb(94, 124, 139);
        }
    `;

    class Sender extends Component {
      constructor(props) {
        super(props);
        this.state = { input: '' };
        this.handleChange = this.handleChange.bind(this);
        this.handleKeyDown = this.handleKeyDown.bind(this);
        this.handleSend = this.handleSend.bind(this);
      }

      handleChange(e) {
        const newValue = e.target.value;

        this.setState({ input: newValue });

        Eif (this.props.onChange) {
          this.props.onChange(newValue);
        }
      }

      handleKeyDown(e) {
        const { sendMessage } = this.props;

        Eif (e.keyCode === 13 && e.shiftKey === false) {
          sendMessage(this.state.input);
          this.setState({ input: '' });
        }
      }

      handleSend() {
        const { sendMessage } = this.props;
        sendMessage(this.state.input);
      }

      render() {
        /* eslint-disable jsx-a11y/no-autofocus */
        const { placeholder, disabled } = this.props;
        return (
          <StyledSenderContainer>
            <div>
              <textarea
                type="text"
                name="message"
                value={this.state.input}
                placeholder={placeholder}
                disabled={disabled}
                autoComplete="off"
                autoFocus
                onChange={this.handleChange}
                onKeyDown={this.handleKeyDown}
              />
              <StyledSenderButtonsContainer>
                <StyledActionButton>
                  <MdAttachment size={20} />
                </StyledActionButton>
                <StyledActionButton onClick={this.handleSend}>
                  <MdSend size={20} />
                </StyledActionButton>
              </StyledSenderButtonsContainer>
            </div>
          </StyledSenderContainer>
        );
      }
    }

    Sender.propTypes = {
      onChange: PropTypes.func,
      sendMessage: PropTypes.func,
      placeholder: PropTypes.string,
      disabled: PropTypes.bool,
    };

    Sender.displayName = 'Sender';

    export default Sender;