Skip to content

Button 按钮

常用的操作按钮。

基础用法

使用 typeplainroundcircle来定义按钮的样式。

<template>
    <p>
        <ec-button>Default</ec-button>
        <ec-button type="primary">Primary</ec-button>
        <ec-button type="success">Success</ec-button>
        <ec-button type="info">Info</ec-button>
        <ec-button type="warning">Warning</ec-button>
        <ec-button type="danger">Danger</ec-button>
    </p>

    <p>
        <ec-button plain>Plain</ec-button>
        <ec-button type="primary" plain>Primary</ec-button>
        <ec-button type="success" plain>Success</ec-button>
        <ec-button type="info" plain>Info</ec-button>
        <ec-button type="warning" plain>Warning</ec-button>
        <ec-button type="danger" plain>Danger</ec-button>
    </p>

    <p>
        <ec-button round>Round</ec-button>
        <ec-button type="primary" round>Primary</ec-button>
        <ec-button type="success" round>Success</ec-button>
        <ec-button type="info" round>Info</ec-button>
        <ec-button type="warning" round>Warning</ec-button>
        <ec-button type="danger" round>Danger</ec-button>
    </p>

    <p>
        <ec-button icon="search" circle />
        <ec-button type="primary" icon="edit" circle />
        <ec-button type="success" icon="check" circle />
        <ec-button type="info" icon="message" circle />
        <ec-button type="warning" icon="star" circle />
        <ec-button type="danger" icon="trash" circle />
    </p>
</template>

禁用状态

你可以使用 disabled 属性来定义按钮是否被禁用。

<template>
    <p>
        <ec-button>Default</ec-button>
        <ec-button type="primary">Primary</ec-button>
        <ec-button type="success">Success</ec-button>
        <ec-button type="info">Info</ec-button>
        <ec-button type="warning">Warning</ec-button>
        <ec-button type="danger">Danger</ec-button>
    </p>
    <p>
        <ec-button disabled>Default</ec-button>
        <ec-button type="primary" disabled>Primary</ec-button>
        <ec-button type="success" disabled>Success</ec-button>
        <ec-button type="info" disabled>Info</ec-button>
        <ec-button type="warning" disabled>Warning</ec-button>
        <ec-button type="danger" disabled>Danger</ec-button>
    </p>
</template>

图标按钮

使用 icon属性来定义按钮的图标。

<template>
    <div>
        <ec-button type="primary" icon="edit" />
        <ec-button type="primary" icon="share" />
        <ec-button type="primary" icon="trash" />
        <ec-button type="primary" icon="search">Search</ec-button>
        <ec-button type="primary">
            Upload<ec-icon icon="upload" style="margin-left: 8px" />
        </ec-button>
    </div>
</template>

按钮组

使用 <ec-button-group> 对多个按钮分组。

<template>
    <p>
        <ec-button-group disabled>
            <ec-button type="primary" icon="arrow-left">Previous Page</ec-button>
            <ec-button type="primary">
                Next Page<ec-icon icon="arrow-right" style="margin-left: 8px" />
            </ec-button>
        </ec-button-group>
    </p>
    <p>
        <ec-button-group type="warning" size="small">
            <ec-button type="primary" icon="edit" round />
            <ec-button type="primary" icon="share" />
            <ec-button type="primary" icon="trash" round />
        </ec-button-group>
    </p>
</template>

加载状态

使用 loading属性来定义按钮的加载状态。

TIP

您可以使用 loading 插槽或 loadingIcon 属性自定义您的 loading 图标

ps: loading 插槽优先级高于 loadingIcon 属性

<template>
    <div>
        <ec-button type="primary" loading>Loading</ec-button>
        <ec-button type="primary" loading-icon="circle-notch" loading>Loading</ec-button>
        <ec-button type="primary" loading>
            <template #loading>
                <ec-icon style="margin-right: 6px" icon="star" spin />
            </template>
            Loading
        </ec-button>
    </div>
</template>

按钮尺寸

使用 size属性来定义按钮的尺寸。

<template>
    <p>
        <ec-button size="large">Large</ec-button>
        <ec-button>Default</ec-button>
        <ec-button size="small">Small</ec-button>
        <ec-button size="large" icon="search">Search</ec-button>
        <ec-button icon="search">Search</ec-button>
        <ec-button size="small" icon="search">Search</ec-button>
    </p>
    <p>
        <ec-button size="large" round>Large</ec-button>
        <ec-button round>Default</ec-button>
        <ec-button size="small" round>Small</ec-button>
        <ec-button size="large" icon="search" round>Search</ec-button>
        <ec-button icon="search" round>Search</ec-button>
        <ec-button size="small" icon="search" round>Search</ec-button>
    </p>
    <p>
        <ec-button size="large" icon="search" circle />
        <ec-button icon="search" circle />
        <ec-button size="small" icon="search" circle />
    </p>
</template>

Tag

可以自定义元素标签。例如,按钮,div,路由链接,nuxt 链接。

div
a

<template>
    <ec-button>button</ec-button>
    <ec-button tag="div" role="button" tabindex="0">div</ec-button>
    <ec-button type="primary" tag="a" href="https://github.com/EricWXY/eric-ui" target="_blank"
        rel="noopener noreferrer">
        a
    </ec-button>
</template>

节流模式

可以通过 useThrottle 属性来定义按钮是否使用节流模式 默认为 true。

<script setup>
function handleBtnClick() {
    console.log("btn click");
}
</script>

<template>
    <p>
        <ec-button @click="handleBtnClick"> with throttle</ec-button>
        <ec-button :use-throttle="false" @click="handleBtnClick">without throttle</ec-button>
    </p>
</template>

Button API

Props

NameDescriptionTypeDefault
size尺寸enum - 'large'| 'default'| 'small'
type类型enum - 'primary'| 'success'| 'warning'| 'danger'| 'info'info
plain是否为朴素按钮booleanfalse
round是否为圆角按钮booleanfalse
circle是否为圆形按钮booleanfalse
loading是否为加载中状态booleanfalse
loading-icon自定义加载中状态图标组件stringspinner
disabled按钮是否为禁用状态booleanfalse
icon按钮图标string-
autofocus是否自动聚焦(原生autofocus属性)booleanfalse
native-type原生 type 属性enum - 'button'| 'submit'| 'reset'button
tag自定义元素标签string/Componentbutton
use-throttle是否使用节流模式booleantrue
throttle-duration节流模式下,节流时间间隔(ms)number500

Events

NameDescriptionType
click按钮点击事件(event: MouseEvent)=> void

Slots

NameDescription
default默认插槽, 按钮内容
loading自定义加载图标

Expose

NameDescriptionType
ref按钮 html 元素Ref<HTMLButtonElement>
size按钮尺寸ComputedRef<''|'small' |'large'>
type按钮类型ComputedRef<''|'primary' |...>
disabled按钮禁用状态ComputedRef<boolean>

ButtonGroup API

Props

NameDescriptionTypeDefault
size尺寸enum - 'large'| 'default'| 'small'
type类型enum - 'primary'| 'success'| 'warning'| 'danger'| 'info'info
disabled按钮组是否为禁用状态booleanfalse

Slots

NameDescriptionSub Component
default默认插槽Button