u-button.vue 19 KB

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