uni-list-item.vue 11 KB

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