uni-fav.vue 3.3 KB

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