All files / source/input_date input_date.js

100% Statements 64/64
76.92% Branches 20/26
90.91% Functions 10/11
100% Lines 64/64
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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305                                24x 24x   24x 24x   24x 24x   24x 24x   24x         24x         24x                                         22x 22x       22x           22x 6x       22x           26x     26x 26x     26x     26x           18x       18x         18x       18x     18x   18x       1x       18x           40x 40x     40x                         4x     4x     4x                   4x       4x         4x                       10x             10x       10x     10x     10x 1x     9x         1x           10x                 4x       4x       4x         4x     4x         8x     8x           8x     8x   8x 8x     8x     8x     8x                               24x                   24x                  
// Dependencies.
import React from 'react'
import PropTypes from 'prop-types'
 
// Utility methods.
import {
  bind,
  exists,
  formatDate,
  trim
} from '@t7/utils'
 
// UI components.
import { Input } from '../'
 
// Date formats.
const DD_MM_YYYY = 'DD/MM/YYYY'
const MM_DD_YYYY = 'MM/DD/YYYY'
 
const MM_DD_YYYY_TO_API = '$3-$1-$2'
const MM_DD_YYYY_TO_UI = '$2/$3/$1'
 
const DD_MM_YYYY_TO_API = '$3-$2-$1'
const DD_MM_YYYY_TO_UI = '$3/$2/$1'
 
const FORMAT_FROM_API = /^(\d{4})-(\d{2})-(\d{2})/
const FORMAT_FROM_UI = /^(\d{2})\/(\d{2})\/(\d{4})/
 
const CONVERT_TO_API = {
  [DD_MM_YYYY]: DD_MM_YYYY_TO_API,
  [MM_DD_YYYY]: MM_DD_YYYY_TO_API
}
 
const CONVERT_TO_UI = {
  [DD_MM_YYYY]: DD_MM_YYYY_TO_UI,
  [MM_DD_YYYY]: MM_DD_YYYY_TO_UI
}
 
const PLACEHOLDER_FORMAT = {
  [DD_MM_YYYY]: DD_MM_YYYY,
  [MM_DD_YYYY]: MM_DD_YYYY
}
 
// =============== //
// =============== //
// 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 = '', dateFormat) {
    // Regex.
    const BEFORE = FORMAT_FROM_UI
    const AFTER = CONVERT_TO_API[dateFormat]
 
    // Format?
    let apiValue = (
      BEFORE.test(value)
        ? value.replace(BEFORE, AFTER)
        : ''
    )
 
    // Invalid?
    if (!that.isValidDate(apiValue)) {
      apiValue = ''
    }
 
    // Expose string.
    return apiValue
  }
 
  // Convert to UI.
  static convertToUiValue (value = '', dateFormat) {
    // Trim to 10 characters.
    value = trim(value).slice(0, 10)
 
    // Regex.
    const BEFORE = FORMAT_FROM_API
    const AFTER = CONVERT_TO_UI[dateFormat]
 
    // Format to "MM/DD/YYYY".
    value = value.replace(BEFORE, AFTER)
 
    // Expose string.
    return value
  }
 
  // Error message.
  static getErrorMessage (value = '', dateFormat) {
    // From API to UI format.
    value =
      that.convertToUiValue(value, dateFormat)
 
    // Clean up.
    value =
      formatDate(value)
 
    // From UI to API format.
    const apiValue =
      that.convertToApiValue(value, dateFormat)
 
    // Valid date?
    const isValidDate =
      that.isValidDate(apiValue)
 
    // Set in conditional.
    let errorMessage = ''
 
    if (
      !isValidDate &&
      value.length === 10
    ) {
      errorMessage = 'Date is invalid'
    }
 
    // Expose string.
    return errorMessage
  }
 
  // Valid date?
  static isValidDate (x = '') {
    // Get date.
    const date = new Date(x)
    const bool = !isNaN(date.getTime())
 
    // Expose boolean
    return bool
  }
}
 
// ============= //
// ============= //
// Define class. //
// ============= //
// ============= //
 
class InputDate 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 {
      dateFormat,
      value,
      errorMessage: propsErrorMessage
    } = this.props
 
    // Error message.
    const errorMessage = (
      propsErrorMessage ||
      that.getErrorMessage(value, dateFormat)
    )
 
    // Update.
    this.state = {
      errorMessage,
      oldValue: value
    }
  }
 
  // Update state.
  static getDerivedStateFromProps (props = {}, state = {}) {
    // State.
    const {
      oldValue,
      errorMessage: oldErrorMessage
    } = state
 
    // Props.
    const {
      dateFormat,
      value,
      errorMessage: propsErrorMessage
    } = props
 
    // New error.
    const newErrorMessage =
      that.getErrorMessage(value, dateFormat)
 
    // 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 {
      dateFormat,
      errorMessage: propsErrorMessage
    } = this.props
 
    // Get API value.
    const apiValue =
      that.convertToApiValue(value, dateFormat)
 
    // Get error message.
    const errorMessage = (
      propsErrorMessage ||
      that.getErrorMessage(value, dateFormat)
    )
 
    // Update.
    this.setState({ errorMessage })
 
    // Callback.
    this.props.handleChange(e, value, apiValue)
  }
 
  // Placeholder.
  getPlaceholder () {
    const { dateFormat } = this.props
 
    // Expose string.
    return PLACEHOLDER_FORMAT[dateFormat]
  }
 
  // Render method.
  render () {
    // State.
    const { errorMessage } = this.state
 
    // Props.
    const { dateFormat } = this.props
 
    let { value } = this.props
    value = that.convertToUiValue(value, dateFormat)
 
    // Placeholder.
    const placeholder = this.getPlaceholder()
 
    // Events.
    const { handleChange } = this
 
    // Expose UI.
    return (
      <Input
        {...this.props}
        errorMessage={errorMessage}
        placeholder={placeholder}
        value={value}
 
        // Events.
        handleChange={handleChange}
        mask={formatDate}
      />
    )
  }
}
 
// Validation.
InputDate.propTypes = {
  dateFormat: PropTypes.string,
  errorMessage: PropTypes.string,
  value: PropTypes.string,
 
  // Events.
  handleChange: PropTypes.func
}
 
// Defaults.
InputDate.defaultProps = {
  dateFormat: DD_MM_YYYY,
 
  // Events.
  handleChange: () => {}
}
 
// Export.
export default InputDate