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