uni-list-item.vue 9.7 KB

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