import '../../../examples/assets/styles/fonts/style.css';
<!--
 * @Description  : button
 * @Author       : luqing
 * @Date         : 2020-10-29 14:46:25
 * @LastEditors: mayako
 * @LastEditTime: 2021-11-08 16:49:12
-->
<template>
  <button
    :style="iconStyle"
    class="el-button"
    @click="handleClick"
    :disabled="buttonDisabled || loading"
    :autofocus="autofocus"
    :type="nativeType"
    :class="[
      type ? 'el-button--' + type : '',
      buttonSize ? 'el-button--' + buttonSize : '',
      {
        'is-disabled': buttonDisabled,
        'is-loading': loading,
        'is-plain': plain,
        'is-round': round,
        'is-circle': circle
      }
    ]"
  >
    <i class="el-icon-loading" v-if="loading"></i>
    <i :class="icon" v-if="icon && !loading"></i>
    <span v-if="$slots.default"><slot></slot></span>
  </button>
</template>
<script>
  export default {
    name: 'ElButton',
    inject: {
      elForm: {
        default: ''
      },
      elFormItem: {
        default: ''
      }
    },
    props: {
      type: {
        type: String,
        default: 'default'
      },
      size: String,
      icon: {
        type: String,
        default: ''
      },
      nativeType: {
        type: String,
        default: 'button'
      },
      loading: Boolean,
      disabled: Boolean,
      plain: Boolean,
      autofocus: Boolean,
      round: Boolean,
      circle: Boolean
    },

    computed: {
      _elFormItemSize() {
        return (this.elFormItem || {}).elFormItemSize;
      },
      buttonSize() {
        return this.size || this._elFormItemSize || (this.$ELEMENT || {}).size;
      },
      buttonDisabled() {
        return this.disabled || (this.elForm || {}).disabled;
      },
      iconStyle() {
        // 判断右侧是否有 icon
        let haveRightIcon = false;
        if (this.$slots.default) {
          let rightElArr = this.$slots.default;
          rightElArr.forEach(el => {
            if (!el.tag) return;
            if (el.tag === 'i') {
              haveRightIcon = true;
            }
          });
        }
        // 如果左侧有 icon 或者 右侧有 icon
        if (this.icon || haveRightIcon) {
          let style = {};
          if (this.$parent) {
            let $elArray = [].concat(this.$parent.$children);
            if (!$elArray.length) { return false; }
            style = {};
            style.paddingLeft = 8 + 'px';
            style.paddingRight = 8 + 'px';
          }
          return style;
        }
      }
    },
    methods: {
      handleClick(evt) {
        this.$emit('click', evt);
      }
    }
  };
</script>
