{"size": 3427, "ext": "js", "max_stars_repo_path": "src/components/Input/Input.js", "max_stars_repo_name": "yonui/yonui-ys", "max_stars_repo_stars_event_min_datetime": "2020-03-09T05:51:43.000Z", "max_stars_repo_stars_event_max_datetime": "2020-09-21T08:17:03.000Z", "max_issues_repo_path": "src/components/Input/Input.js", "max_issues_repo_name": "yonui/yonui-ys", "max_issues_repo_issues_event_min_datetime": "2020-07-03T03:45:47.000Z", "max_issues_repo_issues_event_max_datetime": "2020-07-03T03:45:47.000Z", "max_forks_repo_path": "src/components/Input/Input.js", "max_forks_repo_name": "yonui/yonui-ys", "max_forks_repo_forks_event_min_datetime": "2020-07-02T09:03:10.000Z", "max_forks_repo_forks_event_max_datetime": "2021-06-01T09:23:07.000Z"}
{"max_stars_count": 6, "max_issues_count": 1, "max_forks_count": 5, "avg_line_length": 23.0, "max_line_length": 116, "alphanum_fraction": 0.5800992121}
import React, { Component } from 'react'
import PropTypes from 'prop-types'

import FormControl from 'bee-form-control'
import InputGroup from 'bee-input-group'
import omit from 'omit.js'

import TextArea from './TextArea'
import Search from './Search'

const sizeMap = {
  small: 'sm',
  large: 'lg',
  default: 'md'
}

const defaultProps = {
  size: 'default',
  type: 'text'
}

const propsType = {
  size: PropTypes.string
}

class InputAdapter extends Component {
  constructor (props) {
    super(props)
    this.state = {
      value: this.props.value || this.props.defaultValue || ''
    }
  }

  componentWillReceiveProps (nextProps) {
    if (nextProps.value !== this.state.value) {
      this.setState({
        value: nextProps.value
      })
    }
  }

  focus = () => {
    this.inputRef.input.focus()
  }

  blur = () => {
    this.inputRef.input.blur()
  }

  handleKeyDown = (e) => {
    const { onPressEnter, onKeyDown } = this.props
    if (e.keyCode === 13 && onPressEnter) {
      onPressEnter(e)
    }
    if (onKeyDown) {
      onKeyDown(e)
    }
  }

  handleFocus = (val, e) => {
    this.props.onFocus && this.props.onFocus(e)
  }

  handleBlur = (val, e, clearOpt) => {
    this.props.onBlur && this.props.onBlur(e, clearOpt)
  }

  handleClick = (val, e) => {
    this.props.onClick && this.props.onClick(e)
  }

  handleChange = (val, e) => {
    this.props.onChange && this.props.onChange(e)
    if (val !== this.state.value) {
      this.setState({ value: val })
    }
  }

  render () {
    let { defaultValue, disabled, size, placeholder, addonBefore, addonAfter, suffix, type, ...others } = this.props
    const { value } = this.state

    // 根据withAdon 来判断是不是需要用 InputGroup
    const withAddon = addonAfter || addonBefore

    let { componentClass } = this.props
    componentClass = componentClass || 'input'

    if (type === 'textarea') {
      componentClass = type
      type = 'text'
    }

    const formControl = (
      <FormControl
        {...omit(others, ['value', 'defaultValue'])}
        type={type}
        disabled={disabled}
        ref={(e) => { this.inputRef = e }}
        placeholder={placeholder}
        value={value}
        defaultValue={defaultValue}
        size={sizeMap[size]}
        onKeyDown={this.handleKeyDown}
        onChange={this.handleChange}
        onClick={this.handleClick}
        onBlur={this.handleBlur}
        onFocus={this.handleFocus}
        componentClass={componentClass}
        title={type !== 'password' && (value || placeholder)}
      />
    )

    if (suffix || withAddon) {
      // 目前 suffix 主要用于处理参照的 icon
      return (
        <InputGroup simple={!!suffix} className={disabled ? 'disabled' : ''}>
          {
            withAddon && addonBefore && <InputGroup.Addon>{addonBefore}</InputGroup.Addon>
          }
          {formControl}
          {
            suffix && (
              <InputGroup.Button
                shape='border'
                onClick={this.handleClick}
              >
                {suffix}
              </InputGroup.Button>
            )
          }
          {
            withAddon && addonAfter && <InputGroup.Addon>{addonAfter}</InputGroup.Addon>
          }
        </InputGroup>
      )
    }
    return formControl
  }
}

InputAdapter.TextArea = TextArea
InputAdapter.Search = Search

InputAdapter.defaultProps = defaultProps
InputAdapter.propsType = propsType

export default InputAdapter
