text: "use strict";
  
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  
  exports.__esModule = true;
  exports.default = void 0;
  
  var _babelHelperVueJsxMergeProps = _interopRequireDefault(require("@vue/babel-helper-vue-jsx-merge-props"));
  
  var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  
  var _utils = require("./utils");
  
  var _event = require("../utils/dom/event");
  
  var _resetScroll = require("../utils/dom/reset-scroll");
  
  var _utils2 = require("../utils");
  
  var _icon = _interopRequireDefault(require("../icon"));
  
  var _cell = _interopRequireDefault(require("../cell"));
  
  var _shared = require("../cell/shared");
  
  // Utils
  // Components
  var _createNamespace = (0, _utils2.createNamespace)('field'),
      createComponent = _createNamespace[0],
      bem = _createNamespace[1];
  
  var _default = createComponent({
    inheritAttrs: false,
    provide: function provide() {
      return {
        vanField: this
      };
    },
    inject: {
      vanForm: {
        default: null
      }
    },
    props: (0, _extends2.default)({}, _shared.cellProps, {
      name: String,
      rules: Array,
      error: Boolean,
      disabled: Boolean,
      readonly: Boolean,
      autosize: [Boolean, Object],
      leftIcon: String,
      rightIcon: String,
      clearable: Boolean,
      formatter: Function,
      maxlength: [Number, String],
      labelWidth: [Number, String],
      labelClass: null,
      labelAlign: String,
      inputAlign: String,
      placeholder: String,
      errorMessage: String,
      errorMessageAlign: String,
      showWordLimit: Boolean,
      type: {
        type: String,
        default: 'text'
      }
    }),
    data: function data() {
      return {
        focused: false,
        validateMessage: ''
      };
    },
    watch: {
      value: function value() {
        this.resetValidation();
        this.validateWithTrigger('onChange');
        this.$nextTick(this.adjustSize);
      }
    },
    mounted: function mounted() {
      this.format();
      this.$nextTick(this.adjustSize);
  
      if (this.vanForm) {
        this.vanForm.addField(this);
      }
    },
    beforeDestroy: function beforeDestroy() {
      if (this.vanForm) {
        this.vanForm.removeField(this);
      }
    },
    computed: {
      showClear: function showClear() {
        return this.clearable && this.focused && this.value !== '' && (0, _utils2.isDef)(this.value) && !this.readonly;
      },
      showError: function showError() {
        if (this.vanForm && this.vanForm.showError && this.validateMessage) {
          return true;
        }
  
        return this.error;
      },
      listeners: function listeners() {
        var listeners = (0, _extends2.default)({}, this.$listeners, {
          input: this.onInput,
          keypress: this.onKeypress,
          focus: this.onFocus,
          blur: this.onBlur
        });
        delete listeners.click;
        return listeners;
      },
      labelStyle: function labelStyle() {
        var labelWidth = this.getProp('labelWidth');
  
        if (labelWidth) {
          return {
            width: (0, _utils2.addUnit)(labelWidth)
          };
        }
      },
      formValue: function formValue() {
        if (this.children && (this.$scopedSlots.input || this.$slots.input)) {
          return this.children.value;
        }
  
        return this.value;
      }
    },
    methods: {
      // @exposed-api
      focus: function focus() {
        if (this.$refs.input) {
          this.$refs.input.focus();
        }
      },
      // @exposed-api
      blur: function blur() {
        if (this.$refs.input) {
          this.$refs.input.blur();
        }
      },
      runValidator: function runValidator(value, rule) {
        return new Promise(function (resolve) {
          var returnVal = rule.validator(value, rule);
  
          if ((0, _utils2.isPromise)(returnVal)) {
            return returnVal.then(resolve);
          }
  
          resolve(returnVal);
        });
      },
      isEmptyValue: function isEmptyValue(value) {
        if (Array.isArray(value)) {
          return !value.length;
        }
  
        return !value;
      },
      runSyncRule: function runSyncRule(value, rule) {
        if (rule.required && this.isEmptyValue(value)) {
          return false;
        }
  
        if (rule.pattern && !rule.pattern.test(value)) {
          return false;
        }
  
        return true;
      },
      getRuleMessage: function getRuleMessage(value, rule) {
        var message = rule.message;
  
        if ((0, _utils2.isFunction)(message)) {
          return message(value, rule);
        }
  
        return message;
      },
      runRules: function runRules(rules) {
        var _this = this;
  
        return rules.reduce(function (promise, rule) {
          return promise.then(function () {
            if (_this.validateMessage) {
              return;
            }
  
            var value = _this.formValue;
  
            if (rule.formatter) {
              value = rule.formatter(value, rule);
            }
  
            if (!_this.runSyncRule(value, rule)) {
              _this.validateMessage = _this.getRuleMessage(value, rule);
              return;
            }
  
            if (rule.validator) {
              return _this.runValidator(value, rule).then(function (result) {
                if (result === false) {
                  _this.validateMessage = _this.getRuleMessage(value, rule);
                }
              });
            }
          });
        }, Promise.resolve());
      },
      validate: function validate(rules) {
        var _this2 = this;
  
        if (rules === void 0) {
          rules = this.rules;
        }
  
        return new Promise(function (resolve) {
          if (!rules) {
            resolve();
          }
  
          _this2.runRules(rules).then(function () {
            if (_this2.validateMessage) {
              resolve({
                name: _this2.name,
                message: _this2.validateMessage
              });
            } else {
              resolve();
            }
          });
        });
      },
      validateWithTrigger: function validateWithTrigger(trigger) {
        if (this.vanForm && this.rules) {
          var defaultTrigger = this.vanForm.validateTrigger === trigger;
          var rules = this.rules.filter(function (rule) {
            if (rule.trigger) {
              return rule.trigger === trigger;
            }
  
            return defaultTrigger;
          });
          this.validate(rules);
        }
      },
      resetValidation: function resetValidation() {
        if (this.validateMessage) {
          this.validateMessage = '';
        }
      },
      format: function format(target) {
        if (target === void 0) {
          target = this.$refs.input;
        }
  
        if (!target) {
          return;
        }
  
        var _target = target,
            value = _target.value;
        var maxlength = this.maxlength; // native maxlength not work when type is number
  
        if ((0, _utils2.isDef)(maxlength) && value.length > maxlength) {
          value = value.slice(0, maxlength);
          target.value = value;
        }
  
        if (this.type === 'number' || this.type === 'digit') {
          var originValue = value;
          var allowDot = this.type === 'number';
          value = (0, _utils.formatNumber)(value, allowDot);
  
          if (value !== originValue) {
            target.value = value;
          }
        }
  
        if (this.formatter) {
          var _originValue = value;
          value = this.formatter(value);
  
          if (value !== _originValue) {
            target.value = value;
          }
        }
  
        return value;
      },
      onInput: function onInput(event) {
        // not update v-model when composing
        if (event.target.composing) {
          return;
        }
  
        this.$emit('input', this.format(event.target));
      },
      onFocus: function onFocus(event) {
        this.focused = true;
        this.$emit('focus', event); // readonly not work in lagacy mobile safari
  
        /* istanbul ignore if */
  
        if (this.readonly) {
          this.blur();
        }
      },
      onBlur: function onBlur(event) {
        this.focused = false;
        this.$emit('blur', event);
        this.validateWithTrigger('onBlur');
        (0, _resetScroll.resetScroll)();
      },
      onClick: function onClick(event) {
        this.$emit('click', event);
      },
      onClickLeftIcon: function onClickLeftIcon(event) {
        this.$emit('click-left-icon', event);
      },
      onClickRightIcon: function onClickRightIcon(event) {
        this.$emit('click-right-icon', event);
      },
      onClear: function onClear(event) {
        (0, _event.preventDefault)(event);
        this.$emit('input', '');
        this.$emit('clear', event);
      },
      onKeypress: function onKeypress(event) {
        // trigger blur after click keyboard search button
  
        /* istanbul ignore next */
        if (this.type === 'search' && event.keyCode === 13) {
          this.blur();
        }
  
        this.$emit('keypress', event);
      },
      adjustSize: function adjustSize() {
        var input = this.$refs.input;
  
        if (!(this.type === 'textarea' && this.autosize) || !input) {
          return;
        }
  
        input.style.height = 'auto';
        var height = input.scrollHeight;
  
        if ((0, _utils2.isObject)(this.autosize)) {
          var _this$autosize = this.autosize,
              maxHeight = _this$autosize.maxHeight,
              minHeight = _this$autosize.minHeight;
  
          if (maxHeight) {
            height = Math.min(height, maxHeight);
          }
  
          if (minHeight) {
            height = Math.max(height, minHeight);
          }
        }
  
        if (height) {
          input.style.height = height + 'px';
        }
      },
      genInput: function genInput() {
        var h = this.$createElement;
        var type = this.type;
        var inputSlot = this.slots('input');
        var inputAlign = this.getProp('inputAlign');
  
        if (inputSlot) {
          return h("div", {
            "class": bem('control', [inputAlign, 'custom'])
          }, [inputSlot]);
        }
  
        var inputProps = {
          ref: 'input',
          class: bem('control', inputAlign),
          domProps: {
            value: this.value
          },
          attrs: (0, _extends2.default)({}, this.$attrs, {
            name: this.name,
            disabled: this.disabled,
            readonly: this.readonly,
            placeholder: this.placeholder
          }),
          on: this.listeners,
          // add model directive to skip IME composition
          directives: [{
            name: 'model',
            value: this.value
          }]
        };
  
        if (type === 'textarea') {
          return h("textarea", (0, _babelHelperVueJsxMergeProps.default)([{}, inputProps]));
        }
  
        var inputType = type;
        var inputMode; // type="number" is weired in iOS, and can't prevent dot in Android
        // so use inputmode to set keyboard in mordern browers
  
        if (type === 'number') {
          inputType = 'text';
          inputMode = 'decimal';
        }
  
        if (type === 'digit') {
          inputType = 'tel';
          inputMode = 'numeric';
        }
  
        return h("input", (0, _babelHelperVueJsxMergeProps.default)([{
          "attrs": {
            "type": inputType,
            "inputmode": inputMode
          }
        }, inputProps]));
      },
      genLeftIcon: function genLeftIcon() {
        var h = this.$createElement;
        var showLeftIcon = this.slots('left-icon') || this.leftIcon;
  
        if (showLeftIcon) {
          return h("div", {
            "class": bem('left-icon'),
            "on": {
              "click": this.onClickLeftIcon
            }
          }, [this.slots('left-icon') || h(_icon.default, {
            "attrs": {
              "name": this.leftIcon,
              "classPrefix": this.iconPrefix
            }
          })]);
        }
      },
      genRightIcon: function genRightIcon() {
        var h = this.$createElement;
        var slots = this.slots;
        var showRightIcon = slots('right-icon') || this.rightIcon;
  
        if (showRightIcon) {
          return h("div", {
            "class": bem('right-icon'),
            "on": {
              "click": this.onClickRightIcon
            }
          }, [slots('right-icon') || h(_icon.default, {
            "attrs": {
              "name": this.rightIcon,
              "classPrefix": this.iconPrefix
            }
          })]);
        }
      },
      genWordLimit: function genWordLimit() {
        var h = this.$createElement;
  
        if (this.showWordLimit && this.maxlength) {
          var count = this.value.length;
          var full = count >= this.maxlength;
          return h("div", {
            "class": bem('word-limit')
          }, [h("span", {
            "class": bem('word-num', {
              full: full
            })
          }, [count]), "/", this.maxlength]);
        }
      },
      genMessage: function genMessage() {
        var h = this.$createElement;
  
        if (this.vanForm && this.vanForm.showErrorMessage === false) {
          return;
        }
  
        var message = this.errorMessage || this.validateMessage;
  
        if (message) {
          var errorMessageAlign = this.getProp('errorMessageAlign');
          return h("div", {
            "class": bem('error-message', errorMessageAlign)
          }, [message]);
        }
      },
      getProp: function getProp(key) {
        if ((0, _utils2.isDef)(this[key])) {
          return this[key];
        }
  
        if (this.vanForm && (0, _utils2.isDef)(this.vanForm[key])) {
          return this.vanForm[key];
        }
      },
      genLabel: function genLabel() {
        var h = this.$createElement;
        var colon = this.getProp('colon') ? ':' : '';
  
        if (this.slots('label')) {
          return [this.slots('label'), colon];
        }
  
        if (this.label) {
          return h("span", [this.label + colon]);
        }
      }
    },
    render: function render() {
      var _bem;
  
      var h = arguments[0];
      var slots = this.slots;
      var labelAlign = this.getProp('labelAlign');
      var scopedSlots = {
        icon: this.genLeftIcon
      };
      var Label = this.genLabel();
  
      if (Label) {
        scopedSlots.title = function () {
          return Label;
        };
      }
  
      return h(_cell.default, {
        "attrs": {
          "icon": this.leftIcon,
          "size": this.size,
          "center": this.center,
          "border": this.border,
          "isLink": this.isLink,
          "required": this.required,
          "clickable": this.clickable,
          "titleStyle": this.labelStyle,
          "valueClass": bem('value'),
          "titleClass": [bem('label', labelAlign), this.labelClass],
          "arrowDirection": this.arrowDirection
        },
        "scopedSlots": scopedSlots,
        "class": bem((_bem = {
          error: this.showError
        }, _bem["label-" + labelAlign] = labelAlign, _bem['min-height'] = this.type === 'textarea' && !this.autosize, _bem)),
        "on": {
          "click": this.onClick
        }
      }, [h("div", {
        "class": bem('body')
      }, [this.genInput(), this.showClear && h(_icon.default, {
        "attrs": {
          "name": "clear"
        },
        "class": bem('clear'),
        "on": {
          "touchstart": this.onClear
        }
      }), this.genRightIcon(), slots('button') && h("div", {
        "class": bem('button')
      }, [slots('button')])]), this.genWordLimit(), this.genMessage()]);
    }
  });
  
  exports.default = _default;