tui-form-button.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <template>
  2. <view class="tui-button__container" :style="{width: width,height: height,margin:margin,borderRadius: radius}"
  3. @touchstart="handleStart" @touchend="handleClick" @touchcancel="handleEnd">
  4. <button class="tui-button" :class="[
  5. bold ? 'tui-text__bold' : '',
  6. time && (plain || link) ? 'tui-button__opacity' : '',
  7. disabled && !disabledBackground ? 'tui-button__opacity' : '',
  8. !width || width==='100%' || width===true?'tui-button__flex-1':'',
  9. time && !plain && !link ? 'tui-button__active' : ''
  10. ]" :style="{
  11. width: width,
  12. height: height,
  13. lineHeight: height,
  14. background: disabled && disabledBackground ? disabledBackground : (plain ? 'transparent' : background),
  15. borderWidth:borderWidth,
  16. borderColor: borderColor ? borderColor : disabled && disabledBackground ? disabledBackground : (link?'transparent':background),
  17. borderRadius: radius,
  18. fontSize: size + 'rpx',
  19. color: disabled && disabledBackground ? disabledColor : color
  20. }" :loading="loading" :form-type="formType" :open-type="openType" @getuserinfo="bindgetuserinfo"
  21. @getphonenumber="bindgetphonenumber" @contact="bindcontact" @error="binderror"
  22. @opensetting="bindopensetting" :disabled="disabled" :scope="scope" @tap.stop="handleTap">
  23. <text class="tui-button__text" :class="{'tui-text__bold':bold}" v-if="text"
  24. :style="{fontSize: size + 'rpx',lineHeight:size + 'rpx',color: disabled && disabledBackground ? disabledColor : color}">{{text}}</text>
  25. <slot></slot>
  26. </button>
  27. </view>
  28. </template>
  29. <script>
  30. export default {
  31. name: 'tui-form-button',
  32. emits: ['click', 'getuserinfo', 'contact', 'getphonenumber', 'error', 'opensetting'],
  33. // #ifndef VUE3
  34. // #ifdef MP-WEIXIN
  35. behaviors: ['wx://form-field-button'],
  36. // #endif
  37. // #endif
  38. props: {
  39. //按钮背景色
  40. background: {
  41. type: String,
  42. default: '#5677fc'
  43. },
  44. //按钮显示文本
  45. text: {
  46. type: String,
  47. default: ''
  48. },
  49. //按钮字体颜色
  50. color: {
  51. type: String,
  52. default: '#fff'
  53. },
  54. //按钮禁用背景色
  55. disabledBackground: {
  56. type: String,
  57. default: ''
  58. },
  59. //按钮禁用字体颜色
  60. disabledColor: {
  61. type: String,
  62. default: ''
  63. },
  64. borderWidth: {
  65. type: String,
  66. // #ifdef APP-NVUE
  67. default: '0.5px'
  68. // #endif
  69. // #ifndef APP-NVUE
  70. default: '1rpx'
  71. // #endif
  72. },
  73. borderColor: {
  74. type: String,
  75. default: ''
  76. },
  77. //宽度
  78. width: {
  79. type: String,
  80. default: '100%'
  81. },
  82. //高度
  83. height: {
  84. type: String,
  85. default: '96rpx'
  86. },
  87. //字体大小,单位rpx
  88. size: {
  89. type: [Number, String],
  90. default: 32
  91. },
  92. bold: {
  93. type: Boolean,
  94. default: false
  95. },
  96. margin: {
  97. type: String,
  98. default: '0'
  99. },
  100. //圆角
  101. radius: {
  102. type: String,
  103. default: '6rpx'
  104. },
  105. plain: {
  106. type: Boolean,
  107. default: false
  108. },
  109. link: {
  110. type: Boolean,
  111. default: false
  112. },
  113. disabled: {
  114. type: Boolean,
  115. default: false
  116. },
  117. loading: {
  118. type: Boolean,
  119. default: false
  120. },
  121. formType: {
  122. type: String,
  123. default: ''
  124. },
  125. openType: {
  126. type: String,
  127. default: ''
  128. },
  129. //支付宝小程序
  130. //当 open-type 为 getAuthorize 时,可以设置 scope 为:phoneNumber、userInfo
  131. scope: {
  132. type: String,
  133. default: ''
  134. },
  135. index: {
  136. type: [Number, String],
  137. default: 0
  138. }
  139. },
  140. data() {
  141. return {
  142. time: 0,
  143. trigger: false,
  144. tap: false
  145. };
  146. },
  147. methods: {
  148. handleStart() {
  149. if (this.disabled) return;
  150. this.trigger = false;
  151. this.tap = true;
  152. if (new Date().getTime() - this.time <= 150) return;
  153. this.trigger = true;
  154. this.time = new Date().getTime();
  155. },
  156. handleClick() {
  157. if (this.disabled || !this.trigger) return;
  158. this.time = 0;
  159. },
  160. handleTap() {
  161. if (this.disabled) return;
  162. this.$emit('click', {
  163. index: Number(this.index)
  164. });
  165. },
  166. handleEnd() {
  167. if (this.disabled) return;
  168. setTimeout(() => {
  169. this.time = 0;
  170. }, 150);
  171. },
  172. bindgetuserinfo({
  173. detail = {}
  174. } = {}) {
  175. this.$emit('getuserinfo', detail);
  176. },
  177. bindcontact({
  178. detail = {}
  179. } = {}) {
  180. this.$emit('contact', detail);
  181. },
  182. bindgetphonenumber({
  183. detail = {}
  184. } = {}) {
  185. this.$emit('getphonenumber', detail);
  186. },
  187. binderror({
  188. detail = {}
  189. } = {}) {
  190. this.$emit('error', detail);
  191. },
  192. bindopensetting({
  193. detail = {}
  194. } = {}) {
  195. this.$emit('opensetting', detail);
  196. }
  197. }
  198. };
  199. </script>
  200. <style scoped>
  201. .tui-button__container {
  202. position: relative;
  203. }
  204. .tui-button {
  205. /* #ifdef APP-NVUE */
  206. border-width: 0.5px;
  207. /* #endif */
  208. /* #ifndef APP-NVUE */
  209. border-width: 1rpx;
  210. display: flex;
  211. align-items: center;
  212. justify-content: center;
  213. /* #endif */
  214. border-style: solid;
  215. position: relative;
  216. padding-left: 0;
  217. padding-right: 0;
  218. overflow: hidden;
  219. /* #ifndef APP-NVUE */
  220. transform: translateZ(0);
  221. -webkit-touch-callout: none;
  222. -webkit-user-select: none;
  223. user-select: none;
  224. /* #endif */
  225. }
  226. .tui-button__flex-1 {
  227. flex: 1;
  228. /* #ifndef APP-NVUE */
  229. width: 100%;
  230. /* #endif */
  231. }
  232. .tui-button::after {
  233. border: 0;
  234. }
  235. /* #ifndef APP-NVUE */
  236. .tui-button__active {
  237. overflow: hidden !important;
  238. }
  239. .tui-button__active::after {
  240. content: ' ';
  241. background-color: rgba(0, 0, 0, 0.1);
  242. position: absolute;
  243. width: 100%;
  244. height: 100%;
  245. left: 0;
  246. right: 0;
  247. top: 0;
  248. transform: none;
  249. z-index: 1;
  250. border-radius: 0;
  251. }
  252. /* #endif */
  253. .tui-button__text {
  254. text-align: center;
  255. flex-direction: row;
  256. align-items: center;
  257. justify-content: center !important;
  258. padding-left: 0 !important;
  259. }
  260. .tui-button__opacity {
  261. opacity: 0.5;
  262. }
  263. .tui-text__bold {
  264. font-weight: bold;
  265. }
  266. </style>