uni-list-item.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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'" class="uni-list-item" @click="onClick">
  6. <view class="uni-list-item__container" :class="{ 'uni-list-item--first': isFirstChild }">
  7. <view v-if="thumb" class="uni-list-item__icon"><image :src="thumb" class="uni-list-item__icon-img" /></view>
  8. <view v-else-if="showExtraIcon" class="uni-list-item__icon">
  9. <uni-icons :color="extraIcon.color" :size="extraIcon.size" :type="extraIcon.type" class="uni-icon-wrapper" />
  10. </view>
  11. <view class="uni-list-item__content">
  12. <slot name="left"></slot>
  13. <text class="uni-list-item__content-title">{{ title }}</text>
  14. <text v-if="note" class="uni-list-item__content-note">{{ note }}</text>
  15. </view>
  16. <view class="uni-list-item__extra">
  17. <text v-if="rightText" class="uni-list-item__extra-text">{{ rightText }}</text>
  18. <uni-badge v-if="showBadge" :type="badgeType" :text="badgeText" />
  19. <switch v-if="showSwitch" :disabled="disabled" :checked="switchChecked" @change="onSwitchChange" />
  20. <slot name="right"></slot>
  21. <uni-icons v-if="showArrow" :size="20" class="uni-icon-wrapper" color="#bbb" type="arrowright" />
  22. </view>
  23. </view>
  24. </view>
  25. <!-- #ifdef APP-NVUE -->
  26. </cell>
  27. <!-- #endif -->
  28. </template>
  29. <script>
  30. import uniIcons from '../uni-icons/uni-icons.vue';
  31. import uniBadge from '../uni-badge/uni-badge.vue';
  32. /**
  33. * ListItem 列表子组件
  34. * @description 列表子组件
  35. * @tutorial https://ext.dcloud.net.cn/plugin?id=24
  36. * @property {String} title 标题
  37. * @property {String} note 描述
  38. * @property {String} thumb 左侧缩略图,若thumb有值,则不会显示扩展图标
  39. * @property {String} badgeText 数字角标内容
  40. * @property {String} badgeType 数字角标类型,参考[uni-icons](https://ext.dcloud.net.cn/plugin?id=21)
  41. * @property {String} rightText 右侧文字内容
  42. * @property {Boolean} disabled = [true|false]是否禁用
  43. * @property {Boolean} showArrow = [true|false] 是否显示箭头图标
  44. * @property {Boolean} showBadge = [true|false] 是否显示数字角标
  45. * @property {Boolean} showSwitch = [true|false] 是否显示Switch
  46. * @property {Boolean} switchChecked = [true|false] Switch是否被选中
  47. * @property {Boolean} showExtraIcon = [true|false] 左侧是否显示扩展图标
  48. * @property {Boolean} scrollY = [true|false] 允许纵向滚动,需要显式的设置其宽高
  49. * @property {Object} extraIcon 扩展图标参数,格式为 {color: '#4cd964',size: '22',type: 'spinner'}
  50. * @event {Function} click 点击 uniListItem 触发事件
  51. * @event {Function} switchChange 点击切换 Switch 时触发
  52. */
  53. export default {
  54. name: 'UniListItem',
  55. components: {
  56. uniIcons,
  57. uniBadge
  58. },
  59. props: {
  60. title: {
  61. type: String,
  62. default: ''
  63. }, // 列表标题
  64. note: {
  65. type: String,
  66. default: ''
  67. }, // 列表描述
  68. disabled: {
  69. // 是否禁用
  70. type: [Boolean, String],
  71. default: false
  72. },
  73. showArrow: {
  74. // 是否显示箭头
  75. type: [Boolean, String],
  76. default: true
  77. },
  78. showBadge: {
  79. // 是否显示数字角标
  80. type: [Boolean, String],
  81. default: false
  82. },
  83. showSwitch: {
  84. // 是否显示Switch
  85. type: [Boolean, String],
  86. default: false
  87. },
  88. switchChecked: {
  89. // Switch是否被选中
  90. type: [Boolean, String],
  91. default: false
  92. },
  93. badgeText: {
  94. // badge内容
  95. type: String,
  96. default: ''
  97. },
  98. badgeType: {
  99. // badge类型
  100. type: String,
  101. default: 'success'
  102. },
  103. rightText: {
  104. // 右侧文字内容
  105. type: String,
  106. default: ''
  107. },
  108. thumb: {
  109. // 缩略图
  110. type: String,
  111. default: ''
  112. },
  113. showExtraIcon: {
  114. // 是否显示扩展图标
  115. type: [Boolean, String],
  116. default: false
  117. },
  118. extraIcon: {
  119. type: Object,
  120. default() {
  121. return {
  122. type: 'contact',
  123. color: '#000000',
  124. size: 20
  125. };
  126. }
  127. }
  128. },
  129. inject: ['list'],
  130. data() {
  131. return {
  132. isFirstChild: false
  133. };
  134. },
  135. mounted() {
  136. if (!this.list.firstChildAppend) {
  137. this.list.firstChildAppend = true;
  138. this.isFirstChild = true;
  139. }
  140. },
  141. methods: {
  142. onClick() {
  143. this.$emit('click');
  144. },
  145. onSwitchChange(e) {
  146. this.$emit('switchChange', e.detail);
  147. }
  148. }
  149. };
  150. </script>
  151. <style lang="scss" scoped>
  152. $list-item-pd: $uni-spacing-col-lg $uni-spacing-row-lg;
  153. .uni-list-item {
  154. font-size: $uni-font-size-lg;
  155. position: relative;
  156. flex-direction: column;
  157. justify-content: space-between;
  158. }
  159. .uni-list-item--disabled {
  160. opacity: 0.3;
  161. }
  162. .uni-list-item--hover {
  163. background-color: $uni-bg-color-hover;
  164. }
  165. .uni-list-item__container {
  166. line-height: 48rpx;
  167. position: relative;
  168. /* #ifndef APP-NVUE */
  169. display: flex;
  170. /* #endif */
  171. flex-direction: row;
  172. padding: $list-item-pd;
  173. padding-left: 0;
  174. flex: 1;
  175. position: relative;
  176. justify-content: space-between;
  177. align-items: center;
  178. /* #ifdef APP-PLUS */
  179. border-top-color: $uni-border-color;
  180. border-top-style: solid;
  181. border-top-width: 0.5px;
  182. /* #endif */
  183. }
  184. .uni-list-item--first {
  185. border-top-width: 0px;
  186. }
  187. /* #ifndef APP-NVUE */
  188. .uni-list-item__container:after {
  189. position: absolute;
  190. top: 0;
  191. right: 0;
  192. left: 0;
  193. height: 1px;
  194. content: '';
  195. -webkit-transform: scaleY(0.5);
  196. transform: scaleY(0.5);
  197. background-color: $uni-border-color;
  198. }
  199. .uni-list-item--first:after {
  200. height: 0px;
  201. }
  202. /* #endif */
  203. .uni-list-item__content {
  204. /* #ifndef APP-NVUE */
  205. display: flex;
  206. /* #endif */
  207. flex: 1;
  208. overflow: hidden;
  209. flex-direction: column;
  210. color: #3b4144;
  211. }
  212. .uni-list-item__content-title {
  213. font-size: $uni-font-size-base;
  214. color: #3b4144;
  215. overflow: hidden;
  216. }
  217. .uni-list-item__content-note {
  218. margin-top: 6rpx;
  219. color: $uni-text-color-grey;
  220. font-size: $uni-font-size-sm;
  221. overflow: hidden;
  222. }
  223. .uni-list-item__extra {
  224. // width: 25%;
  225. /* #ifndef APP-NVUE */
  226. display: flex;
  227. /* #endif */
  228. flex-direction: row;
  229. justify-content: flex-end;
  230. align-items: center;
  231. }
  232. .uni-list-item__icon {
  233. display: flex;
  234. margin-right: 18rpx;
  235. flex-direction: row;
  236. justify-content: center;
  237. align-items: center;
  238. margin-left: 20rpx;
  239. }
  240. .uni-list-item__icon-img {
  241. // height: $uni-img-size-base;
  242. // width: $uni-img-size-base;
  243. height: 38rpx;
  244. width: 38rpx;
  245. }
  246. .uni-list-item__extra-text {
  247. color: $uni-text-color-grey;
  248. font-size: $uni-font-size-sm;
  249. }
  250. </style>