uni-fav.vue 3.3 KB

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