uni-events-helper-wx
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

485 lines
19 KiB

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