u-button.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. :hover-class="!disabled && !loading ? 'u-button--active' : ''"
  23. class="u-button u-reset-button"
  24. :style="[baseColor, $u.addStyle(customStyle)]"
  25. @tap="clickHandler"
  26. :class="bemClass"
  27. >
  28. <template v-if="loading">
  29. <u-loading-icon
  30. :mode="loadingMode"
  31. :size="loadingSize * 1.15"
  32. :color="loadingColor"
  33. ></u-loading-icon>
  34. <text
  35. class="u-button__loading-text"
  36. :style="[{ fontSize: textSize + 'px' }]"
  37. >{{ loadingText || text }}</text
  38. >
  39. </template>
  40. <template v-else>
  41. <u-icon
  42. v-if="icon"
  43. :name="icon"
  44. :color="iconColorCom"
  45. :size="textSize * 1.35"
  46. :customStyle="{ marginRight: '2px' }"
  47. ></u-icon>
  48. <slot>
  49. <text
  50. class="u-button__text"
  51. :style="[{ fontSize: textSize + 'px' }]"
  52. >{{ text }}</text
  53. >
  54. </slot>
  55. </template>
  56. </button>
  57. <!-- #endif -->
  58. <!-- #ifdef APP-NVUE -->
  59. <view
  60. :hover-start-time="Number(hoverStartTime)"
  61. :hover-stay-time="Number(hoverStayTime)"
  62. class="u-button"
  63. :hover-class="
  64. !disabled && !loading && !color && (plain || type === 'info')
  65. ? 'u-button--active--plain'
  66. : !disabled && !loading && !plain
  67. ? 'u-button--active'
  68. : ''
  69. "
  70. @tap="clickHandler"
  71. :class="bemClass"
  72. :style="[baseColor, $u.addStyle(customStyle)]"
  73. >
  74. <template v-if="loading">
  75. <u-loading-icon
  76. :mode="loadingMode"
  77. :size="loadingSize * 1.15"
  78. :color="loadingColor"
  79. ></u-loading-icon>
  80. <text
  81. class="u-button__loading-text"
  82. :style="[nvueTextStyle]"
  83. :class="[plain && `u-button__text--plain--${type}`]"
  84. >{{ loadingText || text }}</text
  85. >
  86. </template>
  87. <template v-else>
  88. <u-icon
  89. v-if="icon"
  90. :name="icon"
  91. :color="iconColorCom"
  92. :size="textSize * 1.35"
  93. ></u-icon>
  94. <text
  95. class="u-button__text"
  96. :style="[
  97. {
  98. marginLeft: icon ? '2px' : 0,
  99. },
  100. nvueTextStyle,
  101. ]"
  102. :class="[plain && `u-button__text--plain--${type}`]"
  103. >{{ text }}</text
  104. >
  105. </template>
  106. </view>
  107. <!-- #endif -->
  108. </template>
  109. <script>
  110. import button from "../../libs/mixin/button.js";
  111. import openType from "../../libs/mixin/openType.js";
  112. import mpMixin from '../../libs/mixin/mpMixin.js';
  113. import mixin from '../../libs/mixin/mixin.js';
  114. import props from "./props.js";
  115. /**
  116. * button 按钮
  117. * @description Button 按钮
  118. * @tutorial https://ijry.github.io/uview-plus/components/button.html
  119. *
  120. * @property {Boolean} hairline 是否显示按钮的细边框 (默认 true )
  121. * @property {String} type 按钮的预置样式,info,primary,error,warning,success (默认 'info' )
  122. * @property {String} size 按钮尺寸,large,normal,mini (默认 normal)
  123. * @property {String} shape 按钮形状,circle(两边为半圆),square(带圆角) (默认 'square' )
  124. * @property {Boolean} plain 按钮是否镂空,背景色透明 (默认 false)
  125. * @property {Boolean} disabled 是否禁用 (默认 false)
  126. * @property {Boolean} loading 按钮名称前是否带 loading 图标(App-nvue 平台,在 ios 上为雪花,Android上为圆圈) (默认 false)
  127. * @property {String | Number} loadingText 加载中提示文字
  128. * @property {String} loadingMode 加载状态图标类型 (默认 'spinner' )
  129. * @property {String | Number} loadingSize 加载图标大小 (默认 15 )
  130. * @property {String} openType 开放能力,具体请看uniapp稳定关于button组件部分说明
  131. * @property {String} formType 用于 <form> 组件,点击分别会触发 <form> 组件的 submit/reset 事件
  132. * @property {String} appParameter 打开 APP 时,向 APP 传递的参数,open-type=launchApp时有效 (注:只微信小程序、QQ小程序有效)
  133. * @property {Boolean} hoverStopPropagation 指定是否阻止本节点的祖先节点出现点击态,微信小程序有效(默认 true )
  134. * @property {String} lang 指定返回用户信息的语言,zh_CN 简体中文,zh_TW 繁体中文,en 英文(默认 en )
  135. * @property {String} sessionFrom 会话来源,openType="contact"时有效
  136. * @property {String} sendMessageTitle 会话内消息卡片标题,openType="contact"时有效
  137. * @property {String} sendMessagePath 会话内消息卡片点击跳转小程序路径,openType="contact"时有效
  138. * @property {String} sendMessageImg 会话内消息卡片图片,openType="contact"时有效
  139. * @property {Boolean} showMessageCard 是否显示会话内消息卡片,设置此参数为 true,用户进入客服会话会在右下角显示"可能要发送的小程序"提示,用户点击后可以快速发送小程序消息,openType="contact"时有效(默认false)
  140. * @property {String} dataName 额外传参参数,用于小程序的data-xxx属性,通过target.dataset.name获取
  141. * @property {String | Number} throttleTime 节流,一定时间内只能触发一次 (默认 0 )
  142. * @property {String | Number} hoverStartTime 按住后多久出现点击态,单位毫秒 (默认 0 )
  143. * @property {String | Number} hoverStayTime 手指松开后点击态保留时间,单位毫秒 (默认 200 )
  144. * @property {String | Number} text 按钮文字,之所以通过props传入,是因为slot传入的话(注:nvue中无法控制文字的样式)
  145. * @property {String} icon 按钮图标
  146. * @property {String} iconColor 按钮图标颜色
  147. * @property {String} color 按钮颜色,支持传入linear-gradient渐变色
  148. * @property {Object} customStyle 定义需要用到的外部样式
  149. *
  150. * @event {Function} click 非禁止并且非加载中,才能点击
  151. * @event {Function} getphonenumber open-type="getPhoneNumber"时有效
  152. * @event {Function} getuserinfo 用户点击该按钮时,会返回获取到的用户信息,从返回参数的detail中获取到的值同uni.getUserInfo
  153. * @event {Function} error 当使用开放能力时,发生错误的回调
  154. * @event {Function} opensetting 在打开授权设置页并关闭后回调
  155. * @event {Function} launchapp 打开 APP 成功的回调
  156. * @example <u-button>月落</u-button>
  157. */
  158. export default {
  159. name: "u-button",
  160. // #ifdef MP
  161. mixins: [mpMixin, mixin, button, openType, props],
  162. // #endif
  163. // #ifndef MP
  164. mixins: [mpMixin, 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. emits: ['click', 'getphonenumber', 'getuserinfo',
  264. 'error', 'opensetting', 'launchapp'],
  265. methods: {
  266. clickHandler() {
  267. // 非禁止并且非加载中,才能点击
  268. if (!this.disabled && !this.loading) {
  269. // 进行节流控制,每this.throttle毫秒内,只在开始处执行
  270. uni.$u.throttle(() => {
  271. this.$emit("click");
  272. }, this.throttleTime);
  273. }
  274. },
  275. // 下面为对接uniapp官方按钮开放能力事件回调的对接
  276. getphonenumber(res) {
  277. this.$emit("getphonenumber", res);
  278. },
  279. getuserinfo(res) {
  280. this.$emit("getuserinfo", res);
  281. },
  282. error(res) {
  283. this.$emit("error", res);
  284. },
  285. opensetting(res) {
  286. this.$emit("opensetting", res);
  287. },
  288. launchapp(res) {
  289. this.$emit("launchapp", res);
  290. },
  291. },
  292. };
  293. </script>
  294. <style lang="scss" scoped>
  295. @import "../../libs/css/components.scss";
  296. /* #ifndef APP-NVUE */
  297. @import "./vue.scss";
  298. /* #endif */
  299. /* #ifdef APP-NVUE */
  300. @import "./nvue.scss";
  301. /* #endif */
  302. $u-button-u-button-height: 40px !default;
  303. $u-button-text-font-size: 15px !default;
  304. $u-button-loading-text-font-size: 15px !default;
  305. $u-button-loading-text-margin-left: 4px !default;
  306. $u-button-large-width: 100% !default;
  307. $u-button-large-height: 50px !default;
  308. $u-button-normal-padding: 0 12px !default;
  309. $u-button-large-padding: 0 15px !default;
  310. $u-button-normal-font-size: 14px !default;
  311. $u-button-small-min-width: 60px !default;
  312. $u-button-small-height: 30px !default;
  313. $u-button-small-padding: 0px 8px !default;
  314. $u-button-mini-padding: 0px 8px !default;
  315. $u-button-small-font-size: 12px !default;
  316. $u-button-mini-height: 22px !default;
  317. $u-button-mini-font-size: 10px !default;
  318. $u-button-mini-min-width: 50px !default;
  319. $u-button-disabled-opacity: 0.5 !default;
  320. $u-button-info-color: #323233 !default;
  321. $u-button-info-background-color: #fff !default;
  322. $u-button-info-border-color: #ebedf0 !default;
  323. $u-button-info-border-width: 1px !default;
  324. $u-button-info-border-style: solid !default;
  325. $u-button-success-color: #fff !default;
  326. $u-button-success-background-color: $u-success !default;
  327. $u-button-success-border-color: $u-button-success-background-color !default;
  328. $u-button-success-border-width: 1px !default;
  329. $u-button-success-border-style: solid !default;
  330. $u-button-primary-color: #fff !default;
  331. $u-button-primary-background-color: $u-primary !default;
  332. $u-button-primary-border-color: $u-button-primary-background-color !default;
  333. $u-button-primary-border-width: 1px !default;
  334. $u-button-primary-border-style: solid !default;
  335. $u-button-error-color: #fff !default;
  336. $u-button-error-background-color: $u-error !default;
  337. $u-button-error-border-color: $u-button-error-background-color !default;
  338. $u-button-error-border-width: 1px !default;
  339. $u-button-error-border-style: solid !default;
  340. $u-button-warning-color: #fff !default;
  341. $u-button-warning-background-color: $u-warning !default;
  342. $u-button-warning-border-color: $u-button-warning-background-color !default;
  343. $u-button-warning-border-width: 1px !default;
  344. $u-button-warning-border-style: solid !default;
  345. $u-button-block-width: 100% !default;
  346. $u-button-circle-border-top-right-radius: 100px !default;
  347. $u-button-circle-border-top-left-radius: 100px !default;
  348. $u-button-circle-border-bottom-left-radius: 100px !default;
  349. $u-button-circle-border-bottom-right-radius: 100px !default;
  350. $u-button-square-border-top-right-radius: 3px !default;
  351. $u-button-square-border-top-left-radius: 3px !default;
  352. $u-button-square-border-bottom-left-radius: 3px !default;
  353. $u-button-square-border-bottom-right-radius: 3px !default;
  354. $u-button-icon-min-width: 1em !default;
  355. $u-button-plain-background-color: #fff !default;
  356. $u-button-hairline-border-width: 0.5px !default;
  357. .u-button {
  358. height: $u-button-u-button-height;
  359. position: relative;
  360. align-items: center;
  361. justify-content: center;
  362. @include flex;
  363. /* #ifndef APP-NVUE */
  364. box-sizing: border-box;
  365. /* #endif */
  366. flex-direction: row;
  367. &__text {
  368. font-size: $u-button-text-font-size;
  369. }
  370. &__loading-text {
  371. font-size: $u-button-loading-text-font-size;
  372. margin-left: $u-button-loading-text-margin-left;
  373. }
  374. &--large {
  375. /* #ifndef APP-NVUE */
  376. width: $u-button-large-width;
  377. /* #endif */
  378. height: $u-button-large-height;
  379. padding: $u-button-large-padding;
  380. }
  381. &--normal {
  382. padding: $u-button-normal-padding;
  383. font-size: $u-button-normal-font-size;
  384. }
  385. &--small {
  386. /* #ifndef APP-NVUE */
  387. min-width: $u-button-small-min-width;
  388. /* #endif */
  389. height: $u-button-small-height;
  390. padding: $u-button-small-padding;
  391. font-size: $u-button-small-font-size;
  392. }
  393. &--mini {
  394. height: $u-button-mini-height;
  395. font-size: $u-button-mini-font-size;
  396. /* #ifndef APP-NVUE */
  397. min-width: $u-button-mini-min-width;
  398. /* #endif */
  399. padding: $u-button-mini-padding;
  400. }
  401. &--disabled {
  402. opacity: $u-button-disabled-opacity;
  403. }
  404. &--info {
  405. color: $u-button-info-color;
  406. background-color: $u-button-info-background-color;
  407. border-color: $u-button-info-border-color;
  408. border-width: $u-button-info-border-width;
  409. border-style: $u-button-info-border-style;
  410. }
  411. &--success {
  412. color: $u-button-success-color;
  413. background-color: $u-button-success-background-color;
  414. border-color: $u-button-success-border-color;
  415. border-width: $u-button-success-border-width;
  416. border-style: $u-button-success-border-style;
  417. }
  418. &--primary {
  419. color: $u-button-primary-color;
  420. background-color: $u-button-primary-background-color;
  421. border-color: $u-button-primary-border-color;
  422. border-width: $u-button-primary-border-width;
  423. border-style: $u-button-primary-border-style;
  424. }
  425. &--error {
  426. color: $u-button-error-color;
  427. background-color: $u-button-error-background-color;
  428. border-color: $u-button-error-border-color;
  429. border-width: $u-button-error-border-width;
  430. border-style: $u-button-error-border-style;
  431. }
  432. &--warning {
  433. color: $u-button-warning-color;
  434. background-color: $u-button-warning-background-color;
  435. border-color: $u-button-warning-border-color;
  436. border-width: $u-button-warning-border-width;
  437. border-style: $u-button-warning-border-style;
  438. }
  439. &--block {
  440. @include flex;
  441. width: $u-button-block-width;
  442. }
  443. &--circle {
  444. border-top-right-radius: $u-button-circle-border-top-right-radius;
  445. border-top-left-radius: $u-button-circle-border-top-left-radius;
  446. border-bottom-left-radius: $u-button-circle-border-bottom-left-radius;
  447. border-bottom-right-radius: $u-button-circle-border-bottom-right-radius;
  448. }
  449. &--square {
  450. border-bottom-left-radius: $u-button-square-border-top-right-radius;
  451. border-bottom-right-radius: $u-button-square-border-top-left-radius;
  452. border-top-left-radius: $u-button-square-border-bottom-left-radius;
  453. border-top-right-radius: $u-button-square-border-bottom-right-radius;
  454. }
  455. &__icon {
  456. /* #ifndef APP-NVUE */
  457. min-width: $u-button-icon-min-width;
  458. line-height: inherit !important;
  459. vertical-align: top;
  460. /* #endif */
  461. }
  462. &--plain {
  463. background-color: $u-button-plain-background-color;
  464. }
  465. &--hairline {
  466. border-width: $u-button-hairline-border-width !important;
  467. }
  468. }
  469. </style>