uni-list-item.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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" :custom-style="badgeStyle" />
  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 {Object} badgeStyle 数字角标样式
  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. badgeStyle:{
  141. type: Object,
  142. default () {
  143. return {}
  144. }
  145. },
  146. rightText: {
  147. type: String,
  148. default: ''
  149. },
  150. thumb: {
  151. type: String,
  152. default: ''
  153. },
  154. thumbSize: {
  155. type: String,
  156. default: 'base'
  157. },
  158. showExtraIcon: {
  159. type: [Boolean, String],
  160. default: false
  161. },
  162. extraIcon: {
  163. type: Object,
  164. default () {
  165. return {
  166. type: '',
  167. color: '#000000',
  168. size: 20
  169. };
  170. }
  171. },
  172. border: {
  173. type: Boolean,
  174. default: true
  175. }
  176. },
  177. // inject: ['list'],
  178. data() {
  179. return {
  180. isFirstChild: false
  181. };
  182. },
  183. mounted() {
  184. this.list = this.getForm()
  185. // 判断是否存在 uni-list 组件
  186. if (this.list) {
  187. if (!this.list.firstChildAppend) {
  188. this.list.firstChildAppend = true;
  189. this.isFirstChild = true;
  190. }
  191. }
  192. },
  193. methods: {
  194. /**
  195. * 获取父元素实例
  196. */
  197. getForm(name = 'uniList') {
  198. let parent = this.$parent;
  199. let parentName = parent.$options.name;
  200. while (parentName !== name) {
  201. parent = parent.$parent;
  202. if (!parent) return false
  203. parentName = parent.$options.name;
  204. }
  205. return parent;
  206. },
  207. onClick() {
  208. if (this.to !== '') {
  209. this.openPage();
  210. return;
  211. }
  212. if (this.clickable || this.link) {
  213. this.$emit('click', {
  214. data: {}
  215. });
  216. }
  217. },
  218. onSwitchChange(e) {
  219. this.$emit('switchChange', e.detail);
  220. },
  221. openPage() {
  222. if (['navigateTo', 'redirectTo', 'reLaunch', 'switchTab'].indexOf(this.link) !== -1) {
  223. this.pageApi(this.link);
  224. } else {
  225. this.pageApi('navigateTo');
  226. }
  227. },
  228. pageApi(api) {
  229. let callback = {
  230. url: this.to,
  231. success: res => {
  232. this.$emit('click', {
  233. data: res
  234. });
  235. },
  236. fail: err => {
  237. this.$emit('click', {
  238. data: err
  239. });
  240. }
  241. }
  242. switch (api) {
  243. case 'navigateTo':
  244. uni.navigateTo(callback)
  245. break
  246. case 'redirectTo':
  247. uni.redirectTo(callback)
  248. break
  249. case 'reLaunch':
  250. uni.reLaunch(callback)
  251. break
  252. case 'switchTab':
  253. uni.switchTab(callback)
  254. break
  255. default:
  256. uni.navigateTo(callback)
  257. }
  258. }
  259. }
  260. };
  261. </script>
  262. <style lang="scss">
  263. $uni-font-size-sm:12px;
  264. $uni-font-size-base:14px;
  265. $uni-font-size-lg:16px;
  266. $uni-spacing-col-lg: 12px;
  267. $uni-spacing-row-lg: 15px;
  268. $uni-img-size-sm:20px;
  269. $uni-img-size-base:26px;
  270. $uni-img-size-lg:40px;
  271. $uni-border-color:#e5e5e5;
  272. $uni-bg-color-hover:#f1f1f1;
  273. $uni-text-color-grey:#999;
  274. $list-item-pd: $uni-spacing-col-lg $uni-spacing-row-lg;
  275. .uni-list-item {
  276. /* #ifndef APP-NVUE */
  277. display: flex;
  278. /* #endif */
  279. font-size: $uni-font-size-lg;
  280. position: relative;
  281. justify-content: space-between;
  282. align-items: center;
  283. background-color: #fff;
  284. flex-direction: row;
  285. /* #ifdef H5 */
  286. cursor: pointer;
  287. /* #endif */
  288. }
  289. .uni-list-item--disabled {
  290. opacity: 0.3;
  291. }
  292. .uni-list-item--hover {
  293. background-color: $uni-bg-color-hover;
  294. }
  295. .uni-list-item__container {
  296. position: relative;
  297. /* #ifndef APP-NVUE */
  298. display: flex;
  299. /* #endif */
  300. flex-direction: row;
  301. padding: $list-item-pd;
  302. padding-left: $uni-spacing-row-lg;
  303. flex: 1;
  304. overflow: hidden;
  305. // align-items: center;
  306. }
  307. .container--right {
  308. padding-right: 0;
  309. }
  310. // .border--left {
  311. // margin-left: $uni-spacing-row-lg;
  312. // }
  313. .uni-list--border {
  314. position: absolute;
  315. top: 0;
  316. right: 0;
  317. left: 0;
  318. /* #ifdef APP-NVUE */
  319. border-top-color: $uni-border-color;
  320. border-top-style: solid;
  321. border-top-width: 0.5px;
  322. /* #endif */
  323. }
  324. /* #ifndef APP-NVUE */
  325. .uni-list--border:after {
  326. position: absolute;
  327. top: 0;
  328. right: 0;
  329. left: 0;
  330. height: 1px;
  331. content: '';
  332. -webkit-transform: scaleY(0.5);
  333. transform: scaleY(0.5);
  334. background-color: $uni-border-color;
  335. }
  336. /* #endif */
  337. .uni-list-item__content {
  338. /* #ifndef APP-NVUE */
  339. display: flex;
  340. /* #endif */
  341. padding-right: 8px;
  342. flex: 1;
  343. color: #3b4144;
  344. // overflow: hidden;
  345. flex-direction: column;
  346. justify-content: space-between;
  347. overflow: hidden;
  348. }
  349. .uni-list-item__content--center {
  350. justify-content: center;
  351. }
  352. .uni-list-item__content-title {
  353. font-size: $uni-font-size-base;
  354. color: #3b4144;
  355. overflow: hidden;
  356. }
  357. .uni-list-item__content-note {
  358. margin-top: 6rpx;
  359. color: $uni-text-color-grey;
  360. font-size: $uni-font-size-sm;
  361. overflow: hidden;
  362. }
  363. .uni-list-item__extra {
  364. // width: 25%;
  365. /* #ifndef APP-NVUE */
  366. display: flex;
  367. /* #endif */
  368. flex-direction: row;
  369. justify-content: flex-end;
  370. align-items: center;
  371. }
  372. .uni-list-item__header {
  373. /* #ifndef APP-NVUE */
  374. display: flex;
  375. /* #endif */
  376. flex-direction: row;
  377. align-items: center;
  378. }
  379. .uni-list-item__icon {
  380. margin-right: 18rpx;
  381. flex-direction: row;
  382. justify-content: center;
  383. align-items: center;
  384. }
  385. .uni-list-item__icon-img {
  386. /* #ifndef APP-NVUE */
  387. display: block;
  388. /* #endif */
  389. height: $uni-img-size-base;
  390. width: $uni-img-size-base;
  391. margin-right: 10px;
  392. }
  393. .uni-icon-wrapper {
  394. /* #ifndef APP-NVUE */
  395. display: flex;
  396. /* #endif */
  397. align-items: center;
  398. padding: 0 10px;
  399. }
  400. .flex--direction {
  401. flex-direction: column;
  402. /* #ifndef APP-NVUE */
  403. align-items: initial;
  404. /* #endif */
  405. }
  406. .flex--justify {
  407. /* #ifndef APP-NVUE */
  408. justify-content: initial;
  409. /* #endif */
  410. }
  411. .uni-list--lg {
  412. height: $uni-img-size-lg;
  413. width: $uni-img-size-lg;
  414. }
  415. .uni-list--base {
  416. height: $uni-img-size-base;
  417. width: $uni-img-size-base;
  418. }
  419. .uni-list--sm {
  420. height: $uni-img-size-sm;
  421. width: $uni-img-size-sm;
  422. }
  423. .uni-list-item__extra-text {
  424. color: $uni-text-color-grey;
  425. font-size: $uni-font-size-sm;
  426. }
  427. .uni-ellipsis-1 {
  428. /* #ifndef APP-NVUE */
  429. overflow: hidden;
  430. white-space: nowrap;
  431. text-overflow: ellipsis;
  432. /* #endif */
  433. /* #ifdef APP-NVUE */
  434. lines: 1;
  435. text-overflow:ellipsis;
  436. /* #endif */
  437. }
  438. .uni-ellipsis-2 {
  439. /* #ifndef APP-NVUE */
  440. overflow: hidden;
  441. text-overflow: ellipsis;
  442. display: -webkit-box;
  443. -webkit-line-clamp: 2;
  444. -webkit-box-orient: vertical;
  445. /* #endif */
  446. /* #ifdef APP-NVUE */
  447. lines: 2;
  448. text-overflow:ellipsis;
  449. /* #endif */
  450. }
  451. </style>