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.

503 lines
20 KiB

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