简述
button组件源码很容易阅读,css部分就不分析了,直接上源码。
参数
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| size | 尺寸 | string | medium / small / mini | — |
| type | 类型 | string | primary / success / warning / danger / info / text | — |
| plain | 是否朴素按钮 | boolean | — | false |
| round | 是否圆角按钮 | boolean | — | false |
| circle | 是否圆形按钮 | boolean | — | false |
| loading | 是否加载中状态 | boolean | — | false |
| disabled | 是否禁用状态 | boolean | — | false |
| icon | 图标类名 | string | — | — |
| autofocus | 是否默认聚焦 | boolean | — | false |
| native-type | 原生 type 属性 | string | button / submit / reset | button |
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>