# All files / form FormInput.js

88.89% Statements 24/27
69.57% Branches 16/23
91.67% Functions 11/12
88.46% Lines 23/26

    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

    1x

    22x
    22x

    22x
    22x

    1x

    3x

    23x
    1x
    1x

    22x

    1x

    1x

    1x

    3x
    3x
    3x

    23x
    23x

    23x

    1x

    1x

    1x

     import PropTypes from 'prop-types';
    import React, { Component } from 'react';
    import {
      Animated,
      Easing,
      TextInput,
      StyleSheet,
      Platform,
      Dimensions,
      Text as NativeText,
    } from 'react-native';
    import colors from '../config/colors';
    import normalize from '../helpers/normalizeText';
    import ViewPropTypes from '../config/ViewPropTypes';

    const { width } = Dimensions.get('window');

    class FormInput extends Component {
      constructor(props) {
        super(props);
        this.shake = this.shake.bind(this);
      }

      componentWillMount() {
        this.shakeAnimationValue = new Animated.Value(0);
        this.props.shake && this.shake();
      }

      componentWillReceiveProps(nextProps) {
        nextProps.shake && this.props.shake !== nextProps.shake && this.shake();
      }

      getRef = () => {
        return this.input || this.refs[this.props.textInputRef];
      };

      getRefHandler = () => {
        if (this.props.textInputRef) {
          Eif (typeof this.props.textInputRef === 'function') {
            return input => {
              this.input = input;
              this.props.textInputRef(input);
            };
          } else {
            return this.props.textInputRef;
          }
        } else {
          return input => (this.input = input);
        }
      };

      focus() {
        this.getRef() && this.getRef().focus();
      }

      blur() {
        this.getRef() && this.getRef().blur();
      }

      clearText() {
        this.getRef() && this.getRef().clear();
      }

      shake() {
        const { shakeAnimationValue } = this;
        shakeAnimationValue.setValue(0);
        Animated.timing(shakeAnimationValue, {
          duration: 375,
          toValue: 3,
          ease: Easing.bounce,
        }).start();
      }

      render() {
        const {
          containerStyle,
          inputStyle,
          containerRef,
          normalizeFontSize,
          shake,
          textInputRef,
          ...attributes
        } = this.props;
        const translateX = this.shakeAnimationValue.interpolate({
          inputRange: [0, 0.5, 1, 1.5, 2, 2.5, 3],
          outputRange: [0, -15, 0, 15, 0, -15, 0],
        });
        return (
          <Animated.View
            ref={containerRef}
            style={[
              styles.container,
              containerStyle && containerStyle,
              {
                transform: [{ translateX }],
              },
            ]}
          >
            <TextInput
              {...attributes}
              ref={this.getRefHandler()}
              style={[
                styles.input,
                { fontSize: normalizeFontSize ? normalize(14) : 14 },
                inputStyle && inputStyle,
              ]}
            />
          </Animated.View>
        );
      }
    }

    FormInput.propTypes = {
      containerStyle: ViewPropTypes.style,
      inputStyle: NativeText.propTypes.style,
      // Deprecated
      textInputRef: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
      // Deprecated
      containerRef: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
      normalizeFontSize: PropTypes.bool,
      shake: PropTypes.oneOfType([
        PropTypes.string,
        PropTypes.bool,
        PropTypes.object,
        PropTypes.number,
        PropTypes.array,
      ]),
    };

    FormInput.defaultProps = {
      normalizeFontSize: true,
    };

    const styles = StyleSheet.create({
      container: {
        marginLeft: 15,
        marginRight: 15,
        ...Platform.select({
          ios: {
            borderBottomColor: colors.grey4,
            borderBottomWidth: 1,
            marginLeft: 20,
            marginRight: 20,
          },
        }),
      },
      input: {
        ...Platform.select({
          android: {
            minHeight: 46,
            width: width - 30,
          },
          ios: {
            minHeight: 36,
            width: width,
          },
          web: {
            minHeight: 36,
            width: width,
          },
        }),
        // breaks tests - fix before release
        // Invariant Violation: Invalid undefined `width` of type `string`
        // supplied to `StyleSheet input`, expected `number`.
        // width: '100%',
        color: colors.grey3,
      },
    });

    export default FormInput;