# All files / components/Sender index.js

94% Statements 47/50
76.92% Branches 20/26
86.36% Functions 19/22
95.83% Lines 46/48

    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
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187

    19x
    1x

    1x

    19x

    19x
    19x
    19x
    19x

    19x

    19x

    19x

    19x

    1x

    1x
    19x
    19x

    1x

    13x
    13x
    13x
    13x
    13x
    13x
    13x

    6x
    6x
    6x

    6x
    6x

    6x
    4x

    2x
    2x
    2x

    5x

    3x
    3x
    3x
    2x
    2x

    21x
    21x
    21x

    27x

    1x

    1x

     import React, { Component } from 'react';
    import PropTypes from 'prop-types';
    import styled from 'styled-components';
    import { rgba } from 'polished';
    import { Icon } from 'react-icons-kit';
    import { ic_send as SendIcon } from 'react-icons-kit/md/ic_send';
    import { ic_attach_file as AttachmentIcon } from 'react-icons-kit/md/ic_attach_file';

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

    const MdSend = (props) => <Icon icon={SendIcon} {...props} />;
    const MdAttachment = (props) => <Icon icon={AttachmentIcon} {...props} />;

    const StyledSenderContainer = styled.div`
      min-height: 56px;
      max-height: 200px;
      color: ${(props) => textColor(props.theme, 'light', 'primary')};
      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.footnote.fontSize};
        line-height: ${(props) => props.theme.typography.classes.footnote.lineHeight};
        font-weight: ${(props) => props.theme.typography.weights.fontWeightMedium};

        cursor: ${(props) => props.disabled ? 'not-allowed' : 'default'};

        white-space: pre-wrap;
        background-color: ${(props) => rgba(themeColor(props.theme, 'foreground', 'base'), 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) => props.disabled ? rgba(textColor(props.theme, 'light', 'hint'), 0.1) : 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: ${(props) => props.disabled ? 'not-allowed' : 'pointer'};
        color: ${(props) => props.disabled ? rgba(themeColor(props.theme, 'accent', 'base'), 0.1) : themeColor(props.theme, 'accent', 'base')};
    `;

    const StyledAttachmentButton = styled.button`
        transform: rotate(45deg);
        cursor: ${(props) => props.disabled ? 'not-allowed' : 'pointer'};
        color: ${(props) => props.disabled ? rgba(themeColor(props.theme, 'accent', 'base'), 0.1) : themeColor(props.theme, 'accent', 'base')};
    `;

    class Sender extends React.PureComponent {
      constructor(props) {
        super(props);
        this.myTextArea = null;
        this.state = { input: '', validInput: false };
        this.handleChange = this.handleChange.bind(this);
        this.handleKeyDown = this.handleKeyDown.bind(this);
        this.focusOnTextArea = this.focusOnTextArea.bind(this);
        this.handleSend = this.handleSend.bind(this);
      }

      handleChange(e) {
        const { onChange } = this.props;
        const newValue = e.target.value;
        const validInput = newValue.trim().length > 0;

        this.setState({ input: newValue });
        this.setState({ validInput });

        if (onChange && validInput) {
          onChange(newValue);
        }
      }

      handleKeyDown(e) {
        Eif (e.keyCode === 13 && e.shiftKey === false) {
          this.handleSend();
          e.preventDefault();
        }
      }

      focusOnTextArea() {
        this.myTextArea.focus();
      }

      handleSend() {
        const { input, validInput } = this.state;
        const { sendMessage } = this.props;
        if (validInput) {
          sendMessage(input);
          this.setState({ input: '', validInput: false });
        }
      }

      render() {
        /* eslint-disable jsx-a11y/no-autofocus */
        const { allowAttachments, placeholder, disabled, onAttachmentClick } = this.props;
        const { input, validInput } = this.state;
        return (
          <StyledSenderContainer disabled={disabled}>
            <div
              role="button"
              onKeyDown={this.focusOnTextArea}
              onClick={this.focusOnTextArea}
              tabIndex={0}
            >
              <textarea
                ref={(texta) => { this.myTextArea = texta; }}
                type="text"
                name="message"
                value={input}
                placeholder={placeholder}
                disabled={disabled}
                autoComplete="off"
                autoFocus
                onChange={this.handleChange}
                onKeyDown={this.handleKeyDown}
              />
              <StyledSenderButtonsContainer>
                {allowAttachments && (
                  <StyledAttachmentButton onClick={onAttachmentClick} disabled={disabled}>
                    <MdAttachment size={20} />
                  </StyledAttachmentButton>
                )}
                <StyledActionButton onClick={this.handleSend} disabled={disabled || !validInput}>
                  <MdSend size={20} />
                </StyledActionButton>
              </StyledSenderButtonsContainer>
            </div>
          </StyledSenderContainer>
        );
      }
    }

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

    Sender.displayName = 'Sender';

    export default Sender;