uni-list-item.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <template>
  2. <!-- #ifdef APP-NVUE -->
  3. <cell>
  4. <!-- #endif -->
  5. <view
  6. :class="{ 'uni-list-item--disabled': disabled }"
  7. :hover-class="(!clickable && !link) || disabled || showSwitch ? '' : 'uni-list-item--hover'"
  8. class="uni-list-item"
  9. @click.stop="onClick"
  10. >
  11. <view v-if="!isFirstChild" class="border--left" :class="{ 'uni-list--border': border }"></view>
  12. <view class="uni-list-item__container" :class="{ 'container--right': showArrow || link, 'flex--direction': direction === 'column' }">
  13. <slot name="header">
  14. <view class="uni-list-item__header">
  15. <view v-if="thumb" class="uni-list-item__icon"><image :src="thumb" class="uni-list-item__icon-img" :class="['uni-list--' + thumbSize]" /></view>
  16. <view v-else-if="showExtraIcon" class="uni-list-item__icon"><uni-icons :color="extraIcon.color" :size="extraIcon.size" :type="extraIcon.type" /></view>
  17. </view>
  18. </slot>
  19. <slot name="body">
  20. <view class="uni-list-item__content" :class="{ 'uni-list-item__content--center': thumb || showExtraIcon || showBadge || showSwitch }">
  21. <text v-if="title" class="uni-list-item__content-title" :class="[ellipsis !== 0 && ellipsis <= 2 ? 'uni-ellipsis-' + ellipsis : '']">{{ title }}</text>
  22. <text v-if="note" class="uni-list-item__content-note">{{ note }}</text>
  23. </view>
  24. </slot>
  25. <slot name="footer">
  26. <view v-if="rightText || showBadge || showSwitch" class="uni-list-item__extra" :class="{ 'flex--justify': direction === 'column' }">
  27. <text v-if="rightText" class="uni-list-item__extra-text">{{ rightText }}</text>
  28. <uni-badge v-if="showBadge" :type="badgeType" :text="badgeText" />
  29. <switch v-if="showSwitch" :disabled="disabled" :checked="switchChecked" @change="onSwitchChange" />
  30. </view>
  31. </slot>
  32. </view>
  33. <uni-icons v-if="showArrow || link" :size="16" class="uni-icon-wrapper" color="#bbb" type="arrowright" />
  34. </view>
  35. <!-- #ifdef APP-NVUE -->
  36. </cell>
  37. <!-- #endif -->
  38. </template>
  39. <script>
  40. /**
  41. * ListItem 列表子组件
  42. * @description 列表子组件
  43. * @tutorial https://ext.dcloud.net.cn/plugin?id=24
  44. * @property {String} title 标题
  45. * @property {String} note 描述
  46. * @property {String} thumb 左侧缩略图,若thumb有值,则不会显示扩展图标
  47. * @property {String} thumbSize = [lg|base|sm] 略缩图大小
  48. * @value lg 大图
  49. * @value base 一般
  50. * @value sm 小图
  51. * @property {String} badgeText 数字角标内容
  52. * @property {String} badgeType 数字角标类型,参考[uni-icons](https://ext.dcloud.net.cn/plugin?id=21)
  53. * @property {String} rightText 右侧文字内容
  54. * @property {Boolean} disabled = [true|false] 是否禁用
  55. * @property {Boolean} clickable = [true|false] 是否开启点击反馈
  56. * @property {String} link = [navigateTo|redirectTo|reLaunch|switchTab] 是否展示右侧箭头并开启点击反馈
  57. * @value navigateTo 同 uni.navigateTo()
  58. * @value redirectTo 同 uni.redirectTo()
  59. * @value reLaunch 同 uni.reLaunch()
  60. * @value switchTab 同 uni.switchTab()
  61. * @property {String | PageURIString} to 跳转目标页面
  62. * @property {Boolean} showBadge = [true|false] 是否显示数字角标
  63. * @property {Boolean} showSwitch = [true|false] 是否显示Switch
  64. * @property {Boolean} switchChecked = [true|false] Switch是否被选中
  65. * @property {Boolean} showExtraIcon = [true|false] 左侧是否显示扩展图标
  66. * @property {Object} extraIcon 扩展图标参数,格式为 {color: '#4cd964',size: '22',type: 'spinner'}
  67. * @property {String} direction = [row|column] 排版方向
  68. * @value row 水平排列
  69. * @value column 垂直排列
  70. * @event {Function} click 点击 uniListItem 触发事件
  71. * @event {Function} switchChange 点击切换 Switch 时触发
  72. */
  73. export default {
  74. name: 'UniListItem',
  75. props: {
  76. direction: {
  77. type: String,
  78. default: 'row'
  79. },
  80. title: {
  81. type: String,
  82. default: ''
  83. },
  84. note: {
  85. type: String,
  86. default: ''
  87. },
  88. ellipsis: {
  89. type: [Number],
  90. default: 0
  91. },
  92. disabled: {
  93. type: [Boolean, String],
  94. default: false
  95. },
  96. clickable: {
  97. type: Boolean,
  98. default: false
  99. },
  100. showArrow: {
  101. type: [Boolean, String],
  102. default: false
  103. },
  104. link: {
  105. type: [Boolean, String],
  106. default: false
  107. },
  108. to: {
  109. type: String,
  110. default: ''
  111. },
  112. showBadge: {
  113. type: [Boolean, String],
  114. default: false
  115. },
  116. showSwitch: {
  117. type: [Boolean, String],
  118. default: false
  119. },
  120. switchChecked: {
  121. type: [Boolean, String],
  122. default: false
  123. },
  124. badgeText: {
  125. type: String,
  126. default: ''
  127. },
  128. badgeType: {
  129. type: String,
  130. default: 'success'
  131. },
  132. rightText: {
  133. type: String,
  134. default: ''
  135. },
  136. thumb: {
  137. type: String,
  138. default: ''
  139. },
  140. thumbSize: {
  141. type: String,
  142. default: 'base'
  143. },
  144. showExtraIcon: {
  145. type: [Boolean, String],
  146. default: false
  147. },
  148. extraIcon: {
  149. type: Object,
  150. default() {
  151. return {
  152. type: 'contact',
  153. color: '#000000',
  154. size: 20
  155. };
  156. }
  157. },
  158. border: {
  159. type: Boolean,
  160. default: true
  161. }
  162. },
  163. // inject: ['list'],
  164. data() {
  165. return {
  166. isFirstChild: false
  167. };
  168. },
  169. mounted() {
  170. this.list = this.getForm()
  171. // 判断是否存在 uni-list 组件
  172. if(this.list){
  173. if (!this.list.firstChildAppend) {
  174. this.list.firstChildAppend = true;
  175. this.isFirstChild = true;
  176. }
  177. }
  178. },
  179. methods: {
  180. /**
  181. * 获取父元素实例
  182. */
  183. getForm(name = 'uniList') {
  184. let parent = this.$parent;
  185. let parentName = parent.$options.name;
  186. while (parentName !== name) {
  187. parent = parent.$parent;
  188. if (!parent) return false
  189. parentName = parent.$options.name;
  190. }
  191. return parent;
  192. },
  193. onClick() {
  194. if (this.to !== '') {
  195. this.openPage();
  196. return;
  197. }
  198. if (this.clickable || this.link) {
  199. this.$emit('click', {
  200. data: {}
  201. });
  202. }
  203. },
  204. onSwitchChange(e) {
  205. this.$emit('switchChange', e.detail);
  206. },
  207. openPage() {
  208. if (['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'].indexOf(this.link) !== -1) {
  209. this.pageApi(this.link);
  210. } else {
  211. this.pageApi('navigateTo');
  212. }
  213. },
  214. pageApi(api) {
  215. uni[api]({
  216. url: this.to,
  217. success: res => {
  218. this.$emit('click', {
  219. data: res
  220. });
  221. },
  222. fail: err => {
  223. this.$emit('click', {
  224. data: err
  225. });
  226. console.error(err.errMsg);
  227. }
  228. });
  229. }
  230. }
  231. };
  232. </script>
  233. <style lang="scss">
  234. $list-item-pd: $uni-spacing-col-lg $uni-spacing-row-lg;
  235. .uni-list-item {
  236. /* #ifndef APP-NVUE */
  237. display: flex;
  238. /* #endif */
  239. font-size: $uni-font-size-lg;
  240. position: relative;
  241. justify-content: space-between;
  242. align-items: center;
  243. background-color: #fff;
  244. flex-direction: row;
  245. /* #ifdef H5 */
  246. cursor: pointer;
  247. /* #endif */
  248. }
  249. .uni-list-item--disabled {
  250. opacity: 0.3;
  251. }
  252. .uni-list-item--hover {
  253. background-color: $uni-bg-color-hover;
  254. }
  255. .uni-list-item__container {
  256. position: relative;
  257. /* #ifndef APP-NVUE */
  258. display: flex;
  259. /* #endif */
  260. flex-direction: row;
  261. padding: $list-item-pd;
  262. padding-left: $uni-spacing-row-lg;
  263. flex: 1;
  264. overflow: hidden;
  265. // align-items: center;
  266. }
  267. .container--right {
  268. padding-right: 0;
  269. }
  270. // .border--left {
  271. // margin-left: $uni-spacing-row-lg;
  272. // }
  273. .uni-list--border {
  274. position: absolute;
  275. top: 0;
  276. right: 0;
  277. left: 0;
  278. /* #ifdef APP-NVUE */
  279. border-top-color: $uni-border-color;
  280. border-top-style: solid;
  281. border-top-width: 0.5px;
  282. /* #endif */
  283. }
  284. /* #ifndef APP-NVUE */
  285. .uni-list--border:after {
  286. position: absolute;
  287. top: 0;
  288. right: 0;
  289. left: 0;
  290. height: 1px;
  291. content: '';
  292. -webkit-transform: scaleY(0.5);
  293. transform: scaleY(0.5);
  294. background-color: $uni-border-color;
  295. }
  296. /* #endif */
  297. .uni-list-item__content {
  298. /* #ifndef APP-NVUE */
  299. display: flex;
  300. /* #endif */
  301. padding-right: 8px;
  302. flex: 1;
  303. color: #3b4144;
  304. // overflow: hidden;
  305. flex-direction: column;
  306. justify-content: space-between;
  307. overflow: hidden;
  308. }
  309. .uni-list-item__content--center {
  310. justify-content: center;
  311. }
  312. .uni-list-item__content-title {
  313. font-size: $uni-font-size-base;
  314. color: #3b4144;
  315. overflow: hidden;
  316. }
  317. .uni-list-item__content-note {
  318. margin-top: 6rpx;
  319. color: $uni-text-color-grey;
  320. font-size: $uni-font-size-sm;
  321. overflow: hidden;
  322. }
  323. .uni-list-item__extra {
  324. // width: 25%;
  325. /* #ifndef APP-NVUE */
  326. display: flex;
  327. /* #endif */
  328. flex-direction: row;
  329. justify-content: flex-end;
  330. align-items: center;
  331. }
  332. .uni-list-item__header {
  333. /* #ifndef APP-NVUE */
  334. display: flex;
  335. /* #endif */
  336. flex-direction: row;
  337. align-items: center;
  338. }
  339. .uni-list-item__icon {
  340. margin-right: 18rpx;
  341. flex-direction: row;
  342. justify-content: center;
  343. align-items: center;
  344. }
  345. .uni-list-item__icon-img {
  346. /* #ifndef APP-NVUE */
  347. display: block;
  348. /* #endif */
  349. height: $uni-img-size-base;
  350. width: $uni-img-size-base;
  351. margin-right: 10px;
  352. }
  353. .uni-icon-wrapper {
  354. /* #ifndef APP-NVUE */
  355. display: flex;
  356. /* #endif */
  357. align-items: center;
  358. padding: 0 10px;
  359. }
  360. .flex--direction {
  361. flex-direction: column;
  362. /* #ifndef APP-NVUE */
  363. align-items: initial;
  364. /* #endif */
  365. }
  366. .flex--justify {
  367. /* #ifndef APP-NVUE */
  368. justify-content: initial;
  369. /* #endif */
  370. }
  371. .uni-list--lg {
  372. height: $uni-img-size-lg;
  373. width: $uni-img-size-lg;
  374. }
  375. .uni-list--base {
  376. height: $uni-img-size-base;
  377. width: $uni-img-size-base;
  378. }
  379. .uni-list--sm {
  380. height: $uni-img-size-sm;
  381. width: $uni-img-size-sm;
  382. }
  383. .uni-list-item__extra-text {
  384. color: $uni-text-color-grey;
  385. font-size: $uni-font-size-sm;
  386. }
  387. .uni-ellipsis-1 {
  388. /* #ifndef APP-NVUE */
  389. overflow: hidden;
  390. white-space: nowrap;
  391. text-overflow: ellipsis;
  392. /* #endif */
  393. /* #ifdef APP-NVUE */
  394. lines: 1;
  395. /* #endif */
  396. }
  397. .uni-ellipsis-2 {
  398. /* #ifndef APP-NVUE */
  399. overflow: hidden;
  400. text-overflow: ellipsis;
  401. display: -webkit-box;
  402. -webkit-line-clamp: 2;
  403. -webkit-box-orient: vertical;
  404. /* #endif */
  405. /* #ifdef APP-NVUE */
  406. lines: 2;
  407. /* #endif */
  408. }
  409. </style>