uni-list-item.vue 6.1 KB

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