uni-list-item.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. padding-left: $uni-spacing-row-lg;
  142. }
  143. .uni-list-item--disabled {
  144. opacity: 0.3;
  145. }
  146. .uni-list-item--hover {
  147. background-color: $uni-bg-color-hover;
  148. }
  149. .uni-list-item__container {
  150. /* #ifndef APP-NVUE */
  151. display: flex;
  152. /* #endif */
  153. flex-direction: row;
  154. padding: $list-item-pd;
  155. padding-left: 0;
  156. flex: 1;
  157. position: relative;
  158. justify-content: space-between;
  159. align-items: center;
  160. border-top-color: $uni-border-color;
  161. border-top-style: solid;
  162. border-top-width: 1px;
  163. }
  164. .uni-list-item--first {
  165. border-top-width: 0px;
  166. }
  167. .uni-list-item__content {
  168. /* #ifndef APP-NVUE */
  169. display: flex;
  170. /* #endif */
  171. flex: 1;
  172. overflow: hidden;
  173. flex-direction: column;
  174. color: #3b4144;
  175. }
  176. .uni-list-item__content-title {
  177. font-size: $uni-font-size-base;
  178. color: #3b4144;
  179. overflow: hidden;
  180. }
  181. .uni-list-item__content-note {
  182. margin-top: 6rpx;
  183. color: $uni-text-color-grey;
  184. font-size: $uni-font-size-sm;
  185. overflow: hidden;
  186. }
  187. .uni-list-item__extra {
  188. // width: 25%;
  189. /* #ifndef APP-NVUE */
  190. display: flex;
  191. /* #endif */
  192. flex-direction: row;
  193. justify-content: flex-end;
  194. align-items: center;
  195. }
  196. .uni-list-item__icon {
  197. margin-right: 18rpx;
  198. flex-direction: row;
  199. justify-content: center;
  200. align-items: center;
  201. display: flex;
  202. }
  203. .uni-list-item__icon-img {
  204. height: $uni-img-size-base;
  205. width: $uni-img-size-base;
  206. }
  207. // 修改switch默认大小
  208. .itemSwitch{
  209. transform: translateX(16rpx) scale(.84);
  210. }
  211. .uni-list-item__slot{
  212. color: #909399;
  213. font-size: 28rpx;
  214. }
  215. </style>