function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

/*
Copyright (c) 2018-2019 Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { StyledInput, StyledInputSizer } from './styled-components.js';
import { getOverrides } from '../helpers/overrides.js';
export default class AutosizeInput extends React.Component {
  constructor(...args) {
    super(...args);

    _defineProperty(this, "mounted", void 0);

    _defineProperty(this, "sizer", void 0);

    _defineProperty(this, "state", {
      inputWidth: 5
    });

    _defineProperty(this, "sizerRef", el => {
      this.sizer = el;
    });
  }

  componentDidMount() {
    this.mounted = true;
    this.updateInputWidth();
  }

  componentDidUpdate(prevProps, prevState) {
    this.updateInputWidth();
  }

  componentWillUnmount() {
    this.mounted = false;
  }

  updateInputWidth() {
    if (!this.mounted || !this.sizer || typeof this.sizer.scrollWidth === 'undefined') {
      return;
    }

    const newInputWidth = this.sizer.scrollWidth + 2;

    if (newInputWidth !== this.state.inputWidth && this.sizer.scrollWidth !== this.state.inputWidth) {
      this.setState({
        inputWidth: newInputWidth
      });
    }
  }

  render() {
    const {
      overrides = {},
      inputRef,
      ...restProps
    } = this.props;
    const [Input, inputProps] = getOverrides(overrides.Input, StyledInput);
    const sizerValue = [this.props.defaultValue, this.props.value, ''].reduce((previousValue, currentValue) => {
      if (previousValue !== null && previousValue !== undefined) {
        return previousValue;
      }

      return currentValue;
    });
    const componentInputProps = { ...restProps,
      $width: `${this.state.inputWidth}px`
    };
    return React.createElement(React.Fragment, null, React.createElement(Input, _extends({}, componentInputProps, {
      ref: inputRef
    }, inputProps)), React.createElement(StyledInputSizer, {
      $size: this.props.$size,
      ref: this.sizerRef
    }, sizerValue));
  }

}

_defineProperty(AutosizeInput, "defaultProps", {
  inputRef: React.createRef(),
  value: '',
  overrides: {}
});