import { r as registerInstance, c as createEvent, h, g as getElement } from './index-c948674f.js';

const indexCss = "taro-textarea-core{display:block;width:300px}taro-textarea-core .auto-height{height:auto}.taro-textarea{display:block;position:relative;border:0;width:100%;height:inherit;-webkit-appearance:none;-moz-appearance:none;appearance:none;cursor:auto;line-height:1.5}.taro-textarea:focus{outline:none}";

function fixControlledValue(value) {
  return value !== null && value !== void 0 ? value : '';
}
const Textarea = class {
  constructor(hostRef) {
    registerInstance(this, hostRef);
    this.onInput = createEvent(this, "input", 7);
    this.onFocus = createEvent(this, "focus", 7);
    this.onBlur = createEvent(this, "blur", 7);
    this.onChange = createEvent(this, "change", 7);
    this.onLineChange = createEvent(this, "linechange", 7);
    this.handleInput = (e) => {
      e.stopPropagation();
      this.handleLineChange();
      const value = e.target.value || '';
      this.onInput.emit({
        value,
        cursor: value.length
      });
    };
    this.handleFocus = (e) => {
      e.stopPropagation();
      this.onFocus.emit({
        value: e.target.value
      });
    };
    this.handleBlur = (e) => {
      e.stopPropagation();
      this.onBlur.emit({
        value: e.target.value
      });
    };
    this.handleChange = (e) => {
      e.stopPropagation();
      this.onChange.emit({
        value: e.target.value
      });
    };
    this.handleLineChange = () => {
      const line = this.getNumberOfLines();
      if (line !== this.line) {
        this.line = line;
        this.onLineChange.emit({
          height: this.textareaRef.clientHeight,
          lineCount: this.line
        });
      }
    };
    this.calculateContentHeight = (ta, scanAmount) => {
      let origHeight = ta.style.height, height = ta.offsetHeight, scrollHeight = ta.scrollHeight, overflow = ta.style.overflow, originMinHeight = ta.style.minHeight || null;
      /// only bother if the ta is bigger than content
      if (height >= scrollHeight) {
        ta.style.minHeight = 0;
        /// check that our browser supports changing dimension
        /// calculations mid-way through a function call...
        ta.style.height = height + scanAmount + 'px';
        /// because the scrollbar can cause calculation problems
        ta.style.overflow = 'hidden';
        /// by checking that scrollHeight has updated
        if (scrollHeight < ta.scrollHeight) {
          /// now try and scan the ta's height downwards
          /// until scrollHeight becomes larger than height
          while (ta.offsetHeight >= ta.scrollHeight) {
            ta.style.height = (height -= scanAmount) + 'px';
          }
          /// be more specific to get the exact height
          while (ta.offsetHeight < ta.scrollHeight) {
            ta.style.height = height++ + 'px';
          }
          /// reset the ta back to it's original height
          ta.style.height = origHeight;
          /// put the overflow back
          ta.style.overflow = overflow;
          ta.style.minHeight = originMinHeight;
          return height;
        }
      }
      else {
        return scrollHeight;
      }
    };
    this.getNumberOfLines = () => {
      const ta = this.textareaRef, style = window.getComputedStyle ? window.getComputedStyle(ta) : ta.style, 
      // This will get the line-height only if it is set in the css,
      // otherwise it's "normal"
      taLineHeight = parseInt(style.lineHeight, 10), 
      // Get the scroll height of the textarea
      taHeight = this.calculateContentHeight(ta, taLineHeight), 
      // calculate the number of lines
      numberOfLines = Math.floor(taHeight / taLineHeight);
      return numberOfLines;
    };
    this.value = undefined;
    this.placeholder = undefined;
    this.disabled = false;
    this.maxlength = 140;
    this.autoFocus = false;
    this.autoHeight = false;
    this.name = undefined;
    this.nativeProps = {};
    this.line = 1;
  }
  focus() {
    this.textareaRef.focus();
  }
  componentDidLoad() {
    Object.defineProperty(this.el, 'value', {
      get: () => this.textareaRef.value,
      set: value => (this.value = value),
      configurable: true
    });
    this.autoFocus && this.textareaRef.focus();
  }
  render() {
    const { value, placeholder, disabled, maxlength, autoFocus, autoHeight, name, nativeProps, handleInput, handleFocus, handleBlur, handleChange } = this;
    const otherProps = {};
    if (autoHeight) {
      otherProps.rows = this.line;
    }
    return (h("textarea", Object.assign({ ref: input => {
        if (input) {
          this.textareaRef = input;
        }
      }, class: `taro-textarea ${autoHeight ? 'auto-height' : ''}`, value: fixControlledValue(value), placeholder: placeholder, name: name, disabled: disabled, maxlength: maxlength, autofocus: autoFocus, onInput: handleInput, onFocus: handleFocus, onBlur: handleBlur, onChange: handleChange }, nativeProps, otherProps)));
  }
  get el() { return getElement(this); }
};
Textarea.style = indexCss;

export { Textarea as taro_textarea_core };

//# sourceMappingURL=taro-textarea-core.entry.js.map