uni-list-item.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <template>
  2. <!-- #ifdef APP-NVUE -->
  3. <cell>
  4. <!-- #endif -->
  5. <view :class="disabled ? 'uni-list-item--disabled' : ''" :hover-class="disabled || showSwitch ? '' : 'uni-list-item--hover'"
  6. class="uni-list-item" @click="onClick">
  7. <view class="uni-list-item__container" :class="{'uni-list-item--first':isFirstChild}">
  8. <view v-if="thumb" class="uni-list-item__icon">
  9. <image :src="thumb" mode="aspectFit" class="uni-list-item__icon-img" />
  10. </view>
  11. <view v-else-if="showExtraIcon" class="uni-list-item__icon">
  12. <uni-icons :color="extraIcon.color" :size="extraIcon.size" :type="extraIcon.type" class="uni-icon-wrapper" />
  13. </view>
  14. <view class="uni-list-item__content">
  15. <slot></slot>
  16. <text class="uni-list-item__content-title">{{ title }}</text>
  17. <text v-if="note" class="uni-list-item__content-note">{{ note }}</text>
  18. </view>
  19. <view class="uni-list-item__slot">
  20. <slot name="right" ></slot>
  21. </view>
  22. <view v-if="showBadge || showArrow || showSwitch" class="uni-list-item__extra">
  23. <uni-badge v-if="showBadge" :type="badgeType" :text="badgeText" />
  24. <switch class="itemSwitch" v-if="showSwitch" :color="switchColor" :disabled="disabled" :checked="switchChecked" @change="onSwitchChange" />
  25. <uni-icons v-if="showArrow" :size="20" class="uni-icon-wrapper" color="#bbb" type="arrowright" />
  26. </view>
  27. </view>
  28. </view>
  29. <!-- #ifdef APP-NVUE -->
  30. </cell>
  31. <!-- #endif -->
  32. </template>
  33. <script>
  34. import uniIcons from '../uni-icons/uni-icons.vue'
  35. import uniBadge from '../uni-badge/uni-badge.vue'
  36. export default {
  37. name: 'UniListItem',
  38. components: {
  39. uniIcons,
  40. uniBadge
  41. },
  42. props: {
  43. title: {
  44. type: String,
  45. default: ''
  46. }, // 列表标题
  47. note: {
  48. type: String,
  49. default: ''
  50. }, // 列表描述
  51. disabled: {
  52. // 是否禁用
  53. type: [Boolean, String],
  54. default: false
  55. },
  56. showArrow: {
  57. // 是否显示箭头
  58. type: [Boolean, String],
  59. default: true
  60. },
  61. showBadge: {
  62. // 是否显示数字角标
  63. type: [Boolean, String],
  64. default: false
  65. },
  66. showSwitch: {
  67. // 是否显示Switch
  68. type: [Boolean, String],
  69. default: false
  70. },
  71. switchChecked: {
  72. // Switch是否被选中
  73. type: [Boolean, String],
  74. default: false
  75. },
  76. switchColor:{
  77. type:String,
  78. default:''
  79. },
  80. badgeText: {
  81. // badge内容
  82. type: String,
  83. default: ''
  84. },
  85. badgeType: {
  86. // badge类型
  87. type: String,
  88. default: 'success'
  89. },
  90. thumb: {
  91. // 缩略图
  92. type: String,
  93. default: ''
  94. },
  95. showExtraIcon: {
  96. // 是否显示扩展图标
  97. type: [Boolean, String],
  98. default: false
  99. },
  100. extraIcon: {
  101. type: Object,
  102. default () {
  103. return {
  104. type: 'contact',
  105. color: '#000000',
  106. size: 20
  107. }
  108. }
  109. }
  110. },
  111. inject: ['list'],
  112. data() {
  113. return {
  114. isFirstChild: false
  115. }
  116. },
  117. mounted() {
  118. if (!this.list.firstChildAppend) {
  119. this.list.firstChildAppend = true
  120. this.isFirstChild = true
  121. }
  122. },
  123. methods: {
  124. onClick() {
  125. this.$emit('click')
  126. },
  127. onSwitchChange(e) {
  128. this.$emit('switchChange', e.detail)
  129. }
  130. }
  131. }
  132. </script>
  133. <style lang="scss" scoped>
  134. @import '~@/uni.scss';
  135. $list-item-pd: $uni-spacing-col-lg $uni-spacing-row-lg;
  136. .uni-list-item {
  137. font-size: $uni-font-size-lg;
  138. position: relative;
  139. flex-direction: column;
  140. justify-content: space-between;
  141. }
  142. .uni-list-item--disabled {
  143. opacity: 0.3;
  144. }
  145. .uni-list-item--hover {
  146. background-color: $uni-bg-color-hover;
  147. }
  148. .uni-list-item__container {
  149. /* #ifndef APP-NVUE */
  150. display: flex;
  151. /* #endif */
  152. flex-direction: row;
  153. padding: $list-item-pd;
  154. flex: 1;
  155. position: relative;
  156. justify-content: space-between;
  157. align-items: center;
  158. border-top-color: $uni-border-color;
  159. border-top-style: solid;
  160. border-top-width: 1px;
  161. }
  162. .uni-list-item--first {
  163. border-top-width: 0px;
  164. }
  165. .uni-list-item__content {
  166. /* #ifndef APP-NVUE */
  167. display: flex;
  168. /* #endif */
  169. flex: 1;
  170. overflow: hidden;
  171. flex-direction: column;
  172. color: #3b4144;
  173. }
  174. .uni-list-item__content-title {
  175. font-size: $uni-font-size-base;
  176. color: #3b4144;
  177. overflow: hidden;
  178. }
  179. .uni-list-item__content-note {
  180. margin-top: 6rpx;
  181. color: $uni-text-color-grey;
  182. font-size: $uni-font-size-sm;
  183. overflow: hidden;
  184. }
  185. .uni-list-item__extra {
  186. // width: 25%;
  187. /* #ifndef APP-NVUE */
  188. display: flex;
  189. /* #endif */
  190. flex-direction: row;
  191. justify-content: flex-end;
  192. align-items: center;
  193. }
  194. .uni-list-item__icon {
  195. margin-right: 18rpx;
  196. flex-direction: row;
  197. justify-content: center;
  198. align-items: center;
  199. display: flex;
  200. }
  201. .uni-list-item__icon-img {
  202. height: $uni-img-size-base;
  203. width: $uni-img-size-base;
  204. }
  205. // 修改switch默认大小
  206. .itemSwitch{
  207. transform: translateX(16rpx) scale(.84);
  208. }
  209. .uni-list-item__slot{
  210. color: #909399;
  211. font-size: 28rpx;
  212. }
  213. </style>