Advertisement
Guest User

Untitled

a guest
May 24th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react'
  2. import PropTypes from 'prop-types'
  3. import { Field } from 'formik'
  4. import { FormFieldContext } from './FormField'
  5. import Icon from '../units/Icon'
  6.  
  7. class Input extends React.Component {
  8.   static contextType = FormFieldContext
  9.  
  10.   static propTypes = {
  11.     type: PropTypes.oneOf(['email', 'text', 'password']).isRequired,
  12.     placeholder: PropTypes.string
  13.   }
  14.  
  15.   static defaultProps = {
  16.     type: 'text',
  17.     placeholder: null
  18.   }
  19.  
  20.   constructor (props) {
  21.     super(props)
  22.  
  23.     this.setStateTimeout = this.setStateTimeout.bind(this)
  24.     this.clearStateTimeout = this.clearStateTimeout.bind(this)
  25.     this.createValidateOnChange = this.createValidateOnChange.bind(this)
  26.     this.createValidateOnBlur = this.createValidateOnBlur.bind(this)
  27.     this.prepareRender = this.prepareRender.bind(this)
  28.  
  29.     this.state = {
  30.       timeout: null
  31.     }
  32.   }
  33.  
  34.   componentWillMount () {
  35.     this.validateOnChange = this.createValidateOnChange()
  36.     this.validateOnBlur = this.createValidateOnBlur()
  37.   }
  38.  
  39.   setStateTimeout (value) {
  40.     this.setState({ timeout: value })
  41.   }
  42.  
  43.   clearStateTimeout () {
  44.     clearTimeout(this.state.timeout)
  45.   }
  46.  
  47.   createValidateOnChange (delay = 1000) {
  48.     const setStateTimeout = this.setStateTimeout
  49.     const clearStateTimeout = this.clearStateTimeout
  50.     const {
  51.       name,
  52.       values,
  53.       setFieldTouched,
  54.       setFieldValue,
  55.       validateForm
  56.     } = this.context
  57.  
  58.     return async function validateOnChange (event) {
  59.       const value = event.target.value
  60.       await setFieldValue(name, value, true)
  61.       await setFieldTouched(name, true, true)
  62.       await new Promise((resolve) => {
  63.         clearStateTimeout()
  64.  
  65.         setStateTimeout(
  66.           setTimeout(() => (
  67.             resolve(
  68.               validateForm(
  69.                 Object.assign(values, {
  70.                   [name]: value
  71.                 })
  72.               )
  73.             )
  74.           ), delay)
  75.         )
  76.       })
  77.     }
  78.   }
  79.  
  80.   createValidateOnBlur () {
  81.     const clearStateTimeout = this.clearStateTimeout
  82.     const {
  83.       name,
  84.       values,
  85.       setFieldTouched,
  86.       validateForm
  87.     } = this.context
  88.  
  89.     return async function validateOnBlur (event) {
  90.       const value = event.target.value
  91.       clearStateTimeout()
  92.       await setFieldTouched(name, true, true)
  93.       await validateForm(Object.assign(values, { [name]: value }))
  94.     }
  95.   }
  96.  
  97.   prepareRender () {
  98.     const { name, errors, touched } = this.context
  99.  
  100.     let controlClassName = 'control'
  101.     let inputClassName = 'input'
  102.     let icon = null
  103.  
  104.     if (touched[name]) {
  105.       let iconName = ''
  106.       controlClassName += ' has-icons-right'
  107.  
  108.       if (errors[name]) {
  109.         inputClassName += ' is-danger'
  110.         iconName = 'fa-exclamation-triangle'
  111.       } else {
  112.         inputClassName += ' is-success'
  113.         iconName = 'fa-check'
  114.       }
  115.  
  116.       icon = (
  117.         <Icon
  118.           name={iconName}
  119.           size="is-small"
  120.           position="is-right" />
  121.       )
  122.     }
  123.  
  124.     return { controlClassName, inputClassName, icon }
  125.   }
  126.  
  127.   render () {
  128.     const { name, values } = this.context
  129.     const { type, placeholder } = this.props
  130.     const { controlClassName, inputClassName, icon } = this.prepareRender()
  131.     return (
  132.       <div className={controlClassName}>
  133.         <Field
  134.           className={inputClassName}
  135.           name={name}
  136.           type={type}
  137.           value={values[name]}
  138.           placeholder={placeholder}
  139.           onChange={this.validateOnChange}
  140.           onBlur={this.validateOnBlur}
  141.         />
  142.         {icon}
  143.       </div>
  144.     )
  145.   }
  146. }
  147.  
  148. export default Input
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement