Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useState, useRef, useEffect } from 'react'
  2. import { Button } from '../Button'
  3. import _ from 'lodash'
  4. import './styles.scss'
  5.  
  6. function Input ({ label = 'Label', placeholder, value = '', onChange, lineColor, labelColor, icon, fontSize = '22px', labelSize = '22px', onBlur, onFocus, ...rest }) {
  7.   const [active, setActive] = useState(value !== '' ? true : false)
  8.   const [innerValue, setInnerValue] = useState(value || '')
  9.  
  10.   useEffect(() => {
  11.     // Update the document title using the browser API
  12.     setActive(value !== '')
  13.     setInnerValue(value)
  14.   }, [value]);
  15.  
  16.   const delayed = useRef(_.debounce(value => onChange(value), 500)).current
  17.   let inputRef = React.createRef()
  18.   const valueChanged = e => {
  19.     setActive(e.target.value !== '')
  20.     setInnerValue(e.target.value)
  21.     if (onChange) delayed(e.target.value)
  22.   }
  23.  
  24.   const renderIcon = (icon) => {
  25.     switch (icon) {
  26.       case 'profile':
  27.         return (
  28.           <div className='input-icon'>
  29.             <Button.Icon.Profile onClick={() => { inputRef.focus() }} width={20} height={20} />
  30.           </div>
  31.         )
  32.       case 'calendar':
  33.         return (
  34.           <div className='input-icon'>
  35.             <Button.Icon.Calendar onClick={() => { inputRef.focus() }} width={20} height={20} />
  36.           </div>
  37.         )
  38.       case 'timer':
  39.         return (
  40.           <div className='input-icon'>
  41.             <Button.Icon.Timer onClick={() => { inputRef.focus() }} width={20} height={20} />
  42.           </div>
  43.         )
  44.       case 'clock':
  45.         return (
  46.           <div className='input-icon'>
  47.             <Button.Icon.Clock onClick={() => { inputRef.focus() }} width={20} height={20} />
  48.           </div>
  49.         )
  50.       case 'field':
  51.         return (
  52.           <div className='input-icon'>
  53.             <Button.Icon.Field onClick={() => { inputRef.focus() }} width={20} height={20} />
  54.           </div>
  55.         )
  56.       default:
  57.         return <Button.Icon.Profile />
  58.     }
  59.   }
  60.  
  61.   return (
  62.     <div className='input'>
  63.       <label className={`input-label ${active ? 'input-label-actived' : ''}`} style={ labelColor ? {color: labelColor, fontSize: labelSize} : { fontSize: labelSize } }>{label}</label>
  64.       <input
  65.         {...rest}
  66.         autoComplete="new-password"
  67.         ref={(ref) => inputRef = ref}
  68.         style={ lineColor ? { borderColor: lineColor, fontSize: fontSize } : { fontSize: fontSize } }
  69.         className={`input-text`}
  70.         onChange={valueChanged}
  71.         onFocus={(e) => {
  72.           if (onFocus) onFocus.call(null, e)
  73.           setActive(true)
  74.         }}
  75.         onBlur={(e) => {
  76.           if (onBlur) onBlur.call(null, e)
  77.           setActive( innerValue !== '' ? true : false)
  78.         }}
  79.         value={innerValue}
  80.       />
  81.       {icon && renderIcon(icon)}
  82.     </div>
  83.   )
  84. }
  85.  
  86. export { Input }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement