uni-list-item.vue 11 KB

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