uni-list-item.vue 6.3 KB

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