uni-list-item.vue 11 KB

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