Button 按钮
常用的操作按钮。
基础用法
使用 type
、plain
、 round
和 circle
来定义按钮的样式。
<template>
<p>
<px-button>Button</px-button>
<px-button type="primary">Primary</px-button>
<px-button type="success">Success</px-button>
<px-button type="warning">Warning</px-button>
<px-button type="danger">Danger</px-button>
</p>
<p>
<px-button plain>Plain</px-button>
<px-button type="primary" plain>Primary</px-button>
<px-button type="success" plain>Success</px-button>
<px-button type="warning" plain>Warning</px-button>
<px-button type="danger" plain>Danger</px-button>
</p>
<p>
<px-button round>Round</px-button>
<px-button type="primary" round>Primary</px-button>
<px-button type="success" round>Success</px-button>
<px-button type="warning" round>Warning</px-button>
<px-button type="danger" round>Danger</px-button>
</p>
<p>
<px-button icon="search" circle />
<px-button type="primary" icon="edit-solid" circle />
<px-button type="success" icon="check-solid" circle />
<px-button type="warning" icon="star-solid" circle />
<px-button type="danger" icon="trash-solid" circle />
</p>
</template>
禁用状态
你可以使用 disabled
属性来定义按钮是否被禁用。
<template>
<p>
<px-button>Button</px-button>
<px-button type="primary">Primary</px-button>
<px-button type="success">Success</px-button>
<px-button type="warning">Warning</px-button>
<px-button type="danger">Danger</px-button>
</p>
<p>
<px-button disabled>Button</px-button>
<px-button type="primary" disabled>Primary</px-button>
<px-button type="success" disabled>Success</px-button>
<px-button type="warning" disabled>Warning</px-button>
<px-button type="danger" disabled>Danger</px-button>
</p>
</template>
图标按钮
使用 icon
属性来定义按钮的图标。
<template>
<div>
<px-button type="primary" icon="edit" />
<px-button type="primary" icon="share" />
<px-button type="primary" icon="trash" />
<px-button type="primary" icon="search">Search</px-button>
<px-button type="primary">
Upload
<px-icon icon="upload-alt-solid" style="margin-left: 8px" color="#fff" />
</px-button>
</div>
</template>
按钮组
使用 <px-button-group>
对多个按钮分组。
<template>
<p>
<px-button-group type="primary" round>
<px-button>Button1</px-button>
<px-button>Button2</px-button>
</px-button-group>
</p>
<p>
<px-button-group type="success" round>
<px-button>Button1</px-button>
<px-button>Button2</px-button>
<px-button>Button3</px-button>
</px-button-group>
</p>
<p>
<px-button-group type="warning" round>
<px-button>Button1</px-button>
<px-button>Button2</px-button>
<px-button>Button3</px-button>
<px-button>Button4</px-button>
</px-button-group>
</p>
<p>
<px-button-group type="danger">
<px-button type="primary" icon="edit" round />
<px-button type="primary" icon="share" />
<px-button type="primary" icon="trash" round />
</px-button-group>
</p>
</template>
加载状态
使用 loading
属性来定义按钮的加载状态。
TIP
您可以使用 loading
插槽或 loadingIcon
属性自定义您的 loading 图标
ps: loading
插槽优先级高于 loadingIcon
属性
<template>
<div>
<px-button type="primary" loading>Loading</px-button>
<px-button type="primary" loading-icon="circle-notch" loading>
Loading
</px-button>
<px-button type="primary" loading>
<template #loading>
<px-icon
style="margin-right: 6px"
icon="star-crescent-solid"
spin
color="#fff"
/>
</template>
Loading
</px-button>
</div>
</template>
按钮尺寸
使用 size
属性来定义按钮的尺寸。
<template>
<p>
<px-button type="primary" size="large">817</px-button>
<px-button type="success" size="large">817</px-button>
<px-button type="warning" size="large">817</px-button>
<px-button type="danger" size="large">817</px-button>
</p>
<p>
<px-button type="primary">817</px-button>
<px-button type="success">817</px-button>
<px-button type="warning">817</px-button>
<px-button type="danger">817</px-button>
</p>
<p>
<px-button type="primary" size="small">817</px-button>
<px-button type="success" size="small">817</px-button>
<px-button type="warning" size="small">817</px-button>
<px-button type="danger" size="small">817</px-button>
</p>
</template>
Tag
可以自定义元素标签。例如,按钮,div,路由链接,nuxt 链接。
<template>
<px-button>button</px-button>
<px-button tag="div" role="button" tabindex="0">div</px-button>
<px-button type="primary" tag="a">a</px-button>
</template>
节流模式
可以通过 useThrottle
属性来定义按钮是否使用节流模式 默认为 true。
<script setup>
const handleBtnClick = () => {
console.log('btn click')
}
</script>
<template>
<p>
<px-button @click="handleBtnClick"> with throttle</px-button>
<px-button :use-throttle="false" @click="handleBtnClick">
without throttle
</px-button>
</p>
</template>
Button API
Props
Name | Description | Type | Default |
---|---|---|---|
size | 尺寸 | enum - large | default | small | — |
type | 类型 | enum - primary | success | warning | danger | base | base |
plain | 是否为朴素按钮 | boolean | false |
round | 是否为圆角按钮 | boolean | false |
circle | 是否为圆形按钮 | boolean | false |
loading | 是否为加载中状态 | boolean | false |
loading-icon | 自定义加载中状态图标组件 | string | spinner |
disabled | 按钮是否为禁用状态 | boolean | false |
icon | 按钮图标 | string | - |
autofocus | 是否自动聚焦(原生autofocus 属性) | boolean | false |
native-type | 原生 type 属性 | enum - button | submit | reset | button |
tag | 自定义元素标签 | string /Component | button |
use-throttle | 是否使用节流模式 | boolean | true |
throttle-duration | 节流模式下,节流时间间隔(ms) | number | 500 |
Events
Name | Description | Type |
---|---|---|
click | 按钮点击事件 | (event: MouseEvent) => void |
Slots
Name | Description |
---|---|
default | 默认插槽, 按钮内容 |
loading | 自定义加载图标 |
Expose
Name | Description | Type |
---|---|---|
ref | 按钮 html 元素 | Ref<HTMLButtonElement> |
size | 按钮尺寸 | ComputedRef<default | small | large> |
type | 按钮类型 | ComputedRef<''| primary |...> |
disabled | 按钮禁用状态 | ComputedRef<boolean> |
ButtonGroup API
Props
Name | Description | Type | Default |
---|---|---|---|
size | 尺寸 | enum - large | default | small | — |
type | 类型 | enum - primary | success | warning | danger | base | base |
disabled | 按钮组是否为禁用状态 | boolean | false |
Slots
Name | Description | Sub Component |
---|---|---|
default | 默认插槽 | Button |