Skip to content

Button 按钮

常用的操作按钮。

基础用法

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

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

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

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

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

禁用状态

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

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

图标按钮

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

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

按钮组

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

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

加载状态

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

TIP

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

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

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

按钮尺寸

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

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

Tag

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

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

节流模式

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

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

<template>
  <p>
    <x-button @click="handleBtnClick"> with throttle</x-button>
    <x-button :use-throttle="false" @click="handleBtnClick"
      >without throttle</x-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