all files / src/datums/ email.cjsx

100% Statements 18/18
100% Branches 4/4
100% Functions 5/5
100% Lines 13/13
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                                                       
 
React = require('react')
_ = require('underscore')
 
Datum = require('./datum')
 
###
  For rendering and input of email addresses.  Can render mailto: links like 
  `<a href="mailto:">` in display mode
  
  Validates that email address is a semi valid email based on matching 
  `/^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/` 
###
module.exports = class Email extends Datum
  @displayName: "react-datum.Email"
 
  @propTypes: _.extend {}, Datum.propTypes,
    # If true, display as mailto address link when `inputMode='readonly'`
    displayAsLink: React.PropTypes.bool
 
  constructor: (props) ->
    super
    @addValidations @validateEmail
 
 
  renderValueForDisplay: ->
    value = super
    return if @props.displayAsLink
      <a href={@getMailToHref(value)}>{value}</a>
    else
      value
 
 
  getMailToHref: (value) ->
    return "mailto:#{value}"
 
 
  validateEmail: (value) =>
    return true if value.match /^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/
    return "Invalid email address.  Should be like 'bob@zulily.com'"