uni-fav.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <view :class="[circle === true || circle === 'true' ? 'uni-fav--circle' : '']" :style="[{ backgroundColor: checked ? bgColorChecked : bgColor }]"
  3. @click="onClick" class="uni-fav">
  4. <!-- #ifdef MP-ALIPAY -->
  5. <view class="uni-fav-star" v-if="!checked && (star === true || star === 'true')">
  6. <uni-icons :color="fgColor" :style="{color: checked ? fgColorChecked : fgColor}" size="14" type="star-filled" />
  7. </view>
  8. <!-- #endif -->
  9. <!-- #ifndef MP-ALIPAY -->
  10. <uni-icons :color="fgColor" :style="{color: checked ? fgColorChecked : fgColor}" class="uni-fav-star" size="14" type="star-filled"
  11. v-if="!checked && (star === true || star === 'true')" />
  12. <!-- #endif -->
  13. <text :style="{color: checked ? fgColorChecked : fgColor}" class="uni-fav-text">{{ checked ? contentText.contentFav : contentText.contentDefault }}</text>
  14. </view>
  15. </template>
  16. <script>
  17. /**
  18. * Fav 收藏按钮
  19. * @description 用于收藏功能,可点击切换选中、不选中的状态
  20. * @tutorial https://ext.dcloud.net.cn/plugin?id=864
  21. * @property {Boolean} star = [true|false] 按钮是否带星星
  22. * @property {String} bgColor 未收藏时的背景色
  23. * @property {String} bgColorChecked 已收藏时的背景色
  24. * @property {String} fgColor 未收藏时的文字颜色
  25. * @property {String} fgColorChecked 已收藏时的文字颜色
  26. * @property {Boolean} circle = [true|false] 是否为圆角
  27. * @property {Boolean} checked = [true|false] 是否为已收藏
  28. * @property {Object} contentText = [true|false] 收藏按钮文字
  29. * @event {Function} click 点击 fav按钮触发事件
  30. * @example <uni-fav :checked="true"/>
  31. */
  32. export default {
  33. name: "UniFav",
  34. props: {
  35. star: {
  36. type: [Boolean, String],
  37. default: true
  38. },
  39. bgColor: {
  40. type: String,
  41. default: "#eeeeee"
  42. },
  43. fgColor: {
  44. type: String,
  45. default: "#666666"
  46. },
  47. bgColorChecked: {
  48. type: String,
  49. default: "#007aff"
  50. },
  51. fgColorChecked: {
  52. type: String,
  53. default: "#FFFFFF"
  54. },
  55. circle: {
  56. type: [Boolean, String],
  57. default: false
  58. },
  59. checked: {
  60. type: Boolean,
  61. default: false
  62. },
  63. contentText: {
  64. type: Object,
  65. default () {
  66. return {
  67. contentDefault: "收藏",
  68. contentFav: "已收藏"
  69. };
  70. }
  71. }
  72. },
  73. watch: {
  74. checked() {
  75. if (uni.report) {
  76. if (this.checked) {
  77. uni.report("收藏", "收藏");
  78. } else {
  79. uni.report("取消收藏", "取消收藏");
  80. }
  81. }
  82. }
  83. },
  84. methods: {
  85. onClick() {
  86. this.$emit("click");
  87. }
  88. }
  89. };
  90. </script>
  91. <style lang="scss" scoped>
  92. $fav-height: 25px;
  93. .uni-fav {
  94. /* #ifndef APP-NVUE */
  95. display: flex;
  96. /* #endif */
  97. flex-direction: row;
  98. align-items: center;
  99. justify-content: center;
  100. width: 60px;
  101. height: $fav-height;
  102. line-height: $fav-height;
  103. text-align: center;
  104. border-radius: 3px;
  105. /* #ifdef H5 */
  106. cursor: pointer;
  107. /* #endif */
  108. }
  109. .uni-fav--circle {
  110. border-radius: 30px;
  111. }
  112. .uni-fav-star {
  113. /* #ifndef APP-NVUE */
  114. display: flex;
  115. /* #endif */
  116. height: $fav-height;
  117. line-height: 24px;
  118. margin-right: 3px;
  119. align-items: center;
  120. justify-content: center;
  121. }
  122. .uni-fav-text {
  123. /* #ifndef APP-NVUE */
  124. display: flex;
  125. /* #endif */
  126. height: $fav-height;
  127. line-height: $fav-height;
  128. align-items: center;
  129. justify-content: center;
  130. font-size: $uni-font-size-base;
  131. }
  132. </style>