All files / atom/textarea/src index.js

75% Statements 6/8
63.63% Branches 7/11
50% Functions 1/2
75% Lines 6/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          1x                   3x                 3x         3x     1x   1x                                                          
import cx from 'classnames'
import PropTypes from 'prop-types'
 
import {BASE_CLASS, DEFAULT_PROPS, TEXTAREA_RESIZES, TEXTAREA_SIZES, TEXTAREA_STATES} from './settings.js'
 
const AtomTextarea = ({
  errorState,
  onBlur = DEFAULT_PROPS.onBlur,
  onChange = DEFAULT_PROPS.onChange,
  resize,
  size = DEFAULT_PROPS.size,
  state,
  value,
  ...props
}) => {
  const className = cx(
    BASE_CLASS,
    `${BASE_CLASS}--${size}`,
    errorState && `${BASE_CLASS}--${TEXTAREA_STATES.ERROR}`,
    errorState === false && `${BASE_CLASS}--${TEXTAREA_STATES.SUCCESS}`,
    resize && `${BASE_CLASS}--resize-${resize}`,
    state && `${BASE_CLASS}--${state}`
  )
 
  const handleChange = ev => {
    const {value, name} = ev.target
    onChange(ev, {value, name})
  }
 
  return <textarea {...props} onBlur={onBlur} onChange={handleChange} className={className} value={value} />
}
 
AtomTextarea.displayName = 'AtomTextarea'
 
AtomTextarea.propTypes = {
  /** Size of textarea: 'short', 'long' */
  size: PropTypes.oneOf(Object.values(TEXTAREA_SIZES)),
 
  /** Handler triggered on change */
  onChange: PropTypes.func,
 
  /** Handler triggered when losses focus */
  onBlur: PropTypes.func,
 
  /** Value (content) of the textarea */
  value: PropTypes.string,
 
  /** true = error, false = success, null = neutral */
  errorState: PropTypes.bool,
 
  /* Will set one of all allowed resizes. */
  resize: PropTypes.oneOf(Object.values(TEXTAREA_RESIZES)),
 
  /* Will set a red/green/orange border if set to 'error' / 'success' / 'alert' */
  state: PropTypes.oneOf(Object.values(TEXTAREA_STATES))
}
 
export default AtomTextarea
export {
  TEXTAREA_RESIZES as AtomTextareaResizes,
  TEXTAREA_SIZES as AtomTextareaSizes,
  TEXTAREA_STATES as AtomTextareaStates
}