van-button.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <button
  3. :id="id"
  4. :data-detail="dataset"
  5. :class="buttonClass"
  6. :hover-class="btnHoverClass"
  7. :lang="lang"
  8. :form-type="formType"
  9. :style="buttonStyle"
  10. :open-type="disabled ? '' : openType"
  11. :business-id="businessId"
  12. :session-from="sessionFrom"
  13. :send-message-title="sendMessageTitle"
  14. :send-message-path="sendMessagePath"
  15. :send-message-img="sendMessageImg"
  16. :show-message-card="showMessageCard"
  17. :app-parameter="appParameter"
  18. :aria-label="ariaLabel"
  19. @click="onClick"
  20. @getuserinfo="bindGetUserInfo"
  21. @contact="bindContact"
  22. @getphonenumber="bindGetPhoneNumber"
  23. @error="bindError"
  24. @launchapp="bindLaunchApp"
  25. @opensetting="bindOpenSetting"
  26. >
  27. <block v-if="loading">
  28. <van-loading
  29. :custom-class="loadingCustomClass"
  30. :size="loadingSize"
  31. :type="loadingType"
  32. :color="loadingColor(type, color, plain)"
  33. />
  34. <view v-if="loadingText" class="van-button__loading-text">
  35. {{ loadingText }}
  36. </view>
  37. </block>
  38. <block v-else>
  39. <van-icon
  40. v-if="icon"
  41. size="1.2em"
  42. :name="icon"
  43. :class-prefix="classPrefix"
  44. custom-class="van-button__icon"
  45. class="van-button__icon"
  46. custom-style="line-height: inherit;"
  47. />
  48. <view class="van-button__text">
  49. <slot />
  50. </view>
  51. </block>
  52. </button>
  53. </template>
  54. <script>
  55. import { VantComponent } from '../common/component'
  56. import { button } from '../mixins/button'
  57. import { openType } from '../mixins/open-type'
  58. import VanIcon from '../van-icon/van-icon'
  59. import VanLoading from '../van-loading/van-loading'
  60. const utils = require('../wxs/utils')
  61. export default {
  62. // 组件必须这样子引入,不能放在 VantComponent 里面
  63. components: {
  64. VanIcon,
  65. VanLoading
  66. },
  67. ...VantComponent({
  68. classes: ['hover-class', 'loading-class'],
  69. mixins: [button, openType],
  70. props: {
  71. formType: String,
  72. icon: String,
  73. classPrefix: {
  74. type: String,
  75. default: 'van-icon'
  76. },
  77. plain: Boolean,
  78. block: Boolean,
  79. round: Boolean,
  80. square: Boolean,
  81. loading: Boolean,
  82. hairline: Boolean,
  83. disabled: Boolean,
  84. loadingText: String,
  85. loadingType: {
  86. type: String,
  87. default: 'circular'
  88. },
  89. type: {
  90. type: String,
  91. default: 'default'
  92. },
  93. dataset: null,
  94. size: {
  95. type: String,
  96. default: 'normal'
  97. },
  98. loadingSize: {
  99. type: String,
  100. default: '20px'
  101. },
  102. color: {
  103. type: String
  104. }
  105. },
  106. computed: {
  107. baseStyle() {
  108. let style = ''
  109. const { color, plain } = this
  110. if (color) {
  111. style += `color: ${plain ? color : 'white'};`
  112. if (!plain) {
  113. // Use background instead of backgroundColor to make linear-gradient work
  114. style += `background: ${color};`
  115. }
  116. // hide border when color is linear-gradient
  117. if (color.indexOf('gradient') !== -1) {
  118. style += 'border: 0;'
  119. } else {
  120. style += `border-color: ${color};`
  121. }
  122. }
  123. return style
  124. },
  125. buttonClass() {
  126. const { customClass, type, size, block, round, plain, square, loading, disabled, hairline } = this
  127. const bemClass = utils.bem('button', [type, size, { block, round, plain, square, loading, disabled, hairline, unclickable: disabled || loading }])
  128. return `custom-class ${ customClass } ${ bemClass } ${ hairline ? 'van-hairline--surround' : '' }`
  129. },
  130. buttonStyle() {
  131. const { baseStyle, customStyle } = this
  132. return `${ baseStyle } ${ customStyle }`
  133. },
  134. btnHoverClass() {
  135. return `van-button--active hover-class ${this.hoverClass}`
  136. },
  137. loadingCustomClass() {
  138. return `loading-class ${this.loadingClass}`
  139. }
  140. },
  141. methods: {
  142. loadingColor(type, color, plain) {
  143. if (plain) {
  144. return color ? color : '#c9c9c9'
  145. }
  146. if (type === 'default') {
  147. return '#c9c9c9'
  148. }
  149. return 'white'
  150. },
  151. onClick(e) {
  152. if (this.disabled) {
  153. return
  154. }
  155. if (!this.loading) {
  156. this.$emit('click', e)
  157. }
  158. }
  159. }
  160. })
  161. }
  162. </script>
  163. <style lang="less">
  164. @import './index.less';
  165. </style>