All files / atom/input/src/Input/Component index.js

100% Statements 16/16
94.44% Branches 17/18
100% Functions 3/3
100% Lines 16/16

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 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                    1x                                                                                             47x     22x 22x     47x     38x 38x   38x 37x 37x 35x 2x 1x         47x                       47x                                                                               1x                                                                                                                                                                          
import {forwardRef} from 'react'
 
import PropTypes from 'prop-types'
 
import useMergeRefs from '@s-ui/react-hooks/lib/useMergeRefs'
import PolymorphicElement from '@s-ui/react-primitive-polymorphic-element'
 
import {getClassNames, INPUT_SHAPES, INPUT_STATES, noop, SIZES} from '../../config.js'
import isValidInputValue from '../../helpers/isValidInputValue.js'
 
const Input = forwardRef(
  (
    {
      as = 'input',
      disabled,
      readOnly,
      hideInput,
      noBorder,
      id,
      name,
      onBlur,
      onFocus,
      onPaste,
      onCopy,
      placeholder,
      reference,
      size,
      errorState,
      state,
      type,
      value,
      charsSize,
      tabIndex,
      ariaLabel,
      maxLength,
      minLength,
      defaultValue,
      min,
      max,
      step,
      autoComplete,
      autoFocus,
      onChange = noop,
      onEnter = noop,
      onEnterKey = 'Enter',
      onKeyDown = noop,
      onKeyPress = noop,
      required,
      pattern,
      inputMode,
      shape,
      children,
      className: classNameProp,
      ...props
    },
    forwardedRef
  ) => {
    const changeHandler = ev => {
      const {
        target: {value, name}
      } = ev
      onChange(ev, {value, name})
    }
 
    const handleKeyDown = ev => {
      const {
        target: {value, name}
      } = ev
      const {key} = ev
 
      if (isValidInputValue(event, {type, onEnterKey})) {
        onKeyDown(ev, {value, name})
        if (typeof onEnterKey === 'string') {
          key === onEnterKey && onEnter(ev, {value, name})
        } else if (Array.isArray(onEnterKey)) {
          onEnterKey.includes(key) && onEnter(ev, {value, name})
        }
      }
    }
 
    const className = getClassNames({
      size,
      charsSize,
      hideInput,
      noBorder,
      readOnly,
      errorState,
      state,
      shape,
      className: classNameProp
    })
 
    return (
      <PolymorphicElement
        as={as}
        className={className}
        tabIndex={tabIndex}
        aria-label={ariaLabel}
        disabled={disabled}
        readOnly={readOnly}
        id={id}
        name={name}
        onChange={changeHandler}
        onFocus={onFocus}
        onPaste={onPaste}
        onCopy={onCopy}
        onBlur={onBlur}
        onKeyDown={handleKeyDown}
        onKeyPress={onKeyPress}
        placeholder={placeholder}
        ref={useMergeRefs(...[reference, forwardedRef].filter(Boolean))}
        type={type}
        value={value}
        size={charsSize}
        defaultValue={defaultValue}
        maxLength={maxLength}
        minLength={minLength}
        max={max}
        min={min}
        step={step}
        autoComplete={autoComplete}
        autoFocus={autoFocus}
        required={required}
        pattern={pattern}
        inputMode={inputMode}
        children={as === 'input' ? undefined : children}
        {...props}
      />
    )
  }
)
 
Input.propTypes = {
  /* Render the passed value as the correspondent HTML tag or the component if a function is passed */
  as: PropTypes.elementType,
  /* This Boolean attribute prevents the user from interacting with the input */
  disabled: PropTypes.bool,
  /* This Boolean attribute prevents the user from interacting with the input but without disabled styles */
  readOnly: PropTypes.bool,
  /* The DOM id global attribute. */
  id: PropTypes.string,
  /* sets the name property of an element in the DOM */
  name: PropTypes.string,
  /* onBlur callback */
  onBlur: PropTypes.func,
  /* onKeyDown callback */
  onKeyDown: PropTypes.func,
  /* onKeyPress callback */
  onKeyPress: PropTypes.func,
  /* onChange callback */
  onChange: PropTypes.func,
  /* onFocus callback */
  onFocus: PropTypes.func,
  /* onPaste callback */
  onPaste: PropTypes.func,
  /* onCopy callback */
  onCopy: PropTypes.func,
  /* onEnter callback */
  onEnter: PropTypes.func,
  /* key(s) to provoke the onEnter callback. Valid any value defined here → https://www.w3.org/TR/uievents-key/#named-key-attribute-values */
  onEnterKey: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
  /* A hint to the user of what can be entered in the control. The placeholder text must not contain carriage returns or line-feeds. */
  placeholder: PropTypes.string,
  /* 's' or 'm', default: 'm' */
  size: PropTypes.oneOf(Object.values(SIZES)),
  /* width of input based in number of characters (native "size" attribute) */
  charsSize: PropTypes.number,
  /* specifies the maximum number of characters (native "maxlength" attribute) */
  maxLength: PropTypes.number,
  /* specifies the minimum number of characters (native "minlength" attribute) */
  minLength: PropTypes.number,
  /* specifies the maximum number allowed (native "max" attribute) */
  max: PropTypes.number,
  /* specifies the minimum number allowed (native "min" attribute) */
  min: PropTypes.number,
  /** stepping interval to use when using up and down arrows to adjust the value, as well as for validation (native "step" attribute) */
  step: PropTypes.number,
  /** specifies whether or not an input field should have autocomplete enabled (on|off) */
  autoComplete: PropTypes.string,
  /** native autofocus attribute */
  autoFocus: PropTypes.bool,
  /* text, password, date or number */
  type: PropTypes.string,
  /* value of the control */
  value: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]),
  /* default value of the control */
  defaultValue: PropTypes.string,
  /* react ref to access DOM node */
  reference: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
  /** Wether to show the input or not */
  hideInput: PropTypes.bool,
  /* Will set a red/green border if set to true/false */
  errorState: PropTypes.bool,
  /* Will set a red/green/orange border if set to 'error' / 'success' / 'alert' */
  state: PropTypes.oneOf(Object.values(INPUT_STATES)),
  /** Whether to hide the input border or not */
  noBorder: PropTypes.bool,
  /** tabindex value */
  tabIndex: PropTypes.number,
  /* Native aria-label attribute for a11y */
  ariaLabel: PropTypes.string,
  /** native required attribtue  */
  required: PropTypes.bool,
  /** native pattern attribute */
  pattern: PropTypes.string,
  /** To select input keyboard mode on mobile. It can be 'numeric', 'decimal', 'email', etc */
  inputMode: PropTypes.string,
  /** Sets the shape of the input field. It can be 'rounded', 'square' or 'circle' */
  shape: PropTypes.oneOf(Object.values(INPUT_SHAPES)),
  /** Nodes to be rendered inside the component */
  children: PropTypes.node,
  /** CSS class to be added to the button */
  className: PropTypes.string
}
 
export default Input
export {SIZES as inputSizes, INPUT_STATES as inputStates}