All files / source/input_phone_us input_phone_us.js

100% Statements 34/34
81.82% Branches 18/22
87.5% Functions 7/8
100% Lines 34/34
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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207                                                                7x     7x 4x       7x             6x     6x   6x       1x       6x                         1x     1x     1x                 1x       1x         1x                       4x           4x       4x     4x     4x 1x     3x         1x           4x               1x       1x       1x         1x     1x           2x     2x     2x                           24x                 24x              
// Dependencies.
import React from 'react'
import PropTypes from 'prop-types'
 
// Utility methods.
import {
  bind,
  exists,
  formatInteger,
  formatPhoneUS
} from '@t7/utils'
 
// UI components.
import { Input } from '../'
 
// =============== //
// =============== //
// Helper methods. //
// =============== //
// =============== //
 
/*
  NOTE: We're using a helper class here,
  because we cannot call `this.*` from
  within `getDerivedStateFromProps`.
*/
 
class that {
  // Convert to API.
  static convertToApiValue (value = '') {
    // Only numbers.
    let apiValue =
      formatInteger(value).slice(0, 10)
 
    // Valid length?
    if (apiValue.length < 10) {
      apiValue = ''
    }
 
    // Expose string.
    return apiValue
  }
 
  // Error message.
  static getErrorMessage (value = '') {
    // From UI to API format.
    const apiValue =
      that.convertToApiValue(value)
 
    // Set in conditional.
    let errorMessage = ''
 
    if (
      value.length >= 12 &&
      apiValue.length !== 10
    ) {
      errorMessage = 'Phone format is invalid'
    }
 
    // Expose string.
    return errorMessage
  }
}
 
// ============= //
// ============= //
// Define class. //
// ============= //
// ============= //
 
class InputPhoneUS extends React.Component {
  constructor (props) {
    // Pass `props` into scope.
    super(props)
 
    // Bind context.
    bind(this)
 
    // Get default state.
    this.defaultState()
  }
 
  // Set default state.
  defaultState () {
    // Props.
    const {
      value,
      errorMessage: propsErrorMessage
    } = this.props
 
    // Error message.
    const errorMessage = (
      propsErrorMessage ||
      that.getErrorMessage(value)
    )
 
    // Update.
    this.state = {
      errorMessage,
      oldValue: value
    }
  }
 
  // Update state.
  static getDerivedStateFromProps (props = {}, state = {}) {
    // State.
    const {
      oldValue,
      errorMessage: oldErrorMessage
    } = state
 
    // Props.
    const {
      value,
      errorMessage: propsErrorMessage
    } = props
 
    // New error.
    const newErrorMessage =
      that.getErrorMessage(value)
 
    // Set in conditional.
    let newState = null
 
    // Update?
    if (propsErrorMessage) {
      newState = {
        errorMessage: propsErrorMessage
      }
    } else if (
      value !== oldValue &&
      exists(newErrorMessage) &&
      newErrorMessage !== oldErrorMessage
    ) {
      newState = {
        errorMessage: newErrorMessage
      }
    }
 
    // Expose object
    return newState
  }
 
  // Change event.
  handleChange (e, value = '') {
    // Props.
    const {
      errorMessage: propsErrorMessage
    } = this.props
 
    // Convert to number.
    const apiValue =
      that.convertToApiValue(value)
 
    // Get error message.
    const errorMessage = (
      propsErrorMessage ||
      that.getErrorMessage(value)
    )
 
    // Update.
    this.setState({ errorMessage })
 
    // Callback.
    this.props.handleChange(e, value, apiValue)
  }
 
  // Render method.
  render () {
    // State.
    const { errorMessage } = this.state
 
    // Events.
    const { handleChange } = this
 
    // Expose UI.
    return (
      <Input
        {...this.props}
        errorMessage={errorMessage}
        mask={formatPhoneUS}
        maxlength='12'
        placeholder='000-000-0000'
        handleChange={handleChange}
      />
    )
  }
}
 
// Validation.
InputPhoneUS.propTypes = {
  errorMessage: PropTypes.string,
  value: PropTypes.string,
 
  // Events.
  handleChange: PropTypes.func
}
 
// Defaults.
InputPhoneUS.defaultProps = {
  // Events.
  handleChange: () => {}
}
 
// Export.
export default InputPhoneUS