uni-list-item.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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],
  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. $list-item-pd: $uni-spacing-col-lg $uni-spacing-row-lg;
  257. .uni-list-item {
  258. /* #ifndef APP-NVUE */
  259. display: flex;
  260. /* #endif */
  261. font-size: $uni-font-size-lg;
  262. position: relative;
  263. justify-content: space-between;
  264. align-items: center;
  265. background-color: #fff;
  266. flex-direction: row;
  267. /* #ifdef H5 */
  268. cursor: pointer;
  269. /* #endif */
  270. }
  271. .uni-list-item--disabled {
  272. opacity: 0.3;
  273. }
  274. .uni-list-item--hover {
  275. background-color: $uni-bg-color-hover;
  276. }
  277. .uni-list-item__container {
  278. position: relative;
  279. /* #ifndef APP-NVUE */
  280. display: flex;
  281. /* #endif */
  282. flex-direction: row;
  283. padding: $list-item-pd;
  284. padding-left: $uni-spacing-row-lg;
  285. flex: 1;
  286. overflow: hidden;
  287. // align-items: center;
  288. }
  289. .container--right {
  290. padding-right: 0;
  291. }
  292. // .border--left {
  293. // margin-left: $uni-spacing-row-lg;
  294. // }
  295. .uni-list--border {
  296. position: absolute;
  297. top: 0;
  298. right: 0;
  299. left: 0;
  300. /* #ifdef APP-NVUE */
  301. border-top-color: $uni-border-color;
  302. border-top-style: solid;
  303. border-top-width: 0.5px;
  304. /* #endif */
  305. }
  306. /* #ifndef APP-NVUE */
  307. .uni-list--border:after {
  308. position: absolute;
  309. top: 0;
  310. right: 0;
  311. left: 0;
  312. height: 1px;
  313. content: '';
  314. -webkit-transform: scaleY(0.5);
  315. transform: scaleY(0.5);
  316. background-color: $uni-border-color;
  317. }
  318. /* #endif */
  319. .uni-list-item__content {
  320. /* #ifndef APP-NVUE */
  321. display: flex;
  322. /* #endif */
  323. padding-right: 8px;
  324. flex: 1;
  325. color: #3b4144;
  326. // overflow: hidden;
  327. flex-direction: column;
  328. justify-content: space-between;
  329. overflow: hidden;
  330. }
  331. .uni-list-item__content--center {
  332. justify-content: center;
  333. }
  334. .uni-list-item__content-title {
  335. font-size: $uni-font-size-base;
  336. color: #3b4144;
  337. overflow: hidden;
  338. }
  339. .uni-list-item__content-note {
  340. margin-top: 6rpx;
  341. color: $uni-text-color-grey;
  342. font-size: $uni-font-size-sm;
  343. overflow: hidden;
  344. }
  345. .uni-list-item__extra {
  346. // width: 25%;
  347. /* #ifndef APP-NVUE */
  348. display: flex;
  349. /* #endif */
  350. flex-direction: row;
  351. justify-content: flex-end;
  352. align-items: center;
  353. }
  354. .uni-list-item__header {
  355. /* #ifndef APP-NVUE */
  356. display: flex;
  357. /* #endif */
  358. flex-direction: row;
  359. align-items: center;
  360. }
  361. .uni-list-item__icon {
  362. margin-right: 18rpx;
  363. flex-direction: row;
  364. justify-content: center;
  365. align-items: center;
  366. }
  367. .uni-list-item__icon-img {
  368. /* #ifndef APP-NVUE */
  369. display: block;
  370. /* #endif */
  371. height: $uni-img-size-base;
  372. width: $uni-img-size-base;
  373. margin-right: 10px;
  374. }
  375. .uni-icon-wrapper {
  376. /* #ifndef APP-NVUE */
  377. display: flex;
  378. /* #endif */
  379. align-items: center;
  380. padding: 0 10px;
  381. }
  382. .flex--direction {
  383. flex-direction: column;
  384. /* #ifndef APP-NVUE */
  385. align-items: initial;
  386. /* #endif */
  387. }
  388. .flex--justify {
  389. /* #ifndef APP-NVUE */
  390. justify-content: initial;
  391. /* #endif */
  392. }
  393. .uni-list--lg {
  394. height: $uni-img-size-lg;
  395. width: $uni-img-size-lg;
  396. }
  397. .uni-list--base {
  398. height: $uni-img-size-base;
  399. width: $uni-img-size-base;
  400. }
  401. .uni-list--sm {
  402. height: $uni-img-size-sm;
  403. width: $uni-img-size-sm;
  404. }
  405. .uni-list-item__extra-text {
  406. color: $uni-text-color-grey;
  407. font-size: $uni-font-size-sm;
  408. }
  409. .uni-ellipsis-1 {
  410. /* #ifndef APP-NVUE */
  411. overflow: hidden;
  412. white-space: nowrap;
  413. text-overflow: ellipsis;
  414. /* #endif */
  415. /* #ifdef APP-NVUE */
  416. lines: 1;
  417. /* #endif */
  418. }
  419. .uni-ellipsis-2 {
  420. /* #ifndef APP-NVUE */
  421. overflow: hidden;
  422. text-overflow: ellipsis;
  423. display: -webkit-box;
  424. -webkit-line-clamp: 2;
  425. -webkit-box-orient: vertical;
  426. /* #endif */
  427. /* #ifdef APP-NVUE */
  428. lines: 2;
  429. /* #endif */
  430. }
  431. </style>