# All files / components/Input index.js

100% Statements 19/19
100% Branches 10/10
100% Functions 16/16
100% Lines 19/19

    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

    1x

    2x

    2x
    2x
    2x

    2x

    2x

    2x
    2x

    2x

    2x

    2x

    2x

    2x

    2x
    2x

    1x

    4x

    1x

     /* eslint-disable jsx-a11y/label-has-for */
    /**
    *
    * Input
    *
    */

    import React from 'react';
    import PropTypes from 'prop-types';
    import styled from 'styled-components';
    import classNames from 'classnames';

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

    const StyledInputContainer = styled.div`
      position:relative;
      margin-bottom:35px;

      input, label {
        display:block;
      }

      label {
        font-size: ${(props) => props.theme.typography.classes.small.fontSize};
      }

      input {
        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};
        padding:10px 10px 10px 5px;
        display:block;
        width:100%;
        border:none;
        border-bottom:1px solid ${(props) => textColor(props.theme, 'light', 'disabled')};
      }

      input:focus {
       box-shadow: none;
       outline: none;
      }

      label {
        color:#999;
        font-size: ${(props) => props.theme.typography.classes.body.fontSize};
        font-weight:normal;
        position:absolute;
        pointer-events:none;
        left:5px;
        top:10px;
        transition:0.2s ease all;
        -moz-transition:0.2s ease all;
        -webkit-transition:0.2s ease all;
      }

      /* active state */
      input:focus ~ label, input.touched ~ label {
        top:-20px;
        font-size: ${(props) => props.theme.typography.classes.caption.fontSize};
        color: ${(props) => textColor(props.theme, 'light', 'primary')};
      }

      /* active state */
      input:focus ~ .bar:before, input:focus ~ .bar:after {
        width:50%;
      }

      /* BOTTOM BARS ================================= */
      .bar { position:relative; display:block; width:100%; }
      .bar:before, .bar:after {
        content:'';
        height:2px;
        width:0;
        bottom:1px;
        position:absolute;
        background: ${(props) => props.hasError ? props.theme.palette.error.main : textColor(props.theme, 'light', 'primary')};
        transition:0.2s ease all;
        -moz-transition:0.2s ease all;
        -webkit-transition:0.2s ease all;
      }

      .bar:before {
        left:50%;
      }
      .bar:after {
        right:50%;
      }

      /* HIGHLIGHTER ================================== */
      .highlight {
        position:absolute;
        height:60%;
        width:50%;
        top:25%;
        left:0;
        pointer-events:none;
        opacity:0.5;
      }

      /* active state */
      input:focus ~ .highlight {
        -webkit-animation:inputHighlighter 0.3s ease;
        -moz-animation:inputHighlighter 0.3s ease;
        animation:inputHighlighter 0.3s ease;
      }

      /* ANIMATIONS ================ */
      @-webkit-keyframes inputHighlighter {
        from { background: ${(props) => props.hasError ? props.theme.palette.error.main : textColor(props.theme, 'light', 'primary')}; }
        to { width:0; background:transparent; }
      }
      @-moz-keyframes inputHighlighter {
        from { background: ${(props) => props.hasError ? props.theme.palette.error.main : textColor(props.theme, 'light', 'primary')}; }
        to { width:0; background:transparent; }
      }
      @keyframes inputHighlighter {
        from { background: ${(props) => props.hasError ? props.theme.palette.error.main : textColor(props.theme, 'light', 'primary')}; }
        to { width:0; background:transparent; }
      }

      .error-message {
        color: ${(props) => props.theme.palette.error.main};
        display: inline-block;
        font-size:  ${(props) => props.theme.typography.classes.caption.fontSize};
        line-height: ${(props) => props.theme.typography.classes.caption.lineHeight};
        margin: 5px;
      }
    `;

    const Input = ({
      type, name, value, placeholder, handleChange, handleBlur, className, hasError, errorMsg,
    }) => (
      <StyledInputContainer hasError={hasError}>
        <input
          className={classNames('letstalk-input', className)}
          type={type}
          id={name}
          name={name}
          autoComplete="false"
          onChange={handleChange}
          onBlur={handleBlur}
          value={value}
        />
        <span className="highlight"></span>
        <span className="bar"></span>
        <label htmlFor={name}>
          {placeholder}
        </label>
        {hasError && <span className="error-message">{errorMsg}</span>}
      </StyledInputContainer>
    );

    Input.propTypes = {
      type: PropTypes.string.isRequired,
      name: PropTypes.string.isRequired,
      value: PropTypes.string.isRequired,
      placeholder: PropTypes.string,
      hasError: PropTypes.bool,
      className: PropTypes.string,
      errorMsg: PropTypes.string,
      handleChange: PropTypes.func,
      handleBlur: PropTypes.func,
    };

    export default Input;