elementUI-button

819 阅读1分钟

简述

button组件源码很容易阅读,css部分就不分析了,直接上源码。

参数

参数说明类型可选值默认值
size尺寸stringmedium / small / mini
type类型stringprimary / success / warning / danger / info / text
plain是否朴素按钮booleanfalse
round是否圆角按钮booleanfalse
circle是否圆形按钮booleanfalse
loading是否加载中状态booleanfalse
disabled是否禁用状态booleanfalse
icon图标类名string
autofocus是否默认聚焦booleanfalse
native-type原生 type 属性stringbutton / submit / resetbutton

size

elFormItem是用在From组件中时,通过inject获取elFormItem组件中的参数。

size的优先级是this.size、this._elFormItemSize、(this.$ELEMENT || {}).size;

:class="[
  buttonSize ? 'el-button--' + buttonSize : '',
]"

props: {
    size: String,
},

computed: {
  _elFormItemSize() {
    return (this.elFormItem || {}).elFormItemSize;
  },
  buttonSize() {
    return this.size || this._elFormItemSize || (this.$ELEMENT || {}).size;
  }
},

type、plain、round、circle

通过class控制各种样式

:class="[
  type ? 'el-button--' + type : '',
  {
    'is-plain': plain,
    'is-round': round,
    'is-circle': circle
  }
]"
props: {
  type: {
    type: String,
    default: 'default'
  },
  plain: Boolean,
  round: Boolean,
  circle: Boolean
},

loading

loading要控制disabled、样式、loading的icon和正常的icon相互替换

<button
    :disabled="buttonDisabled || loading"
    :class="[
      {
        'is-loading': loading,
      }
    ]"
>
    <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>
props: {
  loading: Boolean,
},

disabled

控制disabled、样式

<button
    :disabled="buttonDisabled || loading"
    :class="[
      {
        'is-disabled': buttonDisabled,
      }
    ]"
  >
    <span v-if="$slots.default"><slot></slot></span>
</button>
props: {
  disabled: Boolean,
},
computed: {
  buttonDisabled() {
    return this.disabled || (this.elForm || {}).disabled;
  }
},

icon

icon展示图标、图标位置

<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>
props: {
  icon: {
    type: String,
    default: ''
  },
},

autofocus

和属性autofocus完全一样

native-type

button的type属性:button / submit / reset

@click点击事件

<button
    @click="handleClick"
>
    <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>
methods: {
  handleClick(evt) {
    this.$emit('click', evt);
  }
}

全部源码(不含样式)

<template>
  <button
    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;
      }
    },

    methods: {
      handleClick(evt) {
        this.$emit('click', evt);
      }
    }
  };
</script>