Skip to content

Button 按钮

预览
01:38

组件名:rice-button

该组件在小程序端是基于原生的 button 组件为基础进行二次封装的,支持小程序的开放能力

平台兼容性

uni-app x

AndroidiOS鸿蒙Next微信小程序h5

按钮类型

按钮支持 primary success warning error info default 六种类型,默认为 default。类型通过 type 属性进行定义。文字内容通过 text 传入

vue
<rice-button type="primary" text="主要按钮"></rice-button>
<rice-button type="success" text="成功按钮"></rice-button>
<rice-button type="warning" text="警告按钮"></rice-button>
<rice-button type="error" text="危险按钮"></rice-button>
<rice-button type="info" text="信息按钮"></rice-button>
<rice-button text="默认按钮"></rice-button>

朴素带浅色背景的按钮

通过 plain-fill 属性可以将按钮设置为朴素带浅色背景的按钮

vue
<rice-button type="primary" plain-fill text="主要按钮"></rice-button>
<rice-button type="success" plain-fill text="成功按钮"></rice-button>
<rice-button type="warning" plain-fill text="警告按钮"></rice-button>
<rice-button type="error" plain-fill text="危险按钮"></rice-button>

朴素按钮

通过 plain 属性可以设置为朴素按钮,此时文字的颜色为按钮的颜色,按钮的背景色为透明。

vue
<rice-button type="primary" plain text="主要按钮"></rice-button>
<rice-button type="success" plain text="成功按钮"></rice-button>
<rice-button type="warning" plain text="警告按钮"></rice-button>
<rice-button type="error" plain text="危险按钮"></rice-button>

禁用状态

通过 disabled 属性可以禁用按钮,此时点击事件将失效。

vue
<rice-button type="primary" text="禁用状态" disabled></rice-button>
<rice-button type="success" text="禁用状态" disabled></rice-button>

加载状态

通过 loading 属性可以把按钮设置为加载中,此时点击事件将失效。

vue
<rice-button type="primary" loading></rice-button>
<rice-button type="success" loading loading-mode="semicircle" text="加载中…"></rice-button>

按钮形状

通过 shape 属性可以设置按钮的形状。

vue
<rice-button type="primary" shape="square" text="方形按钮"></rice-button>
<rice-button type="success" text="圆形按钮" shape="round"></rice-button>

图标按钮

通过 icon 属性可以设置按钮的图标。

vue
<rice-button type="primary" icon="plus"></rice-button>
<rice-button type="success" icon="star" text="按钮图标"></rice-button>

按钮尺寸

通过 size 属性可以设置按钮的尺寸大小。当然也可以通过 widthheight 自定义按钮的宽高。

vue
<rice-button type="primary" size="large" text="大号按钮"></rice-button>
<rice-button type="primary" text="普通按钮"></rice-button>
<rice-button type="primary" size="small" text="小型按钮"></rice-button>
<rice-button type="primary" size="mini" text="迷你按钮"></rice-button>

自定义颜色

通过 color 属性可以自定义按钮的颜色,支持渐变色。

vue
<rice-button color="#8b60ef" text="紫 色"></rice-button>
<rice-button color="#8b60ef" text="紫色朴素" plain-fill></rice-button>
<rice-button color="linear-gradient(to right, rgb(255, 140, 8), rgb(238, 10, 36))" text="渐变色"></rice-button>

小程序开放能力

vue
<rice-button type="primary" open-type="contact" text="打开客服会话"></rice-button>
<rice-button type="primary" open-type="share" text="分享"></rice-button>
<rice-button type="primary" open-type="getPhoneNumber" text="手机号快速验证" @getphonenumber="getphonenumber" />
<rice-button type="primary" open-type="feedback" text="意见反馈"></rice-button>
<rice-button type="primary" open-type="openSetting" text="打开授权设置页"></rice-button>
<rice-button type="primary" open-type="chooseAvatar" text="获取头像" @chooseavatar="chooseavatar"></rice-button>

API

Props

参数说明类型默认值
type类型, 可选 primary success warning error info defaultstringdefault
size尺寸,可选 large small ministring-
text按钮文字string-
textColor文字颜色string-
textSize文字大小string|number14px
color按钮背景颜色string-
darkColor暗黑模式下按钮颜色string-
plainFill是否为带浅色背景的朴素按钮booleanfalse
plain是否为朴素按钮booleanfalse
icon左侧图标名称或图片链接,等同于 Icon 组件的 name 属性string-
iconSize图标大小string|number16px
iconColor图标/loading颜色string-
loading是否加载状态booleanfalse
loadingSize加载图标大小string|number18px
loadingText加载状态提示文字,按钮处于加载状态时会替换掉按钮的文字string-
loadingMode加载图标类型 ,等同于 Loading 组件的 type 属性stringspinner
disabled是否禁用booleanfalse
shape按钮形状,可选 round squarestring-
height按钮高度string|number-
width按钮宽度string|number-
to页面导航地址,点击时会跳转到对应的页面string-
openType开放能力,仅微信小程序支持,可选 contact share getPhoneNumber getRealtimePhoneNumber getUserInfo chooseAvatar等,其他更多类型和使用方式见小程序文档string-
customStyle自定义样式Object

Event

事件名说明回调参数
click点击事件,按钮状态不为加载或禁用时才会触发-
getphonenumber手机号快速验证回调,open-type=getPhoneNumber时有效,仅微信小程序支持见小程序文档
getrealtimephonenumber手机号实时验证回调,open-type=getRealtimePhoneNumber 时有效,仅微信小程序支持见小程序文档
chooseavatar获取用户头像回调,open-type=chooseAvatar时有效,仅微信小程序支持见小程序文档
getuserinfo获取用户信息的回调,open-type=getUserInfo 时有效,仅微信小程序支持见小程序文档
opensetting在打开授权设置页后回调,open-type=openSetting时有效 时有效,仅微信小程序支持见小程序文档
contact打开客服会话回调,open-type=contact 时有效,仅微信小程序支持见小程序文档
agreeprivacyauthorization用户同意隐私协议事件回调,open-type=agreePrivacyAuthorization时有效,仅微信小程序支持见小程序文档
launchapp打开 APP 成功的回调,open-type=launchApp时有效,仅微信小程序支持见小程序文档
error当使用开放能力时,发生错误的回调,open-type=launchApp时有效,仅微信小程序支持见小程序文档

小程序的开放能力见小程序文档

类型定义

组件导出如下类型

ts
import { ButtonType, ButtonSize, ButtonShape } from '@/uni_modules/rice-ui';
//组件类型
const buttonRef=ref<RiceButtonComponentPublicInstance|null>(null)