u-button.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <template>
  2. <!-- #ifndef APP-NVUE -->
  3. <button :hover-start-time="Number(hoverStartTime)" :hover-stay-time="Number(hoverStayTime)" :form-type="formType"
  4. :open-type="openType" :app-parameter="appParameter" :hover-stop-propagation="hoverStopPropagation"
  5. :send-message-title="sendMessageTitle" :send-message-path="sendMessagePath" :lang="lang" :data-name="dataName"
  6. :session-from="sessionFrom" :send-message-img="sendMessageImg" :show-message-card="showMessageCard"
  7. @getphonenumber="getphonenumber" @getuserinfo="getuserinfo" @error="error" @opensetting="opensetting"
  8. @launchapp="launchapp" @agreeprivacyauthorization="agreeprivacyauthorization"
  9. :hover-class="!disabled && !loading ? 'u-button--active' : ''" class="u-button u-reset-button"
  10. :style="[baseColor, $u.addStyle(customStyle)]" @tap="clickHandler" :class="bemClass">
  11. <template v-if="loading">
  12. <u-loading-icon :mode="loadingMode" :size="loadingSize * 1.15" :color="loadingColor"></u-loading-icon>
  13. <text class="u-button__loading-text"
  14. :style="[{ fontSize: textSize + 'px' }]">{{ loadingText || text }}</text>
  15. </template>
  16. <template v-else>
  17. <u-icon v-if="icon" :name="icon" :color="iconColorCom" :size="textSize * 1.35"
  18. :customStyle="{ marginRight: '2px' }"></u-icon>
  19. <slot>
  20. <text class="u-button__text" :style="[{ fontSize: textSize + 'px' }]">{{ text }}</text>
  21. </slot>
  22. </template>
  23. </button>
  24. <!-- #endif -->
  25. <!-- #ifdef APP-NVUE -->
  26. <view :hover-start-time="Number(hoverStartTime)" :hover-stay-time="Number(hoverStayTime)" class="u-button"
  27. :hover-class="
  28. !disabled && !loading && !color && (plain || type === 'info')
  29. ? 'u-button--active--plain'
  30. : !disabled && !loading && !plain
  31. ? 'u-button--active'
  32. : ''
  33. " @tap="clickHandler" :class="bemClass" :style="[baseColor, $u.addStyle(customStyle)]">
  34. <template v-if="loading">
  35. <u-loading-icon :mode="loadingMode" :size="loadingSize * 1.15" :color="loadingColor"></u-loading-icon>
  36. <text class="u-button__loading-text" :style="[nvueTextStyle]"
  37. :class="[plain && `u-button__text--plain--${type}`]">{{ loadingText || text }}</text>
  38. </template>
  39. <template v-else>
  40. <u-icon v-if="icon" :name="icon" :color="iconColorCom" :size="textSize * 1.35"></u-icon>
  41. <text class="u-button__text" :style="[
  42. {
  43. marginLeft: icon ? '2px' : 0,
  44. },
  45. nvueTextStyle,
  46. ]" :class="[plain && `u-button__text--plain--${type}`]">{{ text }}</text>
  47. </template>
  48. </view>
  49. <!-- #endif -->
  50. </template>
  51. <script>
  52. import button from "../../libs/mixin/button.js";
  53. import openType from "../../libs/mixin/openType.js";
  54. import props from "./props.js";
  55. /**
  56. * button 按钮
  57. * @description Button 按钮
  58. * @tutorial https://www.uviewui.com/components/button.html
  59. *
  60. * @property {Boolean} hairline 是否显示按钮的细边框 (默认 true )
  61. * @property {String} type 按钮的预置样式,info,primary,error,warning,success (默认 'info' )
  62. * @property {String} size 按钮尺寸,large,normal,mini (默认 normal)
  63. * @property {String} shape 按钮形状,circle(两边为半圆),square(带圆角) (默认 'square' )
  64. * @property {Boolean} plain 按钮是否镂空,背景色透明 (默认 false)
  65. * @property {Boolean} disabled 是否禁用 (默认 false)
  66. * @property {Boolean} loading 按钮名称前是否带 loading 图标(App-nvue 平台,在 ios 上为雪花,Android上为圆圈) (默认 false)
  67. * @property {String | Number} loadingText 加载中提示文字
  68. * @property {String} loadingMode 加载状态图标类型 (默认 'spinner' )
  69. * @property {String | Number} loadingSize 加载图标大小 (默认 15 )
  70. * @property {String} openType 开放能力,具体请看uniapp稳定关于button组件部分说明
  71. * @property {String} formType 用于 <form> 组件,点击分别会触发 <form> 组件的 submit/reset 事件
  72. * @property {String} appParameter 打开 APP 时,向 APP 传递的参数,open-type=launchApp时有效 (注:只微信小程序、QQ小程序有效)
  73. * @property {Boolean} hoverStopPropagation 指定是否阻止本节点的祖先节点出现点击态,微信小程序有效(默认 true )
  74. * @property {String} lang 指定返回用户信息的语言,zh_CN 简体中文,zh_TW 繁体中文,en 英文(默认 en )
  75. * @property {String} sessionFrom 会话来源,openType="contact"时有效
  76. * @property {String} sendMessageTitle 会话内消息卡片标题,openType="contact"时有效
  77. * @property {String} sendMessagePath 会话内消息卡片点击跳转小程序路径,openType="contact"时有效
  78. * @property {String} sendMessageImg 会话内消息卡片图片,openType="contact"时有效
  79. * @property {Boolean} showMessageCard 是否显示会话内消息卡片,设置此参数为 true,用户进入客服会话会在右下角显示"可能要发送的小程序"提示,用户点击后可以快速发送小程序消息,openType="contact"时有效(默认false)
  80. * @property {String} dataName 额外传参参数,用于小程序的data-xxx属性,通过target.dataset.name获取
  81. * @property {String | Number} throttleTime 节流,一定时间内只能触发一次 (默认 0 )
  82. * @property {String | Number} hoverStartTime 按住后多久出现点击态,单位毫秒 (默认 0 )
  83. * @property {String | Number} hoverStayTime 手指松开后点击态保留时间,单位毫秒 (默认 200 )
  84. * @property {String | Number} text 按钮文字,之所以通过props传入,是因为slot传入的话(注:nvue中无法控制文字的样式)
  85. * @property {String} icon 按钮图标
  86. * @property {String} iconColor 按钮图标颜色
  87. * @property {String} color 按钮颜色,支持传入linear-gradient渐变色
  88. * @property {Object} customStyle 定义需要用到的外部样式
  89. *
  90. * @event {Function} click 非禁止并且非加载中,才能点击
  91. * @event {Function} getphonenumber open-type="getPhoneNumber"时有效
  92. * @event {Function} getuserinfo 用户点击该按钮时,会返回获取到的用户信息,从返回参数的detail中获取到的值同uni.getUserInfo
  93. * @event {Function} error 当使用开放能力时,发生错误的回调
  94. * @event {Function} opensetting 在打开授权设置页并关闭后回调
  95. * @event {Function} launchapp 打开 APP 成功的回调
  96. * @event {Function} agreeprivacyauthorization 用户同意隐私协议事件回调
  97. * @example <u-button>月落</u-button>
  98. */
  99. export default {
  100. name: "u-button",
  101. // #ifdef MP
  102. mixins: [uni.$u.mpMixin, uni.$u.mixin, button, openType, props],
  103. // #endif
  104. // #ifndef MP
  105. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  106. // #endif
  107. data() {
  108. return {};
  109. },
  110. computed: {
  111. // 生成bem风格的类名
  112. bemClass() {
  113. // this.bem为一个computed变量,在mixin中
  114. if (!this.color) {
  115. return this.bem(
  116. "button",
  117. ["type", "shape", "size"],
  118. ["disabled", "plain", "hairline"]
  119. );
  120. } else {
  121. // 由于nvue的原因,在有color参数时,不需要传入type,否则会生成type相关的类型,影响最终的样式
  122. return this.bem(
  123. "button",
  124. ["shape", "size"],
  125. ["disabled", "plain", "hairline"]
  126. );
  127. }
  128. },
  129. loadingColor() {
  130. if (this.plain) {
  131. // 如果有设置color值,则用color值,否则使用type主题颜色
  132. return this.color ?
  133. this.color :
  134. uni.$u.config.color[`u-${this.type}`];
  135. }
  136. if (this.type === "info") {
  137. return "#c9c9c9";
  138. }
  139. return "rgb(200, 200, 200)";
  140. },
  141. iconColorCom() {
  142. // 如果是镂空状态,设置了color就用color值,否则使用主题颜色,
  143. // u-icon的color能接受一个主题颜色的值
  144. if (this.iconColor) return this.iconColor;
  145. if (this.plain) {
  146. return this.color ? this.color : this.type;
  147. } else {
  148. return this.type === "info" ? "#000000" : "#ffffff";
  149. }
  150. },
  151. baseColor() {
  152. let style = {};
  153. if (this.color) {
  154. // 针对自定义了color颜色的情况,镂空状态下,就是用自定义的颜色
  155. style.color = this.plain ? this.color : "white";
  156. if (!this.plain) {
  157. // 非镂空,背景色使用自定义的颜色
  158. style["background-color"] = this.color;
  159. }
  160. if (this.color.indexOf("gradient") !== -1) {
  161. // 如果自定义的颜色为渐变色,不显示边框,以及通过backgroundImage设置渐变色
  162. // weex文档说明可以写borderWidth的形式,为什么这里需要分开写?
  163. // 因为weex是阿里巴巴为了部门业绩考核而做的你懂的东西,所以需要这么写才有效
  164. style.borderTopWidth = 0;
  165. style.borderRightWidth = 0;
  166. style.borderBottomWidth = 0;
  167. style.borderLeftWidth = 0;
  168. if (!this.plain) {
  169. style.backgroundImage = this.color;
  170. }
  171. } else {
  172. // 非渐变色,则设置边框相关的属性
  173. style.borderColor = this.color;
  174. style.borderWidth = "1px";
  175. style.borderStyle = "solid";
  176. }
  177. }
  178. return style;
  179. },
  180. // nvue版本按钮的字体不会继承父组件的颜色,需要对每一个text组件进行单独的设置
  181. nvueTextStyle() {
  182. let style = {};
  183. // 针对自定义了color颜色的情况,镂空状态下,就是用自定义的颜色
  184. if (this.type === "info") {
  185. style.color = "#323233";
  186. }
  187. if (this.color) {
  188. style.color = this.plain ? this.color : "white";
  189. }
  190. style.fontSize = this.textSize + "px";
  191. return style;
  192. },
  193. // 字体大小
  194. textSize() {
  195. let fontSize = 14,
  196. {
  197. size
  198. } = this;
  199. if (size === "large") fontSize = 16;
  200. if (size === "normal") fontSize = 14;
  201. if (size === "small") fontSize = 12;
  202. if (size === "mini") fontSize = 10;
  203. return fontSize;
  204. },
  205. },
  206. methods: {
  207. clickHandler() {
  208. // 非禁止并且非加载中,才能点击
  209. if (!this.disabled && !this.loading) {
  210. // 进行节流控制,每this.throttle毫秒内,只在开始处执行
  211. uni.$u.throttle(() => {
  212. this.$emit("click");
  213. }, this.throttleTime);
  214. }
  215. },
  216. // 下面为对接uniapp官方按钮开放能力事件回调的对接
  217. getphonenumber(res) {
  218. this.$emit("getphonenumber", res);
  219. },
  220. getuserinfo(res) {
  221. this.$emit("getuserinfo", res);
  222. },
  223. error(res) {
  224. this.$emit("error", res);
  225. },
  226. opensetting(res) {
  227. this.$emit("opensetting", res);
  228. },
  229. launchapp(res) {
  230. this.$emit("launchapp", res);
  231. },
  232. agreeprivacyauthorization(res) {
  233. this.$emit("agreeprivacyauthorization", res);
  234. },
  235. },
  236. };
  237. </script>
  238. <style lang="scss" scoped>
  239. @import "../../libs/css/components.scss";
  240. /* #ifndef APP-NVUE */
  241. @import "./vue.scss";
  242. /* #endif */
  243. /* #ifdef APP-NVUE */
  244. @import "./nvue.scss";
  245. /* #endif */
  246. $u-button-u-button-height: 40px !default;
  247. $u-button-text-font-size: 15px !default;
  248. $u-button-loading-text-font-size: 15px !default;
  249. $u-button-loading-text-margin-left: 4px !default;
  250. $u-button-large-width: 100% !default;
  251. $u-button-large-height: 50px !default;
  252. $u-button-normal-padding: 0 12px !default;
  253. $u-button-large-padding: 0 15px !default;
  254. $u-button-normal-font-size: 14px !default;
  255. $u-button-small-min-width: 60px !default;
  256. $u-button-small-height: 30px !default;
  257. $u-button-small-padding: 0px 8px !default;
  258. $u-button-mini-padding: 0px 8px !default;
  259. $u-button-small-font-size: 12px !default;
  260. $u-button-mini-height: 22px !default;
  261. $u-button-mini-font-size: 10px !default;
  262. $u-button-mini-min-width: 50px !default;
  263. $u-button-disabled-opacity: 0.5 !default;
  264. $u-button-info-color: #323233 !default;
  265. $u-button-info-background-color: #fff !default;
  266. $u-button-info-border-color: #ebedf0 !default;
  267. $u-button-info-border-width: 1px !default;
  268. $u-button-info-border-style: solid !default;
  269. $u-button-success-color: #fff !default;
  270. $u-button-success-background-color: $u-success !default;
  271. $u-button-success-border-color: $u-button-success-background-color !default;
  272. $u-button-success-border-width: 1px !default;
  273. $u-button-success-border-style: solid !default;
  274. $u-button-primary-color: #fff !default;
  275. $u-button-primary-background-color: $u-primary !default;
  276. $u-button-primary-border-color: $u-button-primary-background-color !default;
  277. $u-button-primary-border-width: 1px !default;
  278. $u-button-primary-border-style: solid !default;
  279. $u-button-error-color: #fff !default;
  280. $u-button-error-background-color: $u-error !default;
  281. $u-button-error-border-color: $u-button-error-background-color !default;
  282. $u-button-error-border-width: 1px !default;
  283. $u-button-error-border-style: solid !default;
  284. $u-button-warning-color: #fff !default;
  285. $u-button-warning-background-color: $u-warning !default;
  286. $u-button-warning-border-color: $u-button-warning-background-color !default;
  287. $u-button-warning-border-width: 1px !default;
  288. $u-button-warning-border-style: solid !default;
  289. $u-button-block-width: 100% !default;
  290. $u-button-circle-border-top-right-radius: 100px !default;
  291. $u-button-circle-border-top-left-radius: 100px !default;
  292. $u-button-circle-border-bottom-left-radius: 100px !default;
  293. $u-button-circle-border-bottom-right-radius: 100px !default;
  294. $u-button-square-border-top-right-radius: 3px !default;
  295. $u-button-square-border-top-left-radius: 3px !default;
  296. $u-button-square-border-bottom-left-radius: 3px !default;
  297. $u-button-square-border-bottom-right-radius: 3px !default;
  298. $u-button-icon-min-width: 1em !default;
  299. $u-button-plain-background-color: #fff !default;
  300. $u-button-hairline-border-width: 0.5px !default;
  301. .u-button {
  302. height: $u-button-u-button-height;
  303. position: relative;
  304. align-items: center;
  305. justify-content: center;
  306. @include flex;
  307. /* #ifndef APP-NVUE */
  308. box-sizing: border-box;
  309. /* #endif */
  310. flex-direction: row;
  311. &__text {
  312. font-size: $u-button-text-font-size;
  313. }
  314. &__loading-text {
  315. font-size: $u-button-loading-text-font-size;
  316. margin-left: $u-button-loading-text-margin-left;
  317. }
  318. &--large {
  319. /* #ifndef APP-NVUE */
  320. width: $u-button-large-width;
  321. /* #endif */
  322. height: $u-button-large-height;
  323. padding: $u-button-large-padding;
  324. }
  325. &--normal {
  326. padding: $u-button-normal-padding;
  327. font-size: $u-button-normal-font-size;
  328. }
  329. &--small {
  330. /* #ifndef APP-NVUE */
  331. min-width: $u-button-small-min-width;
  332. /* #endif */
  333. height: $u-button-small-height;
  334. padding: $u-button-small-padding;
  335. font-size: $u-button-small-font-size;
  336. }
  337. &--mini {
  338. height: $u-button-mini-height;
  339. font-size: $u-button-mini-font-size;
  340. /* #ifndef APP-NVUE */
  341. min-width: $u-button-mini-min-width;
  342. /* #endif */
  343. padding: $u-button-mini-padding;
  344. }
  345. &--disabled {
  346. opacity: $u-button-disabled-opacity;
  347. }
  348. &--info {
  349. color: $u-button-info-color;
  350. background-color: $u-button-info-background-color;
  351. border-color: $u-button-info-border-color;
  352. border-width: $u-button-info-border-width;
  353. border-style: $u-button-info-border-style;
  354. }
  355. &--success {
  356. color: $u-button-success-color;
  357. background-color: $u-button-success-background-color;
  358. border-color: $u-button-success-border-color;
  359. border-width: $u-button-success-border-width;
  360. border-style: $u-button-success-border-style;
  361. }
  362. &--primary {
  363. color: $u-button-primary-color;
  364. background-color: $u-button-primary-background-color;
  365. border-color: $u-button-primary-border-color;
  366. border-width: $u-button-primary-border-width;
  367. border-style: $u-button-primary-border-style;
  368. }
  369. &--error {
  370. color: $u-button-error-color;
  371. background-color: $u-button-error-background-color;
  372. border-color: $u-button-error-border-color;
  373. border-width: $u-button-error-border-width;
  374. border-style: $u-button-error-border-style;
  375. }
  376. &--warning {
  377. color: $u-button-warning-color;
  378. background-color: $u-button-warning-background-color;
  379. border-color: $u-button-warning-border-color;
  380. border-width: $u-button-warning-border-width;
  381. border-style: $u-button-warning-border-style;
  382. }
  383. &--block {
  384. @include flex;
  385. width: $u-button-block-width;
  386. }
  387. &--circle {
  388. border-top-right-radius: $u-button-circle-border-top-right-radius;
  389. border-top-left-radius: $u-button-circle-border-top-left-radius;
  390. border-bottom-left-radius: $u-button-circle-border-bottom-left-radius;
  391. border-bottom-right-radius: $u-button-circle-border-bottom-right-radius;
  392. }
  393. &--square {
  394. border-bottom-left-radius: $u-button-square-border-top-right-radius;
  395. border-bottom-right-radius: $u-button-square-border-top-left-radius;
  396. border-top-left-radius: $u-button-square-border-bottom-left-radius;
  397. border-top-right-radius: $u-button-square-border-bottom-right-radius;
  398. }
  399. &__icon {
  400. /* #ifndef APP-NVUE */
  401. min-width: $u-button-icon-min-width;
  402. line-height: inherit !important;
  403. vertical-align: top;
  404. /* #endif */
  405. }
  406. &--plain {
  407. background-color: $u-button-plain-background-color;
  408. }
  409. &--hairline {
  410. border-width: $u-button-hairline-border-width !important;
  411. }
  412. }
  413. </style>